您的位置:首页 > 编程语言 > Java开发

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

结果:

gaoxiang1male1
gaoxiang2male2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: