域对象共享数据

域对象共享数据

框架搭建

web.xml

<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>

SpringMVC.xml


<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>

firstPage.html

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
    <title>首页title>
head>
<body>
首页
body>
html>

success.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

@Controller
public class SetFirstPage {
    @RequestMapping("/")
    public String testFirstPage(){
        return "firstPage";
    }
}

在这里插入图片描述

域对象共享数据

1、使用servletAPI

firstPage.html

<a th:href="@{/testRequestByServletAPI}">通过servletAPI向request域对象共享数据a>

ScopeControoler

@Controller
public class ScopeController {
    //使用servletAPI向request域对象共享数据
    @RequestMapping("/testRequestByServletAPI")
    public String testRequestByServletAPI(HttpServletRequest request){
        request.setAttribute("testRequestScope", "hello servletAPI");
        return "success";
    }
}

success.html


<p th:text="${testRequestScope}">p>

在这里插入图片描述

在这里插入图片描述

2、使用SpringMVC提供的API

①ModelAndView

firstPage.html
<a th:href="@{/testModelAndView}">通过ModelAndView向request域对象共享数据a>
ScopeController
@RequestMapping("/testModelAndView")
//使用ModelAndView返回值必须是ModelAndView
public ModelAndView testModelAndView(){
    ModelAndView mav = new ModelAndView();
    //处理模型数据,即向请求域request数据
    mav.addObject("testScope", "Hello ModelAndView");
    //设置视图名称,相当于平时返回success
    mav.setViewName("success");
    return mav;
}
success.html
<p th:text="${testScope}">p>

在这里插入图片描述


②Model

firstPage.html
<a th:href="@{/testModel}">通过Model向request域对象共享数据a>
ScopeController
@RequestMapping("/testModel")
public String testModel(Model model){
    model.addAttribute("testScope", "Hello model");
    return "success";
}

在这里插入图片描述


③map

firstPage.html
<a th:href="@{/testMap}">通过Map向request域对象共享数据a><br/>
ScopeController
@RequestMapping("/testMap")
public String testMap(Map map){
   map.put("testScope", "Hello Map");
   return "success";
}

在这里插入图片描述


④ModelMap

firstPage.html
<a th:href="@{/testModelMap}">通过ModelMap向request域对象共享数据a><br/>
ScopeController
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
    modelMap.addAttribute("testScope", "Hello ModelMap");
    return "success";
}

在这里插入图片描述


Model、ModelMap、Map之间的关系

ModelModelMapMap类型的参数其实本质上都是BindingAwareModelMap类型

public interface Model{}
public class ModelMap extendes LinkedHasMap<String, Object>{}
public clsss ExtendedModelMap extends ModeMap implements Model{}
public class BindingAwarModelMap extends ExtendedModelMap{}

3、session域共享数据

firstPage.html

<a th:href="@{/testSession}">通过servletApi向Session域对象共享数据a><br/>

ScopeController

@RequestMapping("/testSession")
public String testSession(HttpSession session){
    session.setAttribute("testSessionScope", "Hello session");
    return "success";
}

success.html


<p th:text="${session.testSessionScope}">p>

在这里插入图片描述


4、application域共享数据

firstPage.html

<a th:href="@{/testApplication}">通过servletApi向Application域对象共享数据a><br/>

ScopeController

@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
    ServletContext application = session.getServletContext();
    application.setAttribute("testApplicationScope", "Hello Application");
    return "success";
}

success.html

<p th:text="${application.testApplicationScope}">p>

在这里插入图片描述

你可能感兴趣的:(springMVC,java)