可能是冲突了,把ischeck改成其他名称就好了,我这里把它改为了check
package springForm.test;
public class TestUser {
private String name;
private String password;
private String address;
//这里不能用ischeck,jsp里获取不到,还有这样的坑
private boolean check;
public boolean getCheck() {
return check;
}
public void setCheck(boolean check) {
this.check = check;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package springForm.test;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TestUserController {
@RequestMapping(value="/user",method=RequestMethod.GET)
public ModelAndView user() {
//这里的user对应的是jsp的文件名 /springForm/WEB-INF/jsp/user.jsp
return new ModelAndView("user","command",new TestUser());
}
@RequestMapping(value="/addUser",method=RequestMethod.POST)
public String addUser(@ModelAttribute("SpringWeb") TestUser user,ModelMap model) {
model.addAttribute("username",user.getName());
model.addAttribute("password",user.getPassword());
model.addAttribute("address_test",user.getAddress());
model.addAttribute("checkbox_test", user.getCheck());
return "userList";
}
}
下面是user.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User</title>
</head>
<body>
<h2>用户信息</h2>
<!-- 这里的Action对应的是Java代码里
@RequestMapping(value="/addUser,method=RequestMethod.POST)
addUser的名字要一样,不用加项目名(这里添加了会报错,因为路径重复了)
-->
<form:form action="addUser" method="post">
<table>
<tr>
<td><form:label path="name">用户名:</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<!-- 这里的path要与User.java中的变量明一致,不然报错。private String password -->
<td><form:label path="password">密码:</form:label></td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td><form:label path="address">地址:</form:label></td>
<td><form:textarea path="address" rows="5" clos="30"/></td>
</tr>
<tr>
<td><form:label path="check">复选框</form:label></td>
<td><form:checkbox path="check"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交" />
</td>
</tr>
</table>
</form:form>
</body>
</html>
下面是userList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!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>userList</title>
</head>
<body>
<h2>提交的用户信息</h2>
<table>
<tr>
<td>用户名:</td>
<!-- 这个是使用添加属性时的名字-->
<td>${username}</td>
</tr>
<tr>
<td>密码:</td>
<td>${password}</td>
</tr>
<tr>
<td>地址:</td>
<td>${address_test }</td>
</tr>
<tr>
<td>是否已勾选</td>
<td>${checkbox_test }</td>
</tr>
</table>
</body>
</html>