springMVC 在controller中将参数封装对象

org.springframework.web.bind.annotation 
Annotation Type ModelAttribute

@Target(value={PARAMETER,METHOD})
@Retention(value=RUNTIME)
@Documented
public @interface ModelAttribute
Annotation that binds a method parameter or method return value to a named model attribute, exposed to a web view. Supported for RequestMapping annotated handler classes.

Can be used to expose command objects to a web view, using specific attribute names, through annotating corresponding parameters of a RequestMapping annotated handler method).

Can also be used to expose reference data to a web view through annotating accessor methods in a controller class which is based on RequestMapping annotated handler methods, with such accessor methods allowed to have any arguments that RequestMapping supports for handler methods, returning the model attribute value to expose.

Since:
2.5
Author:
Juergen Hoeller


代码说明

1 controller 方法
public void addUser( @ModelAttribute("user") User user){
user.service.add(user);
}

2 form 表单数据


关键在controller方法中添加注解@ModelArribute

form表单中标的需要 name="user.name";

你可能感兴趣的:(springMVC 在controller中将参数封装对象)