1:pom.xml文件中添加如下配置引入jar包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.22</version> </dependency>
2:
方法一(这个方法是参考网上的,发现用在自己项目会报错):
spring的spring-config.xml中配置(需要注意的是InternalResourceViewResolver优先级最高 所以优先级设置低一些)
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/" /> <property name="suffix" value=".jsp" /> <property name="order" value="1"></property> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> <!-- 配置freeMarker视图解析器(可解析HTML) --> <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="exposeRequestAttributes" value="true" /> <property name="exposeSessionAttributes" value="true" /> <property name="exposeSpringMacroHelpers" value="true" /> <property name="cache" value="true" /> <property name="suffix" value=".html" /> <property name="order" value="0"/> </bean> <!-- 配置freeMarker的模板路径 --> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/page/"/> <property name="defaultEncoding" value="UTF-8"/> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">3600</prop> <prop key="locale">zh_CN</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="date_format">yyyy-MM-dd</prop> <prop key="number_format">#.##</prop> </props> </property> </bean>
方法二:
spring的spring-config.xml中配置(需要注意的是InternalResourceViewResolver优先级最高 所以优先级设置低一些)
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 使用自定义 JSP 视图解析器 --> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> <property name="order" value="1"/> </bean>
<!-- 配置freeMarker的模板路径 --> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- 指定html页面的路径,html文件都放到/view/pages的包下面 --> <property name="templateLoaderPath" value="/view/pages/"/> <property name="defaultEncoding" value="UTF-8"/> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">3600</prop> <prop key="locale">zh_CN</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="date_format">yyyy-MM-dd</prop> <prop key="number_format">#.##</prop> </props> </property> </bean> <!--html解析器,freeMarkerviewResolver需要配置上面freemarkerConfig--> <bean id="htmlviewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="suffix" value=".html"/> <property name="order" value="0"/> <property name="contentType" value="text/html;charset=UTF-8"/> </bean>