springmvc 整合 thymeleaf

2021-06-05 21:04

阅读:653

标签:scan   param   default   not   servlet   drive   pre   coding   startup   

添加依赖

dependency>
    groupId>org.springframeworkgroupId>
    artifactId>spring-webmvcartifactId>
    version>5.3.5version>
dependency>
dependency>
     groupId>org.thymeleafgroupId>
     artifactId>thymeleafartifactId>
     version>3.0.12.RELEASEversion>
dependency>
dependency>
     groupId>org.thymeleafgroupId>
     artifactId>thymeleaf-spring5artifactId>
     version>3.0.12.RELEASEversion>
dependency>

web.xml

xml version="1.0" encoding="UTF-8"?>
web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    servlet>
        servlet-name>servlet-mvcservlet-name>
        servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        init-param>
            param-name>contextConfigLocationparam-name>
            param-value>classpath:applicationContext.xmlparam-value>
        init-param>
        load-on-startup>1load-on-startup>
    servlet>

    servlet-mapping>
        servlet-name>servlet-mvcservlet-name>
        url-pattern>/url-pattern>
    servlet-mapping>

    filter>
        filter-name>characterEncodingfilter-name>
        filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        init-param>
            param-name>encodingparam-name>
            param-value>UTF-8param-value>
        init-param>
    filter>

    filter-mapping>
        filter-name>characterEncodingfilter-name>
        url-pattern>/*url-pattern>
    filter-mapping>

    session-config>
        session-timeout>30session-timeout>
    session-config>
web-app>

 

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: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.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">

    
    mvc:annotation-driven/>
    
    mvc:default-servlet-handler/>
    
    context:component-scan base-package="com.chao"/>

    bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
        property name="prefix" value="/WEB-INF/views/" />
        property name="suffix" value=".html" />
        property name="templateMode" value="HTML5" />
        property name="cacheable" value="false" /> 
        property name="characterEncoding" value="UTF-8" />
    bean>

    bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
        property name="templateResolver" ref="templateResolver" />
    bean>

    bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        property name="templateEngine" ref="templateEngine" />
        property name="characterEncoding" value="UTF-8" />
    bean>

beans>

 

springmvc 整合 thymeleaf

标签:scan   param   default   not   servlet   drive   pre   coding   startup   

原文地址:https://www.cnblogs.com/rchao/p/14619901.html


评论


亲,登录后才可以留言!