Use annotations @ModelAttribute
SpringMVC use of the Controller @ModelAttribute
during which comprises the following three positions:
- Used in the method
- Application of the method parameters
- Application of the method and the method is also used
@RequestMapping
Used in the method
First explain, is @ModelAttribute
annotated method will Controller
perform before each method executes, so for a Controller
time included more than one URL, to be used with caution.
1) @ModelAttribute
Remarks None return a value
@Controller
@RequestMapping(value = "/modelattribute")
public class ModelAttributeController {
@ModelAttribute
public void myModel(@RequestParam(required = false) String abc, Model model) {
model.addAttribute("attributeName", abc);
}
@RequestMapping(value = "/method")
public String method() {
return "method";
}
}
This example, in the request /modelattribute/method?abc=aaa
, the first performs myModel
the method, and then proceed with method
the method, the parameter abc
values are placed in Model
the middle, then brought to method
process.
When you return a view /modelattribute/method
when, Model
on'll be taken to a page, of course, you use @RequestParam
the time can be used required
to specify whether the argument is required.
If myModel
and method
combined, as follows, and this is our most commonly used method:
@RequestMapping(value = "/method") public String method(@RequestParam(required = false) String abc, Model model) { model.addAttribute("attributeName", abc); return "method"; }
2) Use @ModelAttribute
annotation method with a return value
@ModelAttribute public String myModel(@RequestParam(required = false) String abc) { return abc; } @ModelAttribute public Student myModel(@RequestParam(required = false) String abc) { Student student = new Student(abc); return student; } @ModelAttribute public int myModel(@RequestParam(required = false) int number) { return number; }
In this case, the return value of the object will be placed in default implied Model
in the Model
middle of key
is 返回值首字母小写
, value
a value is returned.
The above three cases is equivalent to:
model.addAttribute("string", abc); model.addAttribute("int", number); model.addAttribute("student", student);
In the jsp page use
${int}
when expressions are given:javax.el.ELException: Failed to parse the expression [${int}]
.
Solution:
In the tomcat configuration fileconf/catalina.properties
to add configurationorg.apache.el.parser.SKIP_IDENTIFIER_CHECK=true
What if this is a bit too limited, it is difficult to accept key
for the string
, int
, float
and so on like that.
Want to customize is actually very simple, just give @ModelAttribute
add value
properties to, the following:
@ModelAttribute(value = "num") public int myModel(@RequestParam(required = false) int number) { return number; }
This is equivalent to model.addAttribute("num", number);
.
Annotation parameters @ModelAttribute
@Controller @RequestMapping(value = "/modelattribute") public class ModelAttributeParamController { @ModelAttribute(value = "attributeName") public String myModel(@RequestParam(required = false) String abc) { return abc; } @ModelAttribute public void myModel3(Model model) { model.addAttribute("name", "zong"); model.addAttribute("age", 20); } @RequestMapping(value = "/param") public String param(@ModelAttribute("attributeName") String str, @ModelAttribute("name") String str2, @ModelAttribute("age") int str3) { return "param"; } }
从代码中可以看出,使用@ModelAttribute
注解的参数,意思是从前面的Model
中提取对应名称的属性。
这里提及一下@SessionAttributes
的使用:
- 如果在类上面使用了
@SessionAttributes("attributeName")
注解,而本类中恰巧存在attributeName
,则会将attributeName
放入到session
作用域。- 如果在类上面使用了
@SessionAttributes("attributeName")
注解,SpringMVC会在执行方法之前,自动从session
中读取key
为attributeName
的值,并注入到Model
中。所以我们在方法的参数中使用ModelAttribute("attributeName")
就会正常的从Model
读取这个值,也就相当于获取了session
中的值。- 使用了
@SessionAttributes
之后,Spring无法知道什么时候要清掉@SessionAttributes
存进去的数据,如果要明确告知,也就是在方法中传入SessionStatus
对象参数,并调用status.setComplete
就可以了。
应用在方法上,并且方法上也使用了@RequestMapping
@Controller @RequestMapping(value = "/modelattribute") public class ModelAttributeController { @RequestMapping(value = "/test") @ModelAttribute("name") public String test(@RequestParam(required = false) String name) { return name; } }
这种情况下,返回值String(或者其他对象)就不再是视图了,而是放入到Model
中的值,此时对应的页面就是@RequestMapping
的值test
。
如果类上有@RequestMapping
,则视图路径还要加上类的@RequestMapping
的值,本例中视图路径为modelattribute/test.jsp
。