参考:http://viralpatel.net/blogs/spring-mvc-freemarker-ftl-example/

上图:

目录层级:

启动后的访问地址:http://localhost:8080/Freemarker_SpringMVC_example/index.html

源码:链接:http://pan.baidu.com/s/1bTtvEQ 密码:k00e

User.java:

 1 package net.viralpatel;
 2
 3 public class User {
 4     private String firstname;
 5     private String lastname;
 6
 7     public User() {
 8     }
 9
10     public String getFirstname() {
11         return firstname;
12     }
13
14     public void setFirstname(String firstname) {
15         this.firstname = firstname;
16     }
17
18     public String getLastname() {
19         return lastname;
20     }
21
22     public void setLastname(String lastname) {
23         this.lastname = lastname;
24     }
25
26     public User(String firstname, String lastname) {
27         this.firstname = firstname;
28         this.lastname = lastname;
29
30     }
31
32     // Add Getter and Setter methods
33
34 }

UserController.java:

 1 package net.viralpatel;
 2
 3 import java.util.ArrayList;
 4 import java.util.List;
 5
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.ui.ModelMap;
 8 import org.springframework.web.bind.annotation.ModelAttribute;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestMethod;
11 /**
12  * 参考自:http://viralpatel.net/blogs/spring-mvc-freemarker-ftl-example/
13  * 本地服务启动后访问地址:http://localhost:8080/Freemarker_SpringMVC_example/index.html
14  *
15  * @author Wei
16  * @time  2016年12月14日 下午4:15:43
17  */
18 @Controller
19 public class UserController {
20     /**
21      * Static list of users to simulate Database
22      */
23     private static List<User> userList = new ArrayList<User>();
24
25     //Initialize the list with some data for index screen
26     static {
27         userList.add(new User("Bill", "Gates"));
28         userList.add(new User("Steve", "Jobs"));
29         userList.add(new User("Larry", "Page"));
30         userList.add(new User("Sergey", "Brin"));
31         userList.add(new User("Larry", "Ellison"));
32     }
33
34     /**
35      * Saves the static list of users in model and renders it
36      * via freemarker template.
37      *
38      * @param model
39      * @return The index view (FTL)
40      */
41     @RequestMapping(value = "/index", method = RequestMethod.GET)
42     public String index(@ModelAttribute("model") ModelMap model) {
43
44         model.addAttribute("userList", userList);
45
46         return "index";
47     }
48
49     /**
50      * Add a new user into static user lists and display the
51      * same into FTL via redirect
52      *
53      * @param user
54      * @return Redirect to /index page to display user list
55      */
56     @RequestMapping(value = "/add", method = RequestMethod.POST)
57     public String add(@ModelAttribute("user") User user) {
58
59         if (null != user && null != user.getFirstname()
60                 && null != user.getLastname() && !user.getFirstname().isEmpty()
61                 && !user.getLastname().isEmpty()) {
62
63             synchronized (userList) {
64                 userList.add(user);
65             }
66
67         }
68
69         return "redirect:index.html";
70     }
71
72 }

ftl:

 1 <html>
 2 <head><title>ViralPatel.net - FreeMarker Spring MVC Hello World</title>
 3 <body>
 4 <div id="header">
 5 <H2>
 6     <a href="http://viralpatel.net"><img height="37" width="236" border="0px" src="http://viralpatel.net/blogs/wp-content/themes/vp/images/logo.png" align="left"/></a>
 7     FreeMarker Spring MVC Hello World
 8 </H2>
 9 </div>
10
11 <div id="content">
12
13   <fieldset>
14       <legend>Add User</legend>
15   <form name="user" action="add.html" method="post">
16       Firstname: <input type="text" name="firstname" />    <br/>
17       Lastname: <input type="text" name="lastname" />    <br/>
18       <input type="submit" value="   Save   " />
19   </form>
20   </fieldset>
21   <br/>
22   <table class="datatable">
23       <tr>
24           <th>Firstname</th>  <th>Lastname</th>
25       </tr>
26     <#list model["userList"] as user>
27       <tr>
28           <strong><td>${user.firstname}</td></strong> <td>${user.lastname}</td>
29       </tr>
30     </#list>
31   </table>
32
33 </div>
34 </body>
35 </html>  

spring-servlet.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 6
 7     <!-- freemarker config -->
 8     <bean id="freemarkerConfig"
 9         class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
10         <property name="templateLoaderPath" value="/WEB-INF/ftl/" />
11     </bean>
12
13     <!-- View resolvers can also be configured with ResourceBundles or XML files.
14         If you need different view resolving based on Locale, you have to use the
15         resource bundle resolver. -->
16     <bean id="viewResolver"
17         class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
18         <property name="cache" value="true" />
19         <property name="prefix" value="" />
20         <property name="suffix" value=".ftl" />
21     </bean>
22
23     <context:component-scan base-package="net.viralpatel" />
24
25 </beans>

web.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3         xmlns="http://java.sun.com/xml/ns/javaee"
 4         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 6         id="WebApp_ID" version="3.0">
 7
 8   <display-name>Freemarker_SpringMVC_example</display-name>
 9   <welcome-file-list>
10     <welcome-file>index.html</welcome-file>
11     <welcome-file>index.jsp</welcome-file>
12   </welcome-file-list>
13
14   <servlet>
15         <servlet-name>spring</servlet-name>
16         <servlet-class>
17             org.springframework.web.servlet.DispatcherServlet
18         </servlet-class>
19         <load-on-startup>1</load-on-startup>
20     </servlet>
21     <servlet-mapping>
22         <servlet-name>spring</servlet-name>
23         <!-- <url-pattern>*.html</url-pattern> -->
24         <url-pattern>/</url-pattern>
25     </servlet-mapping>
26 </web-app>

转载于:https://www.cnblogs.com/Sunnor/p/6180346.html

springmvc结合freemarker,非自定义标签相关推荐

  1. freemarker解析html标签,【转】Freemarker输出$和html标签等特殊符号

    原文:http://blog.csdn.net/achilles12345/article/details/41820507 场景:程序员都不喜欢看文档,而更喜欢抄例子.所以,我们把平台组的组件都做成 ...

  2. spring mvc项目中利用freemarker生成自定义标签

    2019独角兽企业重金招聘Python工程师标准>>> spring mvc项目中利用freemarker生成自定义标签 博客分类: java spring mvc +freemar ...

  3. android 自己定义标签的使用,实现扁平化UI设计

    2014年8月6日11:06:44 android对自己定义标签的使用.实现扁平化UI设计: 1.attrs.xml文件里自己定义标签 如: <?xml version="1.0&qu ...

  4. Spring之配置非自定义Bean

    目录 一:概述 二:代码演示 1)配置Druid数据源交由Spring管理 一:概述 以上在xml中配置的Bean都是自己定义的, 例如:UserDaolmpl, UserServicelmpl.但是 ...

  5. css应用重定义标签设置背景、导航条及rgba

    1.应用重定义标签设置背景 2.设置图像列表 3.导航条的书写 4.空链接 1.设置背景和字体等 body{background-image: url(img/logo.jpg);background ...

  6. 绝地求生信号枪只能在服务器吗,绝地求生信号枪非自定义可以捡到吗 绝地求生信号枪在哪可以捡到...

    最近绝地求生中信号枪是大家讨论的热点,这款可以召唤空投的神器时刻都在骚动着玩家们的心,但是在目前为止还没有人在游戏中捡到过这把信号枪,大家所看到的召唤空投的景象全都是在自定义服务器重,那么绝地求生信号 ...

  7. 023_html引用和术语定义标签

    1. <q>标签 1.1. <q>标签定义短的引用. 1.2. 浏览器通常会为<q>元素包围引号. 1.3. 例子 1.3.1. 代码 <!DOCTYPE H ...

  8. springmvc使用freemarker

    一.导入freemarker的jar包 freemarker-2.3.15.jar 二.在springmvc.xml文件中配置freemarker的视图解析器 <!-- freemarker的视 ...

  9. 【用户画像】用户画像添加标签、定义标签任务、搭建工程

    文章目录 一 标签管理 1 标签列表 2 添加标签 (1)添加一级标签 (2)添加二级标签 (3)添加三级标签 (4)添加四级标签 (5)数据库标签字段说明 (6)数据库中存储 3 标签任务 (1)填 ...

最新文章

  1. DeepMind用神经网络求解MIP后,攻破运筹学只是时间问题?你想多了
  2. COMMAND 模式
  3. UEditor添加自定义弹窗 插入音频地址
  4. 【CodeForces - 349B】Color the Fence (贪心,填数)
  5. 十六进制转八进制c++代码_如何将十六进制代码上传到微控制器?
  6. HDOJ 1274 展开字符串
  7. 百度地图开发实例文章(一)
  8. VGG使用重复元素的网络
  9. 基于javaweb实现人脸识别
  10. 泛微e9隐藏明细表_泛微协同 泛微OA e-cology产品功能清单 模块列表
  11. Android 批量修改文件名称
  12. 用CAD看图软件查找文字需要怎么做
  13. ES index not_analyzed
  14. 电子招标采购系统源码功能清单
  15. 如何评价B端产品经理的能力
  16. C语言中access函数的使用
  17. [万字]java后端研发岗秋招常见面经总结
  18. 那些可盐可甜的AI声音,可能都是出自于这家公司
  19. Nginx教程(1)
  20. 劲乐团新人指导[toxin]

热门文章

  1. 下列关于线程调度的叙述中,错误的是()。
  2. c4503文件服务器,理光C3503/C4503/C5503检查状态下各项目说明解释
  3. shell脚本执行命令错误处理
  4. google protobuf 实体类和java对象互转_ProtoBuf为什么被吹出天际
  5. 【java】visualvm 插件 visual gc 使用介绍
  6. 【Clickhouse】Clickhouse 整合 Prometheus 监控 运行时状态
  7. 60-100-028-使用-MySQL 主从复制
  8. 【Flink】Flink Invalid timestamp -1 Timestamp should always be none-negative or null
  9. 【Spring】No suitable HttpMessageConverter repsonse type
  10. 【Java】DNS缓存