实践笔记:JavaBean的思考和实践

目标:

  1. JavaBean的作用和实现
  2. 制作第一个网页

环境:基于前文所述

JavaBean是什么?

javaBean是这样一种类:

  1. 一个公共类,public
  2. 私有的实例变量,private
  3. 通过getter/setter来访问

javaBean用于实现业务逻辑与显示代码的分离

javaBean如何与显示代码交互呢?通过JSP内置对象request来获得初始值,通过setProperty方法设置初始值,通过getProperty方法输出初始值,分别调用setter和getter。

实例:表单提交和显示

Person.java

package cn.com.jsp;

public class Person {
	private String name;
	private int age;
	private String sex;
	private boolean secret;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public boolean isSecret() {
		return secret;
	}
	public void setSecret(boolean secret) {
		this.secret = secret;
	}
}


 

useBean.html

<!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>Java Bean Action</title>

<link rel = 'stylesheet' type = 'text/css' href = 'css/style.css'>

</head>
<body>
<br/>

<div align = "center">
	<form action = "useBean.jsp" method = "post">
		<legend>登陆</legend>
		<table align = "center" width = "400">
			<tr>
				<td align = "right" style = "font-weight:bold;">姓名:</td>
				<td><input type = "text" name = "name" value = "" style = "width:200px;"/></td>
			</tr>
			
			<tr>
				<td align = "right" style = "font-weight:bold;">年龄:</td>
				<td><input type = "text" name = "age" value = "" style = "width:200px;"/></td>
			</tr>
			
			<tr>
				<td align = "right" style = "font-weight:bold;">性别:</td>
				<td>
					<input type = "radio" name = "sex" value = "Male"/>Male
					<input type = "radio" name = "sex" value = "Female"/>Female
				</td>
			</tr>
			
			<tr>
				<td align = "right" style = "font-weight:bold;"></td>
				<td><input type = "submit" name = "search" value = "提交用户信息" class = "button"/></td>
			</tr>
			
		</table>
		
	</form>
</div>
</body>
</html>

form标记一个表单,这个页面通过表单提交用户信息,第14行action属性指出将表单信息通过HTTP的POST指令发送给useBean.jsp。request对象就定义了信息传递的结构。

useBean.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>JavaBean Actions</title>
<link rel = 'stylesheet' type = 'text/css' href = 'css/style.css'>
</head>
<body>

<jsp:useBean id="person" class = "cn.com.jsp.Person" scope = "page"/>

<jsp:setProperty name="person" property="*"/>

<div align = "center">
  	<form action = "" method = "get">
		<legend>信息</legend>
		<table align = "center" width = "400">
			<tr>
				<td align = "right" style = "font-weight:bold;">姓名:</td>
				<td>
					<jsp:getProperty name = "person" property = "name"/>
				</td>
			</tr>
			<tr>
				<td align = "right" style = "font-weight:bold;">年龄:</td>
				<td>
					<jsp:getProperty name = "person" property = "age"/>
				</td>
			</tr>
			<tr>
				<td align = "right" style = "font-weight:bold;">性别:</td>
				<td>
					<jsp:getProperty name = "person" property = "sex"/>
				</td>
			</tr>
			<tr>
				<td align = "right" style = "font-weight:bold;">性别:</td>
				<td>
					<input type="button" onclick = "history.go(-1);" value = "返回" class = "button">
				</td>
			</tr>
		</table>
 	</form>

</div>

</body>
</html>

第12行定义一个Person类的对象,第14行调用setProperty方法初始化这个对象,注意request信息的结构化。

目录结构:

其中Java Resources里放要编译的文件,WebContent里放脚本文件。很明显,采用javaBean后使得脚本语言和java语言文件分离了

 

你可能感兴趣的:(实践笔记:JavaBean的思考和实践)