freemarker、jsp多视图解析器Spring配置
来源:互联网 发布:c语言中如何判断闰年 编辑:程序博客网 时间:2024/06/05 19:06
原文地址,转载请注明出处:http://blog.csdn.net/qq_34021712/article/details/71147364 ©王赛超
前提条件
maven依赖
<!-- 和Spring整合需要此jar包 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.1.3.RELEASE</version></dependency><!-- 使用的freemarker版本 --><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.23</version></dependency>freemarker和SpringMVC整合
1.如果希望前台页面全部使用freemarker渲染,不再使用jsp的话,进行以下配置
完整项目:spring_freemarker1下载
在springmvc.xml中配置以下内容<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置扫描的Controller --><context:component-scan base-package="com.test.freemarker" /><!-- 使用注解 --><mvc:annotation-driven /><!-- Freemarker配置 --> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"><property name="templateLoaderPath" value="/WEB-INF/ftl/" /><property name="defaultEncoding" value="UTF-8" /></bean><!--视图解释器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="suffix"> <!-- 指明freemarker模板扩展名 --> <value>.ftl</value> </property> <property name="contentType" value="text/html;charset=UTF-8"></property> </bean> </beans>Controller类内容如下,那个Student类自己写一个就行了
@RequestMapping("freemarker")public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception { //这里指明使用的是哪一个模板,文件的路径为配置文件中/WEB-INF/ftl/下ModelAndView mv = new ModelAndView("first");//最简单的数据测试mv.addObject("name", "张三");mv.addObject("age", "18");mv.addObject("classzs", "五班");//list数据测试List list=new ArrayList<>();list.add(new Student("蛮王", "20", "三班"));list.add(new Student("寒冰", "18", "四班"));list.add(new Student("德玛", "25", "二班"));mv.addObject("students",list);return mv; }first.ftl模板内容
<html><head><title>测试freemarker</title></head><body>1.测试简单的数据<br><label>姓名:</label>${name}<br><label>年龄:</label>${age}<br><label>班级:</label>${classzs}<br>2.测试list数据<br><#list students as s>${s_index}-${s.name}-${s.age}-${s.classzs}<br></#list></body></html>
2.如果我们不希望所有的动态页面请求都使用Freemarker来渲染,那就需要配置多个视图解析器。
完整项目:spring_freemarker2下载
在springmvc.xml中配置以下内容<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置扫描的Controller --><context:component-scan base-package="com.test.freemarker" /><!-- 使用注解 --><mvc:annotation-driven /> <!--配置Jsp视图解析器--> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" /> <property name="order" value="1"/> </bean> <!-- 配置freeMarker视图解析器 --> <bean id="ftlViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/> <property name="contentType" value="text/html; charset=utf-8"/> <property name="cache" value="true" /> <property name="suffix" value=".ftl" /> <property name="order" value="0"/> </bean> <!-- 配置freeMarker的模板路径 --> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/ftl/" /> <property name="defaultEncoding" value="utf-8" /> <property name="freemarkerVariables"> <map> <entry key="xml_escape" value-ref="fmXmlEscape" /> </map> </property> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">3600</prop> </props> </property> </bean> <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/> </beans>
Controller类内容如下
package com.test.freemarker;import java.util.ArrayList;import java.util.List;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class FreemarkerController {@RequestMapping("freemarker") public String handleRequest(ModelMap model) throws Exception { //这里指明使用的是哪一个模板,文件的路径为配置文件中/WEB-INF/ftl/下 //ModelAndView mv = new ModelAndView("first"); //最简单的数据测试 model.addAttribute("name", "张三"); model.addAttribute("age", "18"); model.addAttribute("classzs", "五班");//list数据测试List list=new ArrayList<>();list.add(new Student("蛮王", "20", "三班"));list.add(new Student("寒冰", "18", "四班"));list.add(new Student("德玛", "25", "二班"));model.addAttribute("students",list); return "first"; } @RequestMapping("toJsp")public String toJsp(ModelMap model) throws Exception { model.addAttribute("name", "张三");model.addAttribute("age", "18");model.addAttribute("classzs", "五班");return "hello";} public class Student{private String name;//姓名private String age;//年龄private String classzs;//班级public String getName() {return name;}public String getAge() {return age;}public String getClasszs() {return classzs;}public void setName(String name) {this.name = name;}public void setAge(String age) {this.age = age;}public void setClasszs(String classzs) {this.classzs = classzs;}public Student(String name, String age, String classzs) {super();this.name = name;this.age = age;this.classzs = classzs;}}}
JSP自己写一个就OK
注意:
1.在视图解析器中有一个<property name="order" value="orderValue"/>的配置,这个配置表示解析器的优先级别。我们将FreeMarkerViewResolver的级别设为0,将InternalResourceViewResolver的级别设为1。这样,解析器就会优先使用 FreeMarkerViewResolver 进行解析,如果找不到相应的模板,就使用InternalResourceViewResolver进行解析,如果还找不到页面,就会产生一个404错误!
2.InternalResourceViewResolver比较特殊的,只要视图名传给他,都不会再交给下一个order,而是直接forward,所以他的order应该最大,否则后面的视图解析器都不能有机会处理。 0 0
- Spring MVC配置多个视图解析器(FreeMarker,JSP)
- freemarker、jsp多视图解析器Spring配置
- spring配置freemarker视图解析器
- spring mvc支持多视图解析(jsp,velocity,freemarker)
- Spring mvc 配置多视图 jsp freemarker velocity等
- Spring MVC多视图配置(jsp, freemarker, Velocity)
- Spring MVC 中如何设置使用多视图解析器--JSP与FreeMarker合作工作
- FreeMarker视图解析器配置
- jsp、freemarker、json多视图混合解析
- Spring MVC的多视图解析器配置及与Freemarker的集成
- Spring MVC的多视图解析器配置及与Freemarker的集成
- Spring MVC的多视图解析器配置及与Freemarker的集成
- Spring MVC的多视图解析器配置及与Freemarker的集成
- Spring MVC的多视图解析器配置及与Freemarker的集成
- Spring MVC的多视图解析器配置及与Freemarker的集成
- Spring MVC的多视图解析器配置及与Freemarker的集成
- ***Spring MVC的多视图解析器配置及与Freemarker的集成
- Spring MVC的多视图解析器配置及与Freemarker的集成
- 【SOA】2.Service Oriented Architecture (SOA)
- SuperMap制图实用指南
- Kinect获取深度图,鼠标点击获取该点三维坐标并保存txt
- HttpClient --post请求数据
- #深度优先算法(DFS)
- freemarker、jsp多视图解析器Spring配置
- C++邻接矩阵实现有向图、无向图
- ~求两个节点的最近公共祖先~
- 自定义圆环点击不同位置有不同效果
- linux初级学习之访问网络共享文件系统2-7
- python编码错误:UnicodeDecodeError: 'utf8' codec can't decode
- list为空判断
- jQuery代码怎么写更好
- 二分快速幂应用题