1. 程式人生 > >spring 在Java中定義了變數ischeck,訪問時jsp報錯

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>