springmvc4 + thymeleaf

该项目环境和插件为:

spring4

thymeleaf

tomcat7

jdk8

该项目参考路径为https://cloud.tencent.com/developer/article/1332635

第一步:

  创建maven项目,具体文件结构如下

第二步:

  配置pom.xml文件,具体代码如下

  

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.spring.thymeleaf</groupId>
  <artifactId>thymeleafSpringmvc</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>thymeleafSpringmvc Maven Webapp</name>
  <url>http://maven.apache.org</url>


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.12</version>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.1.1</version>
      <scope>provided</scope>
    </dependency>

    <!--实现slf4j接口并整合-->
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.1.1</version>
      <scope>provided</scope>
    </dependency>


    <!--4:spring依赖-->
    <!--1)spring核心依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.14.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.14.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.14.RELEASE</version>
    </dependency>

    <!--3)springweb相关依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.14.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.14.RELEASE</version>
    </dependency>

     <!--Aop要导入的包-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.9</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20170516</version>
    </dependency>

     <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring4</artifactId>
      <version>3.0.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
      <version>2.0.1.Final</version>
    </dependency>

  </dependencies>


  <build>
    <finalName>thymeleafSpringmvc</finalName>
    <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
            </plugin>
      </plugins>
  </build>
</project>

第三步:

  配置spring-mvc.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.3.xsd">

        <!-- 开启注解,扫描 -->
        <context:annotation-config></context:annotation-config>

        <!-- 扫描的路径(controller) -->
        <context:component-scan base-package="com.spring.controller"></context:component-scan>

        <!-- 过滤掉js,jpg,png,css等静态文件 -->
        <mvc:default-servlet-handler/>

        <!-- 开启mvc -->
        <mvc:annotation-driven />

        <!--viewResolver-->
	    <bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
	        <property name="order" value="1"/>
	        <property name="characterEncoding" value="UTF-8"/>
	        <property name="templateEngine" ref="templateEngine"/>
	    </bean>

	    <!-- templateEngine -->
	    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
	        <property name="templateResolver" ref="templateResolver"/>
	    </bean>

	    <!-- templateResolver -->
	    <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
	        <property name="prefix" value="/WEB-INF/templates/"/>
	        <property name="suffix" value=".html"/>
	        <property name="templateMode" value="HTML5"/>
	        <property name="characterEncoding" value="UTF-8"/>
	    </bean>


</beans>

第四步:

  配置web.xml,具体代码如下:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!-- 读取配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mvc.xml</param-value>
  </context-param>

  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

   <listener>
       <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
   </listener>

  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <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>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


</web-app>

第五步:

  编辑HelloController.java,具体代码如下:

package com.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class HelloController {

	@RequestMapping("/index")
	public String index(Model model) {
		model.addAttribute("message", "hello spring thymeleaf");

		return "index";
	}

}

第六步:

  编辑index.html文件,具体代码如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>hello this is index.html</h1>
    <span th:text="${message}"></span>
</body>
</html>

第七步:

  启动tomcat,此处用的是内置tomcat,

  启动方式为:

在pom.xml文件中=》右键=》run(R)=》maven build=》输入指令:tomcat7:run=》点击运行

 

第八步:

  在浏览器中输入url进行访问

http://localhost:8080/thymeleafSpringmvc/index

此处的thymeleafSpringmvc为你的项目名

效果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Spring MVC和Thymeleaf的时候,除了需要添加Thymeleaf的主依赖之外,还需要添加thymeleaf-spring4依赖。如果没有添加thymeleaf-spring4依赖,可能会报错org.thymeleaf.spring4.view.ThymeleafViewResolver,表示找不到Thymeleaf解析器。因此,thymeleaf-spring4也是必不可少的。\[1\] 对于Maven项目,可以在pom.xml文件中添加以下依赖配置: ``` <thymeleaf.version>2.1.3.RELEASE</thymeleaf.version> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>${thymeleaf.version}</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring3</artifactId> <version>${thymeleaf.version}</version> </dependency> ``` 或者可以使用以下依赖配置: ``` <!-- thymeleaf --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.11.RELEASE</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.11.RELEASE</version> </dependency> ``` 这样就可以设置Thymeleaf解析器了。\[2\]\[3\] #### 引用[.reference_title] - *1* *3* [【springmvc thymeleafspringmvc整合thymeleaf](https://blog.csdn.net/qq_41929184/article/details/105244034)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringMVC整合Thymeleaf模板](https://blog.csdn.net/hustwht/article/details/79129682)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值