freemarker、jsp多视图解析器Spring配置

原文地址,转载请注明出处: 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;

@Controller
public 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应该最大,否则后面的视图解析器都不能有机会处理。
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值