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>