标签:des style blog http io ar color sp java
JavaBean是用Java语言所编写的可重用组件,他可以应用于系统的很多层中,如:PO,VO,DTO,POJO等,其应用十分广泛。
在JavaBean中,为了防止外部直接对JavaBean属性进行调用,通常将JavaBean中的属性设置为private,并提供public方法对其进行访问。
范例:
在JSP页面中显示JavaBean的属性信息
(1)创建名为Product的类,该类是封装了商品属性的JavaBean
package com.zgy.javabean;
public class Product
{
private String name = "piano"; //商品名称
private float price = 10000; //价格
private int count = 500; //数量
private String producePlace = "China";//产地
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public float getPrice()
{
return price;
}
public void setPrice(float price)
{
this.price = price;
}
public int getCount()
{
return count;
}
public void setCount(int count)
{
this.count = count;
}
public String getProducePlace()
{
return producePlace;
}
public void setProducePlace(String producePlace)
{
this.producePlace = producePlace;
}
}
(2)在JSP页面中获取JavaBean的属性
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="com.zgy.javabean.Product" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Product Attribute</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>
<div align="center">
<jsp:useBean id="product" class="com.zgy.javabean.Product">
</jsp:useBean>
<div>
<ul>
<li>
商品名称:<jsp:getProperty name="product" property="name" />
</li>
<li>
商品价格:<jsp:getProperty name="product" property="price"/>
</li>
<li>
商品数量:<jsp:getProperty name="product" property="count"/>
</li>
<li>
商品产地:<jsp:getProperty name="product" property="producePlace"/>
</li>
</ul>
</div>
</div>
</body>
</html>
范例:
还是刚才的那个Product类,现在创建SetJavaBeanProperty.jsp,在该页面中对Product对象进行实例化,并且对其属性进行赋值
<%@ page language="java" import="java.util.*" pageEncoding="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 ‘SetJavaBeanProperty.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>
<jsp:useBean id="product" class="com.zgy.javabean.Product"></jsp:useBean>
<jsp:setProperty name="product" property="name" value="TV"></jsp:setProperty>
<jsp:setProperty name="product" property="price" value="5000f"/>
<jsp:setProperty name="product" property="count" value="100"></jsp:setProperty>
<jsp:setProperty name="product" property="producePlace" value="USA"/>
<div>
<ul>
<li>
商品名称:<jsp:getProperty name="product" property="name" />
</li>
<li>
商品价格:<jsp:getProperty name="product" property="price"/>
</li>
<li>
商品数量:<jsp:getProperty name="product" property="count"/>
</li>
<li>
商品产地:<jsp:getProperty name="product" property="producePlace"/>
</li>
</ul>
</div>
</body>
</html>
标签:des style blog http io ar color sp java
原文地址:http://blog.csdn.net/yaguanzhou2014/article/details/41421707