springmvc结合freemarker,非自定义标签

参考: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>
时间: 2024-10-20 02:44:56

springmvc结合freemarker,非自定义标签的相关文章

freemarker自己定义标签报错(二)

freemarker自己定义标签 1.错误描写叙述 freemarker.core.ParseException: Unexpected end of file reached. at freemarker.core.FMParser.generateParseException(FMParser.java:4702) at freemarker.core.FMParser.jj_consume_token(FMParser.java:4573) at freemarker.core.FMPar

freemarker自己定义标签报错(七)

1.错误描写叙述 六月 09, 2014 11:11:09 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template processing error: "Expression a is undefined on line 27, column 23 in tag.ftl." Expression a is undefined on line 27, column 23 in tag.ftl. The pro

freemarker自己定义标签(一)

freemarker自己定义标签 1.自己定义标签说明 宏变量存储模板片段能够被用作自己定义指令macro 2.演示样例说明 <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>freemarker自己定义标签</title> </head> <body> &

freemarker自己定义标签报错(三)

freemarker自己定义标签 1.错误描写叙述 freemarker.core.ParseException: Encountered " " at line 14, column 12 in myself.ftl. Was expecting one of: <STRING_LITERAL> ... <RAW_STRING> ... "false" ... "true" ... <INTEGER> ...

SpringMVC和Freemarker整合,带自定义标签的使用方法

SpringMVC和Freemarker整合,带自定义标签的使用方法. [参考来源:http://www.360doc.com/content/14/1225/14/1007797_435663342.shtml] SpringMVC现在是比较热门的一种框架了,使用起来感觉还是很不错的,现在我分享一下集体的配置和使用,希望对学习SpringMVC的朋友有用.一.首先我们做准备工作,下载Spring包,下载Freemarker包.二.配置web.xml. 1 2 3 4 5 6 7 8 9 10

SpringMVC与Freemarker的集成以及多视图解析器配置

一.Spring MVC视图解析器 视图解析器的工作流程大致是这样的:Controller的某个方法执行完成以后,返回一个视图(比如:listUser),视图解析器要做的工作就是找到某个对象 来完成视图的渲染,或者跳转到其他的逻辑视图.这里的渲染对象通常就是我们的jsp文件或者我们下面用的Freemarker(例如listUser.jsp或者 listUser.ftl).渲染完成以后,将解析结果发送到客户端浏览器. 下面介绍一下本文需要用到的解析器(更多解析器资料,请参考 http://e-fr

SpringMVC与Freemarker集成,配置项目全局的绝对路径

我发现网上这方面的相关内容不是很多(当然我学习的不是很深入).在开源中国上也发了一个贴子,有网友帮忙解答,下面附上解决方案,如果对大家有所帮助,我心甚慰! 首先定义一个类,继承FreeMarkerView. import org.springframework.web.servlet.view.freemarker.FreeMarkerView; import javax.servlet.http.HttpServletRequest; import java.util.Map; public 

SpringMVC&lt;from:form&gt;表单标签和&lt;input&gt;表单标签简介 转http://blog.csdn.net/hp_yangpeng/article/details/51906654

SpringMVC<from:form>表单标签和<input>表单标签简介 在使用SpringMVC的时候我们可以使用spring封装的一系列表单标签,这些标签都可以访问到ModelMap中的内容.下面将对这些标签一一介绍. 在正式介绍SpringMVC的表单标签之前,我们需要先在JSP中声明使用的标签,具体做法是在JSP文件的顶部加入以下指令: Jsp代码 <%@taglib uri="http://www.springframework.org/tags/fo

SpringMVC与freemarker整合

FreeMark FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP.它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等. 特性 通用 能够生成各种文本:HTML.XML.RTF.Java源代码等等. 易于嵌入到产品中:轻量级:不需要Servlet环境. 插件式模板载入器:可以从任何源载入模板,如本地文件.数据库等等. 可以按所需生成文本:保存到本