spring mvc 使用Freemarker模板
2007-06-19 00:06
357 查看
首先,定义一个freemarker模板,解析对象studentList
/freemarker/success.ftl
<html>
<body>
<table border="1">
<#list studentList as student>
<tr>
<td>${student.name}</td>
<td>${student.sex}</td>
</tr>
</#list>
</table>
</body>
</html>
JavaBean
package model;
public class Student ...{
private String name;
private String sex;
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getSex() ...{
return sex;
}
public void setSex(String sex) ...{
this.sex = sex;
}
}
编写控制器:
package Action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Student;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;
/** *//**
* @@org.springframework.web.servlet.handler.commonsattributes.PathMap("/home.mvc");
*/
public class FreemarkerHomeController extends AbstractCommandController ...{
protected ModelAndView handle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, BindException arg3) throws Exception ...{
// TODO 自动生成方法存根
return null;
}
protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception ...{
Student stu1=new Student();
stu1.setName("gaoxiang1");
stu1.setSex("male1");
Student stu2=new Student();
stu2.setName("gaoxiang2");
stu2.setSex("male2");
List a=new ArrayList();
a.add(stu1);
a.add(stu2);
System.out.println(a);
return new ModelAndView("success","studentList",a);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/train-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>train</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>train</servlet-name>
<url-pattern>*.mvc</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>character</filter-name>
<filter-class>Action.CharacterFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>character</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
train-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/freemarker.mvc">FreemarkerHomeController</prop>
</props>
</property>
</bean>
<bean id="FreemarkerHomeController" class="Action.FreemarkerHomeController">
</bean>
<!-- freemarker页面解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix">
<value>.ftl</value>
</property>
<property name="viewClass">
<value>org.springframework.web.servlet.view.freemarker.FreeMarkerView</value>
</property>
</bean>
<!-- 配置Freemarker -->
<bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath">
<value>/freemarker/</value>
</property>
<property name="freemarkerSettings"><!-- 如果模板不经常更新,此属性设置更新延迟时间 -->
<props>
<prop key="template_update_delay">3600</prop>
</props>
</property>
</bean>
</beans>
执行/freemarker.mvc
结果:
/freemarker/success.ftl
<html>
<body>
<table border="1">
<#list studentList as student>
<tr>
<td>${student.name}</td>
<td>${student.sex}</td>
</tr>
</#list>
</table>
</body>
</html>
JavaBean
package model;
public class Student ...{
private String name;
private String sex;
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getSex() ...{
return sex;
}
public void setSex(String sex) ...{
this.sex = sex;
}
}
编写控制器:
package Action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Student;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;
/** *//**
* @@org.springframework.web.servlet.handler.commonsattributes.PathMap("/home.mvc");
*/
public class FreemarkerHomeController extends AbstractCommandController ...{
protected ModelAndView handle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, BindException arg3) throws Exception ...{
// TODO 自动生成方法存根
return null;
}
protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception ...{
Student stu1=new Student();
stu1.setName("gaoxiang1");
stu1.setSex("male1");
Student stu2=new Student();
stu2.setName("gaoxiang2");
stu2.setSex("male2");
List a=new ArrayList();
a.add(stu1);
a.add(stu2);
System.out.println(a);
return new ModelAndView("success","studentList",a);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/train-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>train</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>train</servlet-name>
<url-pattern>*.mvc</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>character</filter-name>
<filter-class>Action.CharacterFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>character</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
train-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/freemarker.mvc">FreemarkerHomeController</prop>
</props>
</property>
</bean>
<bean id="FreemarkerHomeController" class="Action.FreemarkerHomeController">
</bean>
<!-- freemarker页面解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix">
<value>.ftl</value>
</property>
<property name="viewClass">
<value>org.springframework.web.servlet.view.freemarker.FreeMarkerView</value>
</property>
</bean>
<!-- 配置Freemarker -->
<bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath">
<value>/freemarker/</value>
</property>
<property name="freemarkerSettings"><!-- 如果模板不经常更新,此属性设置更新延迟时间 -->
<props>
<prop key="template_update_delay">3600</prop>
</props>
</property>
</bean>
</beans>
执行/freemarker.mvc
结果:
gaoxiang1 | male1 |
gaoxiang2 | male2 |
相关文章推荐
- Spring MVC中使用自定义TemplateLoader对freemarker模板做全局html转义
- SpringBoot-07引擎模板thymeleaf和freemarker的使用
- spring boot中使用FreeMarker模板
- Spring MVC 学习总结(七)——FreeMarker模板引擎与动态页面静态化
- Spring Boot 与 Kotlin使用Freemarker模板引擎渲染web视图
- 如何在freemarker模板里面使用struts2的标签
- FreeMarker使用doc模板
- 模板引擎freemarker的简单使用教程
- 如何在freemarker模板里面使用struts2的标签
- 如何在freemarker模板里面使用struts2的标签
- 【FreeMarker模板引擎】5.freemarker结合Struts2使用
- freemarker使用String模板进行渲染展示
- springboot 使用FreeMarker模板(转)
- java使用 freemarker 模板生成word文件
- Spring MVC 学习总结(七)——FreeMarker模板引擎与动态页面静态化
- 使用Freemarker模板生成JAVA代码
- Freemarker模板引擎的使用总结(一)
- spring使用FreeMarker模板发送邮件及附件笔记
- 使用Spring Mvc 转发 带着模板 父页面 之解决方法 decorators.xml
- Spring MVC使用FreeMarker调用java静态方法