1.工程说明
创建一个JavaBean类:Student,然后创建UseBean.jsp使用useBean访问并修改属性
2.运行效果

3.Student类,写在一个test包中
package test;
/*
* 2018.3.26 Dragon
* JavaBean规范
1.序列化
2.无需构造器,默认已写好
3.属性需要更改器和访问器
*/
public class Student {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.UseBean.jsp页面:使用JSP的动作访问Student
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'UseBean.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h2>useBean,jsp:setProperty,jsp:getProperty使用的一个简单例子</h2> <br>
<hr>
<!-- 访问当前的JavaBean:Student -->
<jsp:useBean id="student" class="test.Student" />
<!-- 修改当前的属性 -->
<jsp:setProperty name="student" property="name" value="小龙" />
<jsp:setProperty name="student" property="id" value="5201314" />
<!-- 访问当前的属性 ,在屏幕上输出 -->
<table>
<tr>
<td>姓名:</td>
<td><jsp:getProperty name="student" property="name" /></td>
</tr>
<tr>
<td>学号:</td>
<td><jsp:getProperty name="student" property="id" /></td>
</tr>
</table>
</body>
</html>

该博客通过一个简单的例子展示了如何在JSP中使用useBean、setProperty和getProperty动作来操作JavaBean。首先创建了一个名为Student的JavaBean类,接着在UseBean.jsp页面中利用这些动作进行访问和修改属性。最终,将工程部署到Tomcat服务器,并给出了访问页面的URL以展示运行效果。
932

被折叠的 条评论
为什么被折叠?



