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

【java】intellij idea SpringMVC 配置FreeMarker模板引擎

2017-05-05 17:12 465 查看
要点
理解SpringMVC的配置

理解Spring-mvcxml配置文件

配置步骤总结

关键截图

要点

理解SpringMVC的配置

如下代码来自web.xml

<!-- Spring MVC 核心配置开始 -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 可以通过contextConfigLocation来自定义SpringMVC配置文件的位置,如不指定,则默认在WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,
此时文件名必须为[<servlet-name>]-servlet.xml,否则会出错-->
<!--以下init-param是自定义SpringMVC的配置文件的位置 -->
<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>

<!-- 拦截设置 -->
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring MVC 核心配置结束 -->


意思是spring mvc会拦截所有的请求,并把请求交给dispathServlet来处理,而spring-mvc.xml就是另外一个核心配置文件

理解Spring-mvc.xml配置文件

注意,这个spring-mvc.xml文件的名字是开发者自定义的,你也可以叫spring-servlet.xml,甚至ada.xml,需要抓住的重点就是这段代码,它是spring mvc 配置的核心

<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>


Spring mvc配置文件

<?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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 
<!-- 省略无关配置-->

<!-- Freemarker配置 -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/view/" />

<property name="freemarkerVariables"><!--设置一些常用的全局变量-->
<map>
<entry key="webRoot" value="${webRoot}"></entry>
<entry key="jsRoot" value="${jsRoot}"></entry>
</map>
</property>

<property name="freemarkerSettings">
<props>
<!--检查模板更新的时间间隔,默认5s-->
<prop key="template_update_delay">0</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="number_format">0.##########</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="classic_compatible">true</prop>
<prop key="template_exception_handler">ignore</prop>
<prop key="locale">zh_CN</prop>
</props>
</property>
</bean>

<!-- 配置freeMarker视图解析器 -->
<bean id="viewResolverFtl" 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>

<!--JSP视图解析器-->
<bean id="viewResolverJsp" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"/>
<property name="order" value="1"/>
</bean>

<!-- 省略无关配置-->
</beans>


这里需要注意一下:

id为viewResolverFtl的bean是不能带prefix否则会出现“Could not resolve view with name ‘hello’ in servlet with name ‘mvc-dispatche……”的错误

参考:问题原因是因为freemarker本身配置了templateLoaderPath而在viewResolver中不需要配置prefix,且路径前缀必须配置在templateLoaderPath中

配置步骤总结

(1) 在工程中添加maven依赖

具体版本可以根据情况改变

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>


(2) 配置Spring MVC的核心配置文件

(3) 在controll控制所需返回的view视图(ftl文件)

(4) 在web-inf 下创建相关的view视图

其实重点还是在spring-mvc.xml 和web.xml 几个文件的配置上面

关键截图

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