1.1获取JavaBean属性:
编写名称为Produce的类,此类是封装商品对象的JavaBean在Produce类中定义商品属性,并提供相应的Getter()方法:
package com.wgh;
public class Produce {
private String name="编程词典个人版";
private double price=298;
private int count=10;
private String factoryAdd="吉林省明日科技有限公司";
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getCount() {
return count;
}
public String getFactoryAdd() {
return factoryAdd;
}
}
在Jsp页面中获取商品JavaBean中的商品信息,此操作通过Jsp动作标识进行获取
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<jsp:useBean id="produce" class="com.wgh.Produce"></jsp:useBean>
<!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>Insert title here</title>
</head>
<body>
<div>
<ul>
<li>
商品名称:<jsp:getProperty property="name" name="produce"/>
</li>
<li>
价格:<jsp:getProperty property="price" name="produce"/>(元)
</li>
<li>
数量:<jsp:getProperty property="count" name="produce"/>
</li>
<li>
厂址:<jsp:getProperty property="factoryAdd" name="produce"/>
</li>
</ul>
</div>
</body>
</html>
1.2对javaBean属性赋值
编写名称为Produce的JavaBean,用于封装商品信息。在该类中定义商品属性,以及与属性相对应的setter()和Getter()方法
package com.wgh;
public class Produce {
private String name="编程词典个人版";
private double price=289;
private int count=10;
private String factoryAdd="吉林省明日科技有限公司";
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price=price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count=count;
}
public String getFactoryAdd() {
return factoryAdd;
}
public void setFactoryAdd(String factoryAdd) {
this.factoryAdd=factoryAdd;
}
}
编写名称为index.jsp的页面,在此页面中实例化Produce对象,并对其属性进行赋值并输出:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<jsp:useBean id="produce" class="com.wgh.Produce"></jsp:useBean>
<jsp:setProperty property="name" name="produce" value="手机"/>
<jsp:setProperty property="price" name="produce" value="2890.88"/>
<jsp:setProperty property="count" name="produce" value="1"/>
<jsp:setProperty property="factoryAdd" name="produce" value="深圳###公司"/>
<!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>Insert title here</title>
</head>
<body>
<div>
<ul>
<li>
商品名称:<jsp:getProperty property="name" name="produce"/>
</li>
<li>
价格:<jsp:getProperty property="price" name="produce"/>(元)
</li>
<li>
数量:<jsp:getProperty property="count" name="produce"/>
</li>
<li>
厂址:<jsp:getProperty property="factoryAdd" name="produce"/>
</li>
</ul>
</div>
</body>
</html>
1.3在JSP页面中应用JavaBean
(本例实现办公自动化系统中,输入员工信息功能,主要通过在JSP页面中应用JavaBean进行实现)
编写名称为person的类,将其放置于com.wgh包中,实现对用户信息的封装。
package com.wgh;
public class Person {
private String name;
private int age;
private String sex;
private String address;
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address=address;
}
}
编写程序的主页面index.jsp,在此页面中放置输入员工信息所需要的表单,
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>录入员工信息页面</title>
<style type="text/css">
ul{
list-style:none;/*设置不显示项目符号*/
marge:0px;/*设置边外距*/
padding:5px;/*设置内边距*/
}
li{
padding :5px;/*设置内边距*/
}
</style>
</head>
<body>
<form action="register.jsp" method="post">
<ul>
<li>姓名:<input type="text" name="name"></li>
<li>年龄:<input type="text" name="age"></li>
<li>性别:<input type="text" name="sex"></li>
<li>住址:<input type="text" name="address" ></li>
<li><input type="submit" value="添加"></li>
</ul>
</form>
</body>
</html>
编写名称为register.jsp的JSP页面,用于对index.jsp页面中表单的提交请求进行处理,此页面将获取表单提交的所有信息,然后将所获取的员工信息输出到页面之中
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="person" class="com.wgh.Person" scope="page">
<jsp:setProperty name="person" property="*" />
</jsp:useBean>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>显示录入的员工信息页面</title>
<style type="text/css">
ul {
list-style: none; /*设置不显示项目符号*/
margin: 0px; /*设置外边距*/
padding: 5px; /*设置内边距*/
}
li {
padding: 5px; /*设置内边距*/
}
</style>
</head>
<body>
<ul>
<li>姓 名:<jsp:getProperty property="name" name="person" /></li>
<li>年 龄:<jsp:getProperty property="age" name="person" /></li>
<li>性 别:<jsp:getProperty property="sex" name="person" /></li>
<li>住 址:<jsp:getProperty property="address" name="person" /></li>
</ul>
</body>
</html>