@SessionAttributes,@ModelAttribute
1.@SessionAttributes允许我们有选择地指定Model中的哪些属性需要转存到HttpSession对象当中
package com.game.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.SessionAttributes; import com.game.model.User; @Controller @SessionAttributes("user") public class SessionAttributesController { @RequestMapping(value="/{formName}") public String loginForm(@PathVariable String formName) { return formName;//动态页面的跳转,可以是任何页面 } @RequestMapping(value="/login2") public String loing(@RequestParam("loginId") String loginId,@RequestParam("password") String password,Model model) { User user = new User(); user.setLoginId(loginId); user.setPassword(password); user.setUserName("admin"); model.addAttribute("user", user); return "welcome2"; } }
<body> <h3>测试@SessionAttributes注解</h3> <br> <form action="login2" method="post"> <table> <tr> <td><label>登录名:</label></td> <td><input type="text" id="loginId" name="loginId"></td> </tr> <tr> <td><label>密码:</label></td> <td><input type="password" id="password" name="password"></td> </tr> <tr><td><input id="submit" type="submit" value="登录"></td></tr> </table> </form> </body>
<body> ${ requestScope.user.userName }<br> ${ sessionScope.user.userName } </body>
2.@ModelAttributes将请求参数绑定到Model对象
只支持一个属性value,表示绑定的属性名称
被@ModelAttributes注解的方法会在Controller每个方法执行前被调用
<body> <h3>@ModelAttributes注解测试</h3> <a href="loginForm1">@ModelAttributes(value="")注释返回具体类的方法</a><br> <a href="loginForm1">@ModelAttributes注释void返回值的方法</a><br> <a href="loginForm1">@ModelAttributes注释返回具体类的方法</a><br> <a href="loginForm1">@ModelAttributes和@RequestMapping同时注释一个方法</a><br> <a href="loginForm1">@ModelAttributes注释一个方法的参数</a><br> </body>
package com.game.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class ModelAttributes1Controller { //设置了一个logidI到Model当中,然后@requestScope。loginId就可以取到,指定了model属性的名称,方法的返回值就是属性的值 @ModelAttribute("loginId") public String userModel1(@RequestParam("loginId") String loginId) { return loginId; } @RequestMapping("/login11") public String login1() { return "result1"; } }
package com.game.controller; 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; @Controller public class ModelAttributes2Controller { @ModelAttribute public void userModel1(@RequestParam("loginId") String loginId,Model model) { model.addAttribute("loginId", loginId); } @RequestMapping("/login22") public String login1() { return "result1"; } }
其它几种方式大同小异,不一一列举了