摘要:
jsp:setProperty标签用于对JavaBean中的属性赋值
1 创建一个Student的类,代码如下:
package ok;
public class Student {
private int id;
private int age;
private String classes;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
}
2 创建一个form表单,将它放置在index.jsp中:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="student.jsp" method="post">
<p>学号:<input type="text" name="id"></p>
<p>年龄:<input type="text" name="age"></p>
<p>
<input type="submit" value="提交">
<input type="reset" value="重置">
</p>
</form>
</body>
</html>
3 创建一个student.jsp实例化Student对象,并输出相应的属性值:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="student" class="ok.Student"></jsp:useBean>
<jsp:setProperty property="*" name="student"/>
<div align="center">
<p>学号:<%=student.getId() %></p>
<p>年龄:<%=student.getAge() %></p>
</div>
</body>
</html>