springmvc学习Spring相关

springmvc ModelAttribute 注释的学习

2017-02-21  本文已影响353人  缓慢移动的蜗牛

@ModelAttribute 注解可被应用在方法或方法参数上

注解在方法上的 @ModelAttribute 说明了方法的作用是用于添加一个或多个属性到model上。
这样的方法能接受与 @RequestMapping 注解相同的参数类型,只不过不能直接被映射到具体的请求上。
在同一个控制器中,注解了 @ModelAttribute 的方法实际上会在 @RequestMapping 方法之前被调用
在方法使用@ModelAttribute 方法的两种风格,详情示例代码如下:

public class Account implements Serializable {
   private static final long serialVersionUID = -3075041060818483817L;

   private String name;
   private Integer age;
   //  getter and setter
}
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/modelAtt")
public class ModeAttributeTest {
    /**
     * 方法-:
     * 方法通过返回值的方式默认地将添加一个属性
     *
     * 属性名没有被显式指定的时:框架将根据属性的类型给予一个默认名称
     *                  例如:本例返回一个 Account 类型的对象,则默认的属性名为"account"
     *                  你可以通过设置 @ModelAttribute 注解的值来改变默认值  @ModelAttribute("myAccount")
     * @param name
     * @return
     */
    @ModelAttribute
    public Account addAccount(@RequestParam(value = "name",defaultValue = "test")String name) {
        Account ac = new Account();

        ac.setName(name);
        ac.setAge(12);

        return ac;
    }
    /**
     * 方法二:
     *  方法接收一个 Model 对象,然后可以向其中添加任意数量的属性
     * @param number
     * @param model
     */
    @ModelAttribute
    public void populateModel(@RequestParam(value = "number",defaultValue = "123") String number, Model model) {
        model.addAttribute("number", number);
        model.addAttribute("other", "other");
    }

    @RequestMapping("/hello")
    public ModelAndView hello(){
        ModelAndView modelAndView = new ModelAndView("hello");

        return modelAndView;
    }
}

相关页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
账户名称:${account.name}<br/>
年龄:${account.age}<br/>
number:${number}<br/>
other:${other}<br/>
</body>
</html>

请求:http://localhost:8085/modelAtt/hello?name=zhangsan&number=123
@ModelAttribute 方法通常被用来填充一些公共需要的属性或数据,比如一个下拉列表所预设的几种状态,或者宠物的几种类型,或者去取得一个HTML表单渲染所需要的命令对象,比如 Account 等

注意
@ModelAttribute 注解也可以被用在 @RequestMapping 方法上。这种情况下, @RequestMapping 方法的返回值将会被解释为model的一个属性,而非一个视图名。此时视图名将以视图命名约定来方式来决议,与返回值为void的方法所采用的处理方法类似

以我的配置为例

<!-- 视图解析 -->
<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="order" value="2" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>
@Controller
@RequestMapping("/modelAtt")
public class ModeAttributeTest {
    @ModelAttribute("key")
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}

此时在浏览器中请求/hello 时,会去找/WEB-INF/jsp/modelAtt/hello.jsp此页面,并可在页面中获取附带的数据${key}

注解在方法参数上的 @ModelAttribute 说明了该方法参数的值将由model中取得。如果model中找不到,那么该参数会先被实例化,然后被添加到model中。在model中存在以后,请求中所有名称匹配的参数都会填充到该参数中。这在Spring MVC中被称为数据绑定,一个非常有用的特性,节约了你每次都需要手动从表格数据中转换这些字段数据的时间。

@RequestMapping("/hello")
public ModelAndView hello(@ModelAttribute Account account){
    account.setAge(12);
    account.setName("456");
    return new ModelAndView("hello");
}

上面的代码只是一种简单的使用方法
其实account的来源可由以下几种方式获得

上一篇 下一篇

猜你喜欢

热点阅读