动力节点旗下在线教育品牌  |  咨询热线:400-8080-105 学Java全栈,上蛙课网
首页 > 文章

Spring MVC配置HTML视图渲染器过程

08-31 17:50 109浏览
举报 T字号
  • 大字
  • 中字
  • 小字

我们所接触的大多数Spring MVC开发的项目总是将页面映射到一个.JSP文件上,但相对于HTML语言来说JSP不太友好。前端开发人员将开发好的html模板交给后端人员开发,后端人员要先将html页面转化为jsp页面,此过程容易出错。然后就是jsp页面发布后,解析器要又要将jsp页面转化为html页面,造成效率不高。所以本文就把页面渲染方式改成了.HTML谈一谈Spring MVC配置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配置HTML视图渲染器的配置情况,如果需要用到的小伙伴可以牢记。当然,你也可以随时在本站的Spring MVC视频教程中查看相关配置,随用随取哦。

0人推荐
共同学习,写下你的评论
0条评论
天鹏桑
程序员天鹏桑

12篇文章贡献57547字

作者相关文章更多>

推荐相关文章更多>

DOM渲染的详细过程

QCode09-04 14:38

CSS水平和垂直居中技巧大梳理

Code大师09-04 14:50

mui的input框在IOS系统下无法聚焦或点击多次才能聚焦

不写代码你养我啊08-23 11:14

推荐的-视.频播放器以及在线客服

不写代码你养我啊09-17 18:02

谈谈java多线程的三大特性

要学习了06-18 18:13

发评论

举报

0/150

取消