用Set方法对JavaBean属性赋值

1、创建名称为Produce的类,该类是封装商品信息的JavaBean,在该类中创建商品属性及与属性相对应的Get()和Set()方法。

package com.lyq.bean;

public class Produce {

    private String name="电吉他";
    private double price=1880.5;
    private int count=100;
    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、创建index.jsp页面,在该页面中实例化Produce对象,然后对其属性进行赋值并输出。

  <body>
 <jsp:useBean id = "produce" class ="com.lyq.bean.Produce">jsp:useBean>
 <jsp:setProperty property="name" name="produce" value="洗衣机"/>
 <jsp:setProperty property="price" name="produce" value="8888.88"/>
 <jsp:setProperty property="count" name="produce" value="88"/>
 <jsp:setProperty property="factoryAdd" name="produce" value="江苏省徐州市丰县"/>
 <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>

你可能感兴趣的:(用Set方法对JavaBean属性赋值)