Usage Instructions for Model Objects in SpringMVC
- 2021-10-11 18:27:52
- OfStack
The main function of model objects is to save data, which can be used to bring data to the front end.
Commonly used model objects are the following:
ModelAndView
(As the name implies, models and views can carry both data information and view information. The general usage is as follows.)
/**
* ModelAndView Binding data to a view ( ModelMap Used to transfer data View Object for jumping)
* @return
* @throws Exception
*/
@RequestMapping(value="/case2")
public ModelAndView case2() throws Exception {
ModelAndView mav = new ModelAndView();
mav.setViewName("/demo03/model.jsp");
mav.addObject("sex", "boy");
return mav;
}
Map
, and
modelAndView
Principle 1, the same is to put data one by one
requestScope
In, the data fetched from the front end is also ${model data}
/**
* Target methods can be added Map Type ( In fact, it can also be Model Type or ModelMap Type ) Parameters of .
* @param map
* @return
*/
@RequestMapping("/case")
public String case1(Map map) throws Exception{
map.put("sex", " Achieve success !!");
return "/demo03/model.jsp";
}
@SessionAttributes
(Equivalent to creating
session
Object to the
session
Put data in the object, which is perfectly solved with 1 annotation here)
The basic format is as follows:
/**
* @SessionAttributes In addition to specifying the attributes that need to be put into the session by the attribute name, ( What is actually used is value Attribute value ),
* You can also specify which model attributes need to be put into the session by the object type of the model attributes ( What is actually used is types Attribute value )
* Attention : This annotation can only be placed on the top of the class . And can't modify the method when it is in map Neutralization session Medium Each has stored it 1 Entity classes, 1 A String String of class
*/
@SessionAttributes("user")
@Controller
public class SessionController {
@ModelAttribute("user")
public User getUser(){
User user = new User();
return user;
}
/**
* http://localhost:8080/s/s1?id=1
* Request forwarding forward: No processing is required
* Request redirection redirect: Use SessionAttribute Mode Used to pass to in redirection Stores the value in the session Medium "Remember to clear when you use it"
* @return
* @throws Exception
*/
@RequestMapping(value="/s1",method=RequestMethod.GET)
public String case1(@ModelAttribute("user") User user) throws Exception{
return "redirect:/s2";
}
@RequestMapping(value="/s2",method=RequestMethod.GET)
public String case2(Map map,HttpServletResponse res,SessionStatus sessionStatus) throws Exception{
User user=(User)map.get("user");
res.getWriter().println(user.getId());
sessionStatus.setComplete();
return null;
}
}
Difference between Model and ModelAndView in SpringMVC
1. Main differences
Model
Is the default parameter that exists in every request, using its
addAttribute()
Method to pass the server's value to the
jsp
Page;
ModelAndView
Include
model
And
view
Two parts, when using, you need to instantiate yourself and use
ModelMap
Used to pass values, or set
view
Name of
2. Examples
1) Use
Model
Value transfer
@RequestMapping(value="/list-books")
private String getAllBooks(Model model){
logger.error("/list-books");
List<Book> books= bookService.getAllBooks();
model.addAttribute("books", books);
return "BookList";
}
In
jsp
Page profit
${books}
You can take out the value
2) Use
ModelAndView
There are two ways to pass values. The different methods are in
jsp
The value of the page is different, and the value of the page is set
view
Name of
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) {
LibraryException le=null;
if(ex instanceof LibraryException){
le=(LibraryException)ex;
}else{
le=new LibraryException(" System unknown exception! ");
}
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("exception",le.getMessage());
modelAndView.getModel().put("exception",le.getMessage());
modelAndView.setViewName("error");
return modelAndView;
}
jsp
Medium
${requestScope.exception1}
Can be removed
exception1
The value of;
jsp
Medium
${exception2}
Can be removed
exception2
Value of