首页 > 其他 > 详细

4.ModelAttribute(模型映射)

时间:2020-12-07 22:08:17      阅读:39      评论:0      收藏:0      [点我收藏+]

目的:在页面跳跃后,文本框里的自动获取数据。

 

方法1:

原生态写法

 

@Controller
public class BookController {
    
    @RequestMapping(value="/book",method=RequestMethod.GET)
    public ModelAndView method1(HttpServletRequest request){
        request.setAttribute("title", "springMVC");
        request.setAttribute("author", "Apache");
        return new ModelAndView("/comm/book");
    }
    

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="root" value="${pageContext.request.contextPath}"></c:set>
<!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>hello</title>
</head>
<body>
    <form action="${root}/book" method="post">
        书名<input type="text" name="title" value="${requestScope.title}">
        作者<input type="text" name="author" value="${requestScope.author}">
        <input type="submit" value="book">
    </form>
</body>
</html>

结果:

技术分享图片

 

方法2:

@RequestMapping(value="/book",method=RequestMethod.GET)
    public ModelAndView method1(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("title","springMvc");
        mv.addObject("author","Apache");
        mv.setViewName("/comm/book");
        return  mv;
    }
    

当有第二个方法初始化的数据一样的时候:

mv.addObject("title","springMvc");
mv.addObject("author","Apache");



那可以写代码
//每次请求前调用
@ModelAttribute
public void method0{
     model.addAttribute("title","springMVC");
     model.addAttribute("author","Apache");
}
@RequestMapping(value="/book",method=RequestMethod.GET)
    public ModelAndView method1(){
       return new ModelAndView("/comm/book);
    }
@RequestMapping(value="/book1",method=RequestMethod.GET)
    public ModelAndView method2(){
       return new ModelAndView("/comm/book);
    }

当初始化的值是引用类型的时候:

//初始化方式1
@RequestMapping(value="/book1",method=RequestMethod.GET) public ModelAndView method1(Model model){ Book book=new Book("SpringMVC","Apache"); model.addAttribute("book",book); }

//初始化方式2
@RequestMapping(value="/book1",method=RequestMethod.GET)
    public Book method2(Model model){
       return new Book("springMVC","Apache");
    }
 

 

重难点:模型映射

//共同的接口 Map
org.springframework.ui.Model; -> 接口 注值
org.springframework.ui.ModelMap; -> 类 注值

//在注值的处理上是同步的
org.springframework.web.servlet.ModelAndView -> 类 (注值并转发)

//Model接口对象可以转换为ModelMap
Model model -> model.asMap(); -> ModelMap

//ModelAndView对象可以转换为ModelMap
ModelAndView -> mv.getModelMap() -> ModelMap

理论:
@ModelAttribute
用法:
1.初始化Model
@ModelAttribute
public void method0(Model model){
System.out.println("@ModelAttribute");
//对象映射
Book book = new Book("SpringMVC", "Apache");
model.addAttribute("book", book);
System.out.println("--------------------------------");
}
2.初始化特定数据
@ModelAttribute(name="book")
public Book method0(){
System.out.println("@ModelAttribute");
System.out.println("--------------------------------");
return new Book("SpringMVC", "Apache");
}
3.初始化对象参数 (可省略)
@ModelAttribute Book book2 -> model.addAttribute("book", new Book());
book2 = model.asMap().get("book");
原理:
[参数]类声明
首先将类进行反射,获取对象(属性都为初始值一般为null),
接着 通过表单请求参数对属性进行注值(不匹配忽略)
最后 Model.getMap modelMap -> put(key[类名首字母小写],value[反射并注值结果])

规则:
获取对象 -> Map<String,Object>
model.asMap().get("book")
modelMap.get("book")
Book book1 = modelMap.get("book");

 

4.ModelAttribute(模型映射)

原文:https://www.cnblogs.com/lanbao5339/p/14099632.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!