Spring - annotated using @ModelAttribute

Use annotations @ModelAttribute

SpringMVC use of the Controller @ModelAttributeduring 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 @ModelAttributeannotated method will Controllerperform before each method executes, so for a Controllertime included more than one URL, to be used with caution.

1) @ModelAttributeRemarks 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 myModelthe method, and then proceed with methodthe method, the parameter abcvalues are placed in Modelthe middle, then brought to methodprocess.

When you return a view /modelattribute/methodwhen, Modelon'll be taken to a page, of course, you use @RequestParamthe time can be used requiredto specify whether the argument is required.

If myModeland methodcombined, 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 @ModelAttributeannotation 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 Modelin the Modelmiddle of keyis 返回值首字母小写, valuea 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 file conf/catalina.propertiesto add configurationorg.apache.el.parser.SKIP_IDENTIFIER_CHECK=true

What if this is a bit too limited, it is difficult to accept keyfor the string, int, floatand so on like that.

Want to customize is actually very simple, just give @ModelAttributeadd valueproperties 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中读取keyattributeName的值,并注入到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

 

Guess you like

Origin www.cnblogs.com/daijiabao/p/11670534.html