freemarker、jsp多檢視解析器Spring配置
阿新 • • 發佈:2019-01-09
前提條件
maven依賴
freemarker和SpringMVC整合<!-- 和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>
1.如果希望前臺頁面全部使用freemarker渲染,不再使用jsp的話,進行以下配置
在springmvc.xml中配置以下內容Controller類內容如下,那個Student類自己寫一個就行了<?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>
first.ftl模板內容@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; }
<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來渲染,那就需要配置多個檢視解析器。
在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應該最大,否則後面的檢視解析器都不能有機會處理。