1.前端页面
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试@ModelAttribute</title>
</head>
<body>
<h3>测试@ModelAttribute的不同用法</h3>
<a href="loginForm1">测试@ModelAttribute(value="")注释返回具体类的方法 </a><br><br>
<a href="loginForm2">测试@ModelAttribute注释void返回值的方法</a><br><br>
<a href="loginForm3">测试@ModelAttribute注释返回具体类的方法</a><br><br>
<a href="loginForm4">测试@ModelAttribute和@RequestMapping同时注释一个方法 </a><br><br>
<a href="loginForm5">测试@ModelAttribute注释一个方法的参数 </a><br><br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试@ModelAttribute</title>
</head>
<body>
<h3>测试@ModelAttribute(value="")注释返回具体类的方法</h3>
<form action="login1" method="post">
<table>
<tr>
<td><label>登录名: </label></td>
<td><input type="text" id="loginname" name="loginname" ></td>
</tr>
<tr>
<td><input id="submit" type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
loginForm2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试@ModelAttribute</title>
</head>
<body>
<h3>测试@ModelAttribute注释void返回值的方法</h3>
<form action="login2" method="post">
<table>
<tr>
<td><label>登录名: </label></td>
<td><input type="text" id="loginname" name="loginname" ></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>
</html>
loginForm3.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试@ModelAttribute</title>
</head>
<body>
<h3>测试@ModelAttribute注释返回具体类的方法</h3>
<form action="login3" method="post">
<table>
<tr>
<td><label>登录名: </label></td>
<td><input type="text" id="loginname" name="loginname" ></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>
</html>
loginForm4.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试@ModelAttribute</title>
</head>
<body>
<h3>测试@ModelAttribute和@RequestMapping同时注释一个方法</h3>
<form action="login4" method="post">
<table>
<tr>
<td><label>登录名: </label></td>
<td><input type="text" id="loginname" name="loginname" ></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>
</html>
loginForm5.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试@ModelAttribute</title>
</head>
<body>
<h3>测试@ModelAttribute注释一个方法的参数 </h3>
<form action="login5" method="post">
<table>
<tr>
<td><label>登录名: </label></td>
<td><input type="text" id="loginname" name="loginname" ></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>
</html>
result1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试@ModelAttribute(value="")注释返回具体类的方法</title>
</head>
<body>
访问request作用范围域中的loginname对象:${requestScope.loginname }<br>
<br>
</body>
</html>
result2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试@ModelAttribute注释void返回值的方法</title>
</head>
<body>
访问request作用范围域中的loginname对象:${requestScope.loginname }<br>
访问request作用范围域中的password对象:${requestScope.password }<br>
<br>
</body>
</html>
result3.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试@ModelAttribute注释返回具体类的方法</title>
</head>
<body>
访问request作用范围域中的user对象:${requestScope.user.username }<br>
<br>
</body>
</html>
result5.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试@ModelAttribute注释一个方法的参数</title>
</head>
<body>
访问request作用范围域中的user对象:${requestScope.user.username }<br>
<br>
</body>
</html>
login4.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试@ModelAttribute和@RequestMapping同时注释一个方法</title>
</head>
<body>
访问request作用范围域中的username对象:${requestScope.username }<br>
<br>
</body>
</html>
2.JavaBean
User.java
package com.bean;
import java.io.Serializable;
//域对象,实现序列化接口
public class User implements Serializable {
// 私有字段
private String loginname;
private String password;
private String username;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String loginname, String password, String username) {
super();
this.loginname = loginname;
this.password = password;
this.username = username;
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
3.控制类
FormController.java
package com.control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/*
* 总体控制类
*/
//Controller注解用于指示该类是一个控制器,可以同时处理多个请求动作
@Controller
public class FormController {
// 该方法映射的请求为http://localhost:8080/ModelAttributeTest/{formName}
@RequestMapping(value="/{formName}")
public String loginForm(@PathVariable String formName){
// 动态跳转页面
return formName;
}
}
ModelAttribute1Controller.java
package com.control;
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注解用于指示该类是一个控制器,可以同时处理多个请求动作
@Controller
public class ModelAttribute1Controller {
// 使用@ModelAttribute注释的value属性,来指定model属性的名称,model属性对象就是方法的返回值
@ModelAttribute("loginname")
public String userModel1(@RequestParam("loginname") String loginname){
return loginname;
}
@RequestMapping(value="/login1")
public String login1() {
return "result1";
}
}
ModelAttribute2Controller.java
package com.control;
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注解用于指示该类是一个控制器,可以同时处理多个请求动作
@Controller
public class ModelAttribute2Controller {
// model属性名称和model属性对象由model.addAttribute()实现,前提是要在方法中加入一个Model类型的参数。
// 注意:当URL或者post中不包含对应的参数时,程序会抛出异常。
@ModelAttribute
public void userModel2(
@RequestParam("loginname") String loginname,
@RequestParam("password") String password,
Model model){
model.addAttribute("loginname", loginname);
model.addAttribute("password", password);
}
@RequestMapping(value="/login2")
public String login2() {
return "result2";
}
}
ModelAttribute3Controller.java
package com.control;
import java.util.ArrayList;
import java.util.List;
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 com.bean.User;
//Controller注解用于指示该类是一个控制器,可以同时处理多个请求动作
@Controller
public class ModelAttribute3Controller {
// 静态List<User>集合,此处代替数据库用来保存注册的用户信息
private static List<User> userList;
// UserController类的构造器,初始化List<User>集合
public ModelAttribute3Controller() {
super();
userList = new ArrayList<User>();
User user1 = new User("test","123456","测试用户");
User user2 = new User("admin","123456","管理员");
// 存储User用户,用于模拟数据库数据
userList.add(user1);
userList.add(user2);
}
// 根据登录名和密码查询用户,用户存在返回包含用户信息的User对象,不存在返回null
public User find(String loginname,String password){
for(User user: userList){
if(user.getLoginname().equals(loginname) && user.getPassword().equals(password)){
return user;
}
}
return null;
}
// model属性的名称没有指定,它由返回类型隐含表示,如这个方法返回User类型,那么这个model属性的名称是user。
// 这个例子中model属性名称由返回对象类型隐含表示,model属性对象就是方法的返回值。它不需要指定特定的参数。
@ModelAttribute
public User userModel3(
@RequestParam("loginname") String loginname,
@RequestParam("password") String password){
return find(loginname,password);
}
@RequestMapping(value="/login3")
public String login3() {
return "result3";
}
}
ModelAttribute4Controller.java
package com.control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
//Controller注解用于指示该类是一个控制器,可以同时处理多个请求动作
@Controller
public class ModelAttribute4Controller {
// 这时这个方法的返回值并不是表示一个视图名称,而是model属性的值,视图名称是@RequestMapping的value值。
// Model属性名称由@ModelAttribute(value=””)指定,相当于在request中封装了username(key)=admin(value)。
@RequestMapping(value="/login4")
@ModelAttribute(value="username")
public String login4() {
return "admin";
}
}
ModelAttribute5Controller.java
package com.control;
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 com.bean.User;
//Controller注解用于指示该类是一个控制器,可以同时处理多个请求动作
@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.setLoginname(loginname);
user.setPassword(password);
return user;
}
// @ModelAttribute("user") User user注释方法参数,参数user的值来源于userModel5()方法中的model属性。
@RequestMapping(value="/login5")
public String login5(@ModelAttribute("user") User user){
user.setUsername("管理员");
return "result5";
}
}
截图