SpringMVC (four)

Access via ModelAndView

@RequestMapping(value="/index")
public ModelAndView index(User user){
	user.setAge("A");
	user.setSex("B");
// ModelAndView mav = new ModelAndView();
// mav.setViewName("message"); // no extension needed
	
	ModelAndView mav = new ModelAndView("message");
	
	mav.addObject(user);
	return mav;

}

 Access data model @ModelAttribute

 

 way of use

@RequestMapping(value="/index")
public String index(){
	return "message";
}

@ModelAttribute
public void populateModel(@RequestParam String age,@RequestParam String sex,Model model){
	model.addAttribute("str", "modelAttribute ==== > " + age + "   :" + sex);
}

 When the URL accesses the index method, the populateModel method will be used before , and after the execution is completed, the index() will be executed again.

 

 

 Method 2 ( return object )

@RequestMapping(value="/index")
public String index(){
	return "message";
}

@ModelAttribute
public User populateModel(){
	User user = new User();
	user.setAge("10");
	return user;
}

 The model name does not need to be specified, it is implicitly represented by the return type. If this method returns the User type, then the name of the model attribute is user.

@ModelAttribute(value="myUser")
public User populateModel(){
	User user = new User();
	user.setAge("10");
	return user;
}

 You can also specify the return name. After specifying, the returned object name is myUser . If not specified, it will return User.

 

   Use method 3 ( merge objects for return )

@RequestMapping(value="/index")
public String index(User user){
	user.setSex ("man");
	return "message";
}

@ModelAttribute
public User populateModel(){
	User user = new User();
	user.setAge("10");
	return user;
}

  Use mode 4 ( object merge to specify object name )

@RequestMapping(value="/index")
public String index(@ModelAttribute("myUser")User user){
	user.setSex ("man");
	return "message";
}

@ModelAttribute(value="myUser")
public User populateModel(){
	User user = new User();
	user.setAge("10");
	return user;
}

 public String test(@ModelAttribute("user") UserModel user)

 

There is an additional annotation @ModelAttribute("user") here, its function is to add the bound command object with the name "user" to the model object for the display of the view page. At this point, we can use ${user.username} on the view page to get the properties of the bound command object.

 

@SessionAttributes annotation

@Controller
@SessionAttributes("myUser") // Automatically save the object myUser to the session
public class TController {

	@RequestMapping(value="/index")
	public String index(@ModelAttribute("myUser")User user){
		user.setSex ("man");
		return "message";
	}

	@ModelAttribute(value="myUser")
	public User populateModel(){
		User user = new User();
		user.setAge("10");
		return user;
	}
	
	@RequestMapping(value="/out")
	public String out(@ModelAttribute("myUser")User user,SessionStatus sessionStatus){
		user.setSex ("man");
		sessionStatus.setComplete(); // Clear the session attribute corresponding to this processor
		return "message";
	}

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326081763&siteId=291194637