目的:在页面跳跃后,文本框里的自动获取数据。
方法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");
原文:https://www.cnblogs.com/lanbao5339/p/14099632.html