JavaBean的应用

1. 获取JavaBean属性信息

例1.1 在JSP页面中显示JavaBean属性信息。

(1)创建名称为Produce的类,该类是封装商品对象的JavaBean,在Produce类中创建商品属性,并提供相应的getXXX()方法。关键代码如下:

package com.cn.gao;

public class Produce {
    //商品名称
    private String name="电吉他";
    private double price = 1880.5;
    private int count = 100;
    private String factoryAdd = "吉林省长春市XXX琴行";
    
    public String getName() {
        return name;
    }
    
    public double getPrice() {
        return price;
    }
    
    public int getCount() {
        return count;
    }
    
    public String getFactoryAdd() {
        return factoryAdd;
    }
    
    
}

(2)在JSP页面中获取商品JavaBean中的属性信息,该操作通过JSP动作标签进行获取。关键代码如下:

produce.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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 'produce.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="produce" class="com.cn.gao.Produce"></jsp:useBean>
    <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>

本实例中主要通过<jsp:useBean>标签实例化商品的JavaBean对象,<jsp:getProperty>标签获取JavaBean中的属性信息。实例运行结果如下图:

注:<jsp:useBean>标签与<jsp:getProperty>标签之所以能够操作Java类,是因为我们所编写的Java类遵循了JavaBean规范。所以在编写JavaBean时要遵循制定的JavaBean规范。

 2. 对JavaBean属性赋值

例2.1 创建商品对象的JavaBean,在该类中提供属性及与属性相对应的get和set方法,在JSP页面中对JavaBean属性赋值并获取输出。

(1)创建名称为Produce的类,该类是封装商品信息的JavaBean,在该类中创建商品属性及与属性相应的get和set方法。关键代码如下:

package com.cn.gao;

public class Produce {
    //商品名称
    private String name;
    private double price;
    private int count;
    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;
    }
    
    
}

(2)创建名称为produce.jsp页面,在该页面中实例化Produce对象,然后对其属性进行赋值并输出。关键代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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 'produce.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="produce" class="com.cn.gao.Produce"></jsp:useBean>
    <jsp:setProperty property="name" name="produce" value="洗衣机"/>
    <jsp:setProperty property="price" name="produce" value="888.88"/>
    <jsp:setProperty property="count" name="produce" value="88"/>
    <jsp:setProperty property="factoryAdd" name="produce" value="广东省XXX公司"/>
    <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>

页面主要是通过<jsp:useBean>标签实例化商品的JavaBean对象,<jsp:setProperty>标签对Produce对象中的属性赋值,然后再通过<jsp:getProperty>标签输出已赋值的Produce对象中的属性信息。实例运行结果如下图所示:

posted @ 2015-04-12 11:24  ~风轻云淡~  阅读(1162)  评论(0编辑  收藏  举报