spring 在Java中定义了变量ischeck,访问时jsp报错

可能是冲突了,把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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值