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">
<!-- 配置SpringMVC前端控制器DispatcherServlet--><servlet><servlet-name>SpringMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 设置SpringMVC配置文件的位置和名称--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param>
<!-- 将DispatcherServlet的初始化时间提前到服务器启动时--><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>SpringMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>
spring配置文件
<?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: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/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描控制层组件--><context:component-scan base-package="com.zmx.controller"></context:component-scan>
<!-- 配置Thymeleaf视图解析器--><bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"><property name="order" value="1"/><property name="characterEncoding" value="UTF-8"/><property name="templateEngine"><bean class="org.thymeleaf.spring5.SpringTemplateEngine"><property name="templateResolver"><bean class="org.thymeleaf.spring5.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></property></bean></property></bean>
</beans>
controller控制层
package com.zmx.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@Controller
//@RequestMapping(value = {"/"})
public class HelloController {//@GetMapping()//@PostMapping()//@DeleteMapping()//PutMapping()@RequestMapping("/")public String index(){//将逻辑视图返回return "index";}@RequestMapping(value = {"/hello","/world"},method = {RequestMethod.POST,RequestMethod.GET},params = {"username","!password","age=20","gender!=女"},headers = {"referer"})public String hello(){return "success";}
// ?任意单个字符
// /he?lo/world/ant
// *任意个数的任意字符
// /he*lo/world/ant
// **任意层数的任意目录,写在双斜线之间,两侧不能有任意字符
// /**/world/ant@RequestMapping(value = {"/he?lo/wo*ld/**/"})public String testAnt(){return "success";}@RequestMapping(value = {"hello/world/{username}/{password}"})public String testRest(@PathVariable("username") String username,@PathVariable("password") Integer password){System.out.println("username = " + username + ", password = " + password);return "success";}
}
view层
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>hello world!</h1>
<a th:href="@{/hello}">hello</a>
<a th:href="@{/world}">hello</a>
<form th:action="@{/hello?username=admin&age=20&gender='男'}" method="post"><input name="hello" type="submit" value="访问">
</form>
<a th:href="@{hello/world/ant}">ant</a>
<a th:href="@{hello/world/zmx/2213}">admin</a>
<!--<form th:action="@{/hello(username='admin'&age='20'&gender='男')}" method="post">-->
<!-- <input name="hello" type="submit" value="访问">-->
<!--</form>-->
</body>
</html>