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

SpringMVC配置html视图渲染器

2017-04-01 13:30 316 查看
以前的项目总是将页面映射到一个.jsp文件上,个人做了很久前端之后,看到jsp总感觉不友好,所以就把页面渲染方式改成了.html

html视图渲染器的核心是freemarker

1.首先配置web.xml (不属于这次配置的部分已经删除)

<!--引入外部的spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/config/applicationContext.xml</param-value>
</context-param>
<!--配置servlet加载spring的配置和辅助的spring配置文件-->
<!--过滤路径是/表示所有/的路径都过滤,后缀随意都接受-->
<servlet>
<servlet-name>nbBaseServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/config/nbBaseServlet-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>nbBaseServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


2.applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> 
<!-- freemarker html渲染器配置,搜索/WEB-INF/view下的.html进行匹配 -->

<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/views/" />
<property name="freemarkerSettings">
<props>
<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>
</props>
</property>
</bean>

</beans>


3.nbBaseServlet-servlet.xml配置(不属于这次配置的部分已经删除)

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 加载Spring的全局配置文件 -->
<beans:import resource="applicationContext.xml" />

<!-- SpringMVC静态文件配置 -->
<mvc:resources location="/themes/" mapping="/themes/**"></mvc:resources>

<!-- 配置SpringMVC的视图渲染器, 让其前缀为:/ 后缀为.html  将视图渲染到/view/<method返回值>.html -->

<beans:bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"></beans:bean>
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<beans:property name="exposeRequestAttributes" value="true" />
<beans:property name="exposeSessionAttributes" value="true" />
<beans:property name="viewClass">
<beans:value>org.springframework.web.servlet.view.freemarker.FreeMarkerView</beans:value>
</beans:property>
<beans:property name="cache">
<beans:value>true</beans:value>
</beans:property>
<beans:property name="suffix">
<beans:value>.html</beans:value>
</beans:property>
<beans:property name="contentType">
<beans:value>text/html; charset=UTF-8</beans:value>
</beans:property>
</beans:bean>

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