修改pom.xml引入Thymeleaf相关包:

<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>xuan</groupId><artifactId>springmvc</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>springmvc Maven Webapp</name><url>http://maven.apache.org</url><properties><spring.version>4.1.3.RELEASE</spring.version><thymeleaf.version>2.1.2.RELEASE</thymeleaf.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring4</artifactId><version>${thymeleaf.version}</version></dependency><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf</artifactId><version>${thymeleaf.version}</version></dependency></dependencies><build><finalName>springmvc</finalName></build>
</project>

修改springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"><!-- 激活@Controller模式 --><mvc:annotation-driven /><!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 --><context:component-scanbase-package="org.xuan.springmvc.controller" /><mvc:resources location="/static/" mapping="/static/**" /><!-- 模板解析器 --><bean id="templateResolver"class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"><property name="prefix" value="/WEB-INF/templates/" /><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.spring4.SpringTemplateEngine"><property name="templateResolver" ref="templateResolver" /></bean><bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver"><property name="templateEngine" ref="templateEngine" /><property name="characterEncoding" value="UTF-8" /></bean></beans>

在WEB-INF下面新建templates目录,在templates下面新建hello.html:

<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<script type="text/javascript" src="static/js/jquery-1.10.2.min.js"th:src="@{/static/js/jquery-1.10.2.min.js}"></script><script th:inline="javascript">$(function(){var _ctx = [[${application.ctx}]];alert("Project ContextPath:"+_ctx);alert("路径:"+$("#ctx").val());});
</script>
<title>Spring MVC + Thymeleaf Example</title>
</head>
<body><!-- Project ContextPath --><input type="hidden" id="ctx" th:value="${application.ctx}" /> Hello,<span th:text="${name}" />!<br /> Hello,<span th:text="${query}" />!<br /> Hello,<span th:text="${submit}" />!<br /><a th:href="@{/query?name=a_href}"> query</a><br /><form th:action="@{/submit}"><input type="text" name="name" /><input type="text" name="age" /><button type="submit">submit</button></form></body>
</html>

修改MainController.java:

package org.xuan.springmvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;@Controller
public class MainController {@RequestMapping(value = "/{name}", method = RequestMethod.GET)public String getMovie(@PathVariable String name, ModelMap model) {model.addAttribute("name", name);model.addAttribute("query", "");model.addAttribute("submit", "");return "hello";}@RequestMapping(value = "/query", method = RequestMethod.GET)public String query(@RequestParam("name") String name, ModelMap model) {model.addAttribute("name", "");model.addAttribute("query", name);model.addAttribute("submit", "");return "hello";}@RequestMapping(value = "/submit", method = RequestMethod.GET)public String submit(@RequestParam("name") String name, @RequestParam("age") String age, ModelMap model) {model.addAttribute("name", "");model.addAttribute("query", "");model.addAttribute("submit", name + age);return "hello";}
}

整个项目的目录结构如下:

运行效果:

1

2

3

转载于:https://www.cnblogs.com/grasp/p/9024566.html

eclipse springmvc+Thymeleaf相关推荐

  1. SpringMVC+Thymeleaf +HTML的简单框架

    SpringMVC+Thymeleaf +HTML的简单框架 一.问题 项目中需要公众号开发,移动端使用的是H5,但是如果不用前端框架的话,只能考虑JS前端用ajax解析JSON字符串了.今天我们就简 ...

  2. SpringMVC+thymeleaf

    这个也是配置了好久,对Spring也是似懂非懂,虽然可以简单的搭建出来项目,但是很多东西确实还是不太理解 但是最近公司的项目上是利用的SpringBoot这个框架,都是Spring啊,所以我自己练习的 ...

  3. SpringMVC+Thymeleaf如何处理URL中的动态查询参数

    1.使用一个Map<String, String>接收数据 When you request a Map annotated with @RequestParam Spring creat ...

  4. SpringMVC集成Thymeleaf

    一.项目配置 以前在SpringBoot中使用过Thymeleaf,感觉非常好用,可是现在准备做一个ssm的项目,里面需要集成一个前端模版引擎.为什么选择Thymeleaf,他有以下好处 Thymel ...

  5. Spring MVC + Thymeleaf

    参考网址: https://www.cnblogs.com/litblank/p/7988689.html 一.简介 1.Thymeleaf 在有网络和无网络的环境下皆可运行,而且完全不需启动WEB应 ...

  6. JAVA入门[22]—thymeleaf

    一.thymeleaf官网 官网:https://www.thymeleaf.org/index.html doc:https://www.thymeleaf.org/documentation.ht ...

  7. thymeleaf 学习笔记-基础篇(中文教程)

    转自: http://www.cnblogs.com/vinphy/p/4674247.html (一)Thymeleaf 是个什么? 简单说, Thymeleaf 是一个跟 Velocity.Fre ...

  8. 前端js获取SpringMvc后台model中传值

    也许你迷茫,但是我想说,在你迷茫的同时,保持本心,过好今天就好. 使用 SpringBoot +SpringMVC +thymeleaf 组合实现的功能,期望在 thymeleaf 中的html中的j ...

  9. Thymeleaf 是个什么?

    thymeleaf 空值的处理:<!--th:value="${result?.data?.menuName}"--> 一)Thymeleaf 是个什么? 简单说, T ...

最新文章

  1. Windows Phone开发者注册秘籍
  2. 一个还不错的源码解析网站
  3. neo4j 连接java
  4. Python中json用法【详解】_Python系列学习笔记
  5. 学完计算机的感想300,计算机实训总结计算机实训心得300
  6. 服务器显示AL018是什么意思,IIS服务器80端口却已被占用的问题
  7. 如何查看文件夹里有几张图片_如何把几张图片合成一个pdf?图片合并为pdf的操作教程...
  8. python开发wince软件_Wince6.0应用开发:二、模拟器的使用
  9. 别瞎忙活:创业公司的6条时间管理策略
  10. LeetCode 1430. 判断给定的序列是否是二叉树从根到叶的路径(递归)
  11. 二分查找以及数组下标的移动规律
  12. Java面试官最爱问的垃圾回收机制,Java编程配置思路详解
  13. php 递归太多报错,php – javascript太多的递归?
  14. 2017-5-5/PHP实现负载均衡的加权轮询
  15. 一位资深程序员大牛给予Java的学习路线建议
  16. python yield详解
  17. Excel图表制作(一):商务图表之甘特图
  18. Java程序员必读精选书籍分享,强烈推荐
  19. 为什么计算机是32位64位,64位是x86还是x64_为什么64位是X64,32位是X86?
  20. input 无法输入

热门文章

  1. Gym 101206L Daylight Saving Time 根据年月日计算星期
  2. TypeScript 中的 SOLID 原则
  3. netstat监控大量ESTABLISHED连接与Time_Wait连接问题
  4. 23种设计模式类图总结
  5. 一个广为人知但鲜有人用的技巧:对象池
  6. GDB调试精粹及使用实例来源-转
  7. 蓝桥杯 ADV-189 算法提高 连接乘积
  8. [swift] LeetCode 20. Valid Parentheses
  9. 蓝桥杯 BASIC-6 基础练习 杨辉三角形
  10. 如何:删除Word 2010中的“向下箭头”