springmvc整合jsp和freemark兩個模板引擎
加入freemark.jar包,在web-inf下面寫一個ftldpt-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
<!-- 自動掃描 -->
<context:component-scan base-package="com.pacific.product.controller" />
<!-- 註解驅動 -->
<mvc:annotation-driven />
<!-- 總錯誤處理 -->
<bean id="customResolver"
class="com.pacific.product.custom.exception.CustomSimpleMappingExceptionResolver"></bean>
<!-- 資源管理 -->
<mvc:resources location="/resources/" mapping="/resources/**" />
<mvc:resources location="/upload/" mapping="/upload/**" />
<mvc:resources location="/source/" mapping="/source/**" />
<!-- 上傳檔案解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485670" /> <!-- 10M -->
</bean>
<!-- freemarker的配置 -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/view/" />
<property name="defaultEncoding" value="utf-8" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">10</prop>
<prop key="locale">zh_CN</prop>
<prop key="datetime_format">yyyy-MM-dd</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="number_format">#.##</prop>
</props>
</property>
</bean>
<!-- FreeMarker檢視解析 如返回student。。在這裡配置字尾名ftl和檢視解析器。。 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property>
<property name="suffix" value=".ftl" />
<property name="contentType" value="text/html;charset=utf-8" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
</bean>
</beans>
在spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
<!-- 自動掃描 -->
<context:component-scan base-package="com.pacific.product.controller" />
<!-- 註解驅動 -->
<mvc:annotation-driven />
<!-- 總錯誤處理 -->
<bean id="customResolver"
class="com.pacific.product.custom.exception.CustomSimpleMappingExceptionResolver"></bean>
<!-- 資源管理 -->
<mvc:resources location="/resources/" mapping="/resources/**" />
<mvc:resources location="/upload/" mapping="/upload/**" />
<mvc:resources location="/source/" mapping="/source/**" />
<!-- 上傳檔案解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485670" /> <!-- 10M -->
</bean>
<!-- 內部資源檢視解析器 prefix + logicName + suffix /WEB-INF/jsps/ + index + .jsp -->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 字首 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 字尾 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
上面兩個檔案分別定義了不同的模板引擎
第二步,在web.xml檔案配一下,何時使用哪一個引擎
在web.xml檔案中加入如下內容
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- freemark -->
<servlet>
<servlet-name>ftldpt</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- freemark mapping -->
<servlet-mapping>
<servlet-name>ftldpt</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
從上面的內容來來看,當以.do結尾的就用jsp引擎,當使用ftl結尾的仿問就用freemark來處理
測試如下:
@Controller
@RequestMapping("/freemark")
public class FreemarkTestController {
@RequestMapping("/index")
public String index(Model model){
model.addAttribute("userName","lin");
return "index";
}
}
上面是一個controller,當仿問index時,會通過不同的結尾呼叫不同的模板引擎
專案中儘量使用freemark這種靜態模板,如果頁面修改比較多時,每一次修改,jsp都會編譯一下比較慢,而freemark不會。另外使用freemark可以方便實現頁面靜態化,因為它的輸出不一定是servlet容器,而可以是file檔案。