org.springframework.web.bind.annotation.ModelAttribute注解类型将请求参数绑定到Model对象。 @ModelAttribute注解只支持一个属性value,类型为String,表示绑定的属性名称。
提示:被@ModelAttribute注释的方法会在Controller每个方法执行前被执行,因此在一个Controller映射到多个URL时,要谨慎使用。
index.jsp 用于展示调用关于@ModelAttribute注解不同的使用方法。
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <h3>测试@ModelAttribute</h3> <a href="loginForm1">测试@ModelAttribute(value="")注释返回具体类的方法</a> <a href="loginForm2">测试@ModelAttribute注释void返回值的方法</a> <a href="loginForm3">测试@ModelAttribute注释返回具体类的方法</a> <a href="loginForm4">测试@ModelAttribute和@RequestMapping同时注释一个方法</a> <a href="loginForm5">测试@ModelAttribute注释一个方法的参数</a> </body> </html>
FormController 用于接受index.jsp页,a标签(超链接)传递的地址。
@Controller public class FormController { @RequestMapping(value = "/{formName}") public String loginForm(@PathVariable String formName){ return formName; } }
loginForm1.jsp
<html> <head> <title>Title</title> </head> <body> <h3>测试@ModelAttribute(value="")注释返回具体类的方法 </h3> <form action="login111" method="post"> <label for="loginName">登录名:</label><input type="text" id="loginName" name="loginName"> <input type="submit"> </form> </body> </html>
ModelAttribute1Controller
package com.wen.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 ModelAttribute1Controller { //使用@ModelAttribute注释的value属性,来指定model属性的名称,model属性的值就是方法的返回值 @ModelAttribute("loginName") public String userModel1( @RequestParam("loginName") String loginName){ return loginName; } @RequestMapping(value = "/login111") public String login111(){ return "result1"; } }
result1.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${requestScope.loginName} </body> </html>
loginForm2.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h3>测试@ModelAttribute注释void返回值的方法</h3> <form action="login222" method="post"> <label for="loginName">登录名:</label><input type="text" id="loginName" name="loginName"> <label for="password">密码:</label><input type="password" id="password" name="password"> <input type="submit" value="登录"> </form> </body> </html>
ModelAttribute2Controller
package com.wen.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 ModelAttribute2Controller { //model属性名称和值由model.addAttribute()实现,前提是要在方法中加入图个Model类型的参数 @ModelAttribute public void userModel2( @RequestParam("loginName") String loginName, @RequestParam("password") String password, Model model) { model.addAttribute("loginName", loginName); model.addAttribute("password", password); } @RequestMapping(value = "/login222") public String login222(){ return "result2"; } }
result2.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${requestScope.loginName} ${requestScope.password} </body> </html>
loginForm3.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h3>测试@ModelAttribute注释返回具体类的方法</h3> <form action="login333" method="post"> <label for="loginName">登录名:</label><input type="text" id="loginName" name="loginName"> <label for="password">密码:</label><input type="password" id="password" name="password"> <input type="submit" value="登录"> </form> </body> </html> </body> </html>
ModelAttribute3Controller
package com.wen.controller; import com.wen.domain.User; 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; import java.util.ArrayList; import java.util.List; @Controller public class ModelAttribute3Controller { //静态List<User>集合,此处代替数据库用来保存注册的用户信息 private static List<User> userList; //此构造器,初始化List<User>集合 public ModelAttribute3Controller() { super(); userList = new ArrayList<>(); User user1 = new User("test", "123"); User user2 = new User("admin", "456"); userList.add(user1); userList.add(user2); } //根据登录名和密码查询用户,用户存在返回用户信息的User对象,不存在返回null private User find(String loginName, String password) { for (User user : userList) { if (user.getName().equals(loginName) && user.getPwd().equals(password)) { return user; } } return null; } //model属性的名称没有被锁定,它由返回类型隐含表示,如这个方法返回User类型, //那么这个model属性的名称就是user @ModelAttribute public User userModel3( @RequestParam("loginName") String loginName, @RequestParam("password") String password) { return find(loginName, password); } @RequestMapping(value = "/login333") public String login333() { return "result3"; } }
result3.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${requestScope.user.name} </body> </html>
loginForm4.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <body> <h3>测试@ModelAttribute注释void返回值的方法</h3> <form action="login444" method="post"> <label for="loginName">登录名:</label><input type="text" id="loginName" name="loginName"> <label for="password">密码:</label><input type="password" id="password" name="password"> <input type="submit" value="登录"> </form> </body> </body> </html>
ModelAttribute4Controller
package com.wen.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class ModelAttribute4Controller { /** * 视图名称是@RequestMapping的value值——"/login444",即跳转结果 * * @return model属性名称就是value的值 */ @RequestMapping(value = "/login444") @ModelAttribute(value = "username") public String login444() { return "admin"; } }
login444.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${requestScope.username} </body> </html>
loginForm5.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <body> <h3>测试@ModelAttribute注释一个方法的参数</h3> <form action="login555" method="post"> <label for="loginName">登录名:</label><input type="text" id="loginName" name="loginName"> <label for="password">密码:</label><input type="password" id="password" name="password"> <input type="submit" value="登录"> </form> </body> </body> </html>
ModelAttribute5Controller
package com.wen.controller; import com.wen.domain.User; 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 ModelAttribute5Controller { //model的属性名称就是value的值,即"user",model属性对象就是方法的返回值 @ModelAttribute("user") public User userModel5( @RequestParam("loginName") String loginName, @RequestParam("password") String password){ User user =new User(); user.setName(loginName); user.setPwd(password); return user; } @RequestMapping(value = "/login555") public String login555(@ModelAttribute("user") User user){ user.setName("管理员"); return "result5"; } }
result5.jsp
Tips:@ModelAttribute注解的使用的方法有很多种,非常灵活,我们可以根据业务需求选择使用。
原文链接:https://www.cnblogs.com/guowenrui/p/10363583.html