<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">
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceResponseEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>springMVCservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:SpringMVC.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springMVCservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
<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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:component-scan base-package="com.louis.controller">context:component-scan>
<bean id="ThymeleafViewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<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>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
<title>首页title>
head>
<body>
首页
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
<title>Titletitle>
head>
<body>
success
body>
html>
@Controller
public class SetFirstPage {
@RequestMapping("/")
public String testFirstPage(){
return "firstPage";
}
}
<a th:href="@{/testRequestByServletAPI}">通过servletAPI向request域对象共享数据a>
@Controller
public class ScopeController {
//使用servletAPI向request域对象共享数据
@RequestMapping("/testRequestByServletAPI")
public String testRequestByServletAPI(HttpServletRequest request){
request.setAttribute("testRequestScope", "hello servletAPI");
return "success";
}
}
<p th:text="${testRequestScope}">p>
<a th:href="@{/testModelAndView}">通过ModelAndView向request域对象共享数据a>
@RequestMapping("/testModelAndView")
//使用ModelAndView返回值必须是ModelAndView
public ModelAndView testModelAndView(){
ModelAndView mav = new ModelAndView();
//处理模型数据,即向请求域request数据
mav.addObject("testScope", "Hello ModelAndView");
//设置视图名称,相当于平时返回success
mav.setViewName("success");
return mav;
}
<p th:text="${testScope}">p>
<a th:href="@{/testModel}">通过Model向request域对象共享数据a>
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "Hello model");
return "success";
}
<a th:href="@{/testMap}">通过Map向request域对象共享数据a><br/>
@RequestMapping("/testMap")
public String testMap(Map map){
map.put("testScope", "Hello Map");
return "success";
}
<a th:href="@{/testModelMap}">通过ModelMap向request域对象共享数据a><br/>
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testScope", "Hello ModelMap");
return "success";
}
Model
、ModelMap
、Map
类型的参数其实本质上都是BindingAwareModelMap
类型
public interface Model{}
public class ModelMap extendes LinkedHasMap<String, Object>{}
public clsss ExtendedModelMap extends ModeMap implements Model{}
public class BindingAwarModelMap extends ExtendedModelMap{}
<a th:href="@{/testSession}">通过servletApi向Session域对象共享数据a><br/>
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "Hello session");
return "success";
}
<p th:text="${session.testSessionScope}">p>
<a th:href="@{/testApplication}">通过servletApi向Application域对象共享数据a><br/>
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("testApplicationScope", "Hello Application");
return "success";
}
<p th:text="${application.testApplicationScope}">p>