SSM笔记-SpringMVC的处理模型数据

1、模型数据的ModelAndView
①返回值和返回类型是ModelAndView及对象,如果返回的是String的话,request域对象会拿不到ModelAndView的model的数据
②ModelAndView中可以包含视图和模型信息
③SpringMVC会把ModelAndView的model数据放到request域对象中
④注意:初始化ModelAndView的时候需要把name写上(即需要返回的页面名字) 如:ModelAndView modelAndView = new ModelAndView(name);
⑤SpringMVC支持为目标方法添加Map类型参数(可以是Model类型或者ModelMap类型)

2、模型数据的SessionAttributes
①可以通过属性名指定需要放到回话中的属性(SessionAttributes的value参数的属性值)
②可以通过模型属性的对象类型指定那些模型属性需要放到回话中(SessionAttributes的types参数的属性值)
③注意:@SessionAttributes注解只能放到类上面,只能修饰类
④添加@SessionAttributes注解之后,当程序为域对象赋值的时候会同时会为Session赋值

3、@ModelAttribute
运行顺序
①执行@ModelAttribute修饰的方法,把初始的或者数据库取出的对象放到Map中)
②SpringMVC从Map中取出数据,并把表单请求参数赋值给该对象对应值
③SpringMVC把上面的对象传入目标方法的参数中
④@ModelAttribute修饰的方法中,放入到Map时的键,如果没有特别使用@ModelAttribute修的的话,会和目标方法入参类型的小写首字母的字符串一致
⑤使用@SessionAttributes的时候,要不需要有@ModelAttribute修饰的方法,要不就需要在目标方法的入参里面用@ModelAttribute修饰入参,让隐含的模型对象中存在@SessionAttributes需要的key
⑥调用逻辑顺序

1、Spring MVC 在调用处理方法之前,在请求线程中自动的创建一个隐含的模型对象
2、判断隐含的模型对象是否已经有 @ModelAttribute的属性值

①如果有:将 @ModelAttribute的属性值赋值到入参(定义的dao对象属性)的相应属性,并返回
②如果没有:不进行后面的步骤
3、判断调用的方法所在的标注了@Controller类中,有没有@SessionAttributes修饰
①如果有:查看Session中是否存在@SessionAttributes的属性值,并且把该属性值添加到隐含的模型对象中,如果该属性值已经存在于隐含的模型对象中,则原来的属性值会被覆盖,如果不存在该属性值,则抛出异常 HttpSessionRequiredException
②如果没有:继续下一步
3-1、通过反射创建POJO类型的参数,并将其赋值给入参(定义的dao对象属性),并将用户的请求消息 赋值给入参的相应属性
4、把key和value存入隐含的模型对象,在保存到request中
5、SpringMVC调用每个方法前,都会调用@ModelAttribute标记的方法

4、再次提醒 注意事项:
①当handler方法使用ModelAndView对象时,返回值是String的时候,request域对象会拿不到ModelAndView的model的数据
②初始化ModelAndView的时候需要把name写上
③SpringMVC支持为目标方法添加Map类型参数
④@SessionAttributes注解只能放到类上面,只能修饰类
⑤添加@SessionAttributes注解之后,当程序为域对象赋值的时候会同时会Session赋值
⑥@ModelAttribute修饰的方法中,放入到Map时的键,如果没有特别使用@ModelAttribute修的的话,会和目标方法入参类型的小写首字母的字符串一致
⑦使用@SessionAttributes的时候,要不需要有@ModelAttribute修饰的方法,要不就需要在目标方法的入参里面用@ModelAttribute修饰入参,让隐含的模型对象中存在@SessionAttributes需要的key

5、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>SpringMVC_5_Model</display-name>
  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

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

    <!-- 配置自动扫描包 -->
    <context:component-scan base-package="com.test.springmvc"></context:component-scan>

    <!-- 配置视图解析器,把handler返回值解析为实际视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

7、Handler.java

package com.test.springmvc.handlers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.test.springmvc.dao.Info;

@SessionAttributes(value={"mapKey1","mapKey2"},types=(String.class))
@RequestMapping("/test")
@Controller
public class Handler {

    //测试模型数据的ModelAndView
    //返回值和返回类型是ModelAndView及对象,如果返回的是String的话,request域对象会拿不到ModelAndView的model的数据
    //其中可以包含视图和模型信息
    //SpringMVC会把ModelAndView的model数据放到request域对象中
    //注意:初始化ModelAndView的时候需要把name写上(即需要返回的页面名字)
    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        String name = "result";
        ModelAndView modelAndView = new ModelAndView(name);
        modelAndView.addObject("oName", "1");
        modelAndView.addObject("oId", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        return modelAndView;
    }

    //测试模型数据的Map
    //SpringMVC支持为目标方法添加Map类型参数(可以是Model类型或者ModelMap类型)
    @RequestMapping("/testMap")
    public String testMap(Map<String, String>map){
        map.put("mapKey", "mapValue");
        return "result";
    }

    //测试模型数据的SessionAttributes
    //可以通过属性名指定需要放到回话中的属性(SessionAttributes的value参数的属性值)
    //可以通过模型属性的对象类型指定那些模型属性需要放到回话中(SessionAttributes的types参数的属性值)
    //注意:@SessionAttributes注解只能放到类上面,只能修饰类
    //添加@SessionAttributes注解之后,当程序为域对象赋值的时候会同时会Session赋值
    @RequestMapping("/testSessionAttributes")
    public String testSessionAttributes(Map<String,Object>map){
        Info info = new Info();
        info.setInfoId(1);
        info.setInfoName("name");
        info.setInfoDesc("description");
        map.put("mapKey1", info);
        map.put("mapKey2","mapvalue2");
        return "result";
    }
}

8、Handler2.java

package com.test.springmvc.handlers;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.test.springmvc.dao.Info;

@SessionAttributes(value={"InfoN"},types={String.class})
@RequestMapping("/test2")
@Controller
public class Handler2 {

    //测试@ModelAttribute
    //运行顺序
    //1、执行@ModelAttribute修饰的方法,把初始的或者数据库取出的对象放到Map中)
    //2、SpringMVC从Map中取出数据,并把表单请求参数赋值给该对象对应值
    //3、SpringMVC把上面的对象传入目标方法的参数中

    //注意:
    //1、@ModelAttribute修饰的方法中,放入到Map时的键,如果没有特别使用@ModelAttribute修的的话,会和目标方法入参类型的小写首字母的字符串一致
    //2、使用@SessionAttributes的时候,要不需要有@ModelAttribute修饰的方法,要不就需要在目标方法的入参里面用@ModelAttribute修饰入参,让隐含的模型对象中存在@SessionAttributes需要的key

    //调用逻辑顺序
    //1、Spring MVC 在调用处理方法之前,在请求线程中自动的创建一个隐含的模型对象
    //2、判断隐含的模型对象是否已经有 @ModelAttribute的属性值
    //  ①如果有:将 @ModelAttribute的属性值赋值到入参(定义的dao对象属性)的相应属性,并返回
    //  ②如果没有:不进行后面的步骤
    //3、判断调用的方法所在的标注了@Controller类中,有没有@SessionAttributes修饰
    //  ①如果有:查看Session中是否存在@SessionAttributes的属性值,并且把该属性值添加到隐含的模型对象中,如果该属性值已经存在于隐含的模型对象中,则原来的属性值会被覆盖,如果不存在该属性值,则抛出异常 HttpSessionRequiredException
    //  ②如果没有:继续下一步
    //3-1、通过反射创建POJO类型的参数,并将其赋值给入参(定义的dao对象属性),并将用户的请求消息 赋值给入参的相应属性 
    //4、把key和value存入隐含的模型对象,在保存到request中

    //SpringMVC调用每个方法前,都会调用@ModelAttribute标记的方法
    @ModelAttribute
    public void getInfoFromDB(@RequestParam(value="id",required=false) Integer id,Map<String, Object>map){
        System.out.println("id:"+id);
        Info info = new Info(1, "name", "desc");
        map.put("InfoN", info);
    }

    @RequestMapping("/testModelAttributes")
    //这里面使用@ModelAttribute("InfoN"),testModelAttributes会先从request中查找有没有key为InfoN的数据,如果有则直接把InfoN的值作为目标方法testModelAttributes的入参
    //如果request中没有key为InfoN的数据,则通过反射创建一个POJO类型的数据Info
    public String testModelAttributes(@ModelAttribute("InfoN") Info info){
        System.out.println("testModelAttributes Info:"+info);
        return "result";
    }
}

9、Info.java

package com.test.springmvc.dao;

public class Info {

    Integer infoId;
    String infoName;
    String infoDesc;

    public Info(){}

    public Info(Integer infoId,String infoName,String infoDesc){
        System.out.println("infoId:"+infoId+",infoName:"+infoName+",infoDesc:"+infoDesc);
    }

    public Integer getInfoId() {
        return infoId;
    }
    public void setInfoId(Integer infoId) {
        this.infoId = infoId;
    }
    public String getInfoName() {
        return infoName;
    }
    public void setInfoName(String infoName) {
        this.infoName = infoName;
    }
    public String getInfoDesc() {
        return infoDesc;
    }
    public void setInfoDesc(String infoDesc) {
        this.infoDesc = infoDesc;
    }

    @Override
    public String toString() {
        return "Info [infoId=" + infoId + ", infoName=" + infoName + ", infoDesc=" + infoDesc
                + "]";
    }
}

10、index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <a href="test/testModelAndView">testModelAndView</a>
    <br><br>
    <a href="test/testMap">testMap</a>
    <br><br>
    <a href="test/testSessionAttributes">testSessionAttributes</a>
    <br><br>

    <form action="test2/testModelAttributes" method="post">
        <input type="text" name="infoId" value="${info.infoId}">
        <input type="text" name="infoName" value="${info.infoName}">
        <input type="text" name="infoDesc" value="${info.infoDesc}">
        <input type="submit" name="submit" value="submit">
    </form>

</body>
</html>

11、result.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
result page<br>

oid : ${oId}<br>
oName : ${oName}<br>

map : ${mapKey}<br>
mapKey1 : ${requestScope.mapKey1},${sessionScope.mapKey1}<br>
mapKey2 : ${requestScope.mapKey2},${sessionScope.mapKey2}<br>

Handler2<br>
infoId:${info.infoId}<br>
infoName:${info.infoName}<br>
infoDesc:${info.infoDesc}<br>


</body>
</html>

12、项目目录
项目目录

13、demo
https://download.csdn.net/download/qq_22778717/10599114

猜你喜欢

转载自blog.csdn.net/qq_22778717/article/details/81608710