-1

我有一个接受产品详细信息的表单,也显示输入的详细信息,我使用 bean 来存储和显示回来。我的问题是当我从不同的机器加载相同的页面时,以前的用户数据是可见的。

有人可以帮我吗...在此先感谢

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  >
<h:head>
    <h:outputStylesheet library="css" name="table-style.css"  />
</h:head>
<h:body>


    <h:form>
        <h:dataTable value="#{order.orderList}" var="o"
            styleClass="order-table"
            headerClass="order-table-header"
            rowClasses="order-table-odd-row,order-table-even-row"
        >

        <h:column>

            <f:facet name="header">Order No</f:facet>
            #{o.orderNo}

        </h:column>

        <h:column>

            <f:facet name="header">Product Name</f:facet>
            #{o.productName}

        </h:column>

        <h:column>

            <f:facet name="header">Price</f:facet>
            #{o.price}

        </h:column>

        <h:column>

            <f:facet name="header">Quantity</f:facet>
            #{o.qty}

        </h:column>

        <h:column>

            <f:facet name="header">Action</f:facet>

            <h:commandLink value="Delete" 
                               action="#{order.deleteAction(o)}" />

        </h:column>

        </h:dataTable>

        <h3>Enter Order</h3>
        <table>
        <tr>
            <td>Order No :</td>
            <td><h:inputText size="10" value="#{order.orderNo}" /></td>
        </tr>
        <tr>
            <td>Product Name :</td>
            <td><h:inputText size="20" value="#{order.productName}" /></td>
        </tr>
        <tr>
            <td>Quantity :</td>
            <td><h:inputText size="5" value="#{order.price}" /></td>
        </tr>
        <tr>
            <td>Price :</td>
            <td><h:inputText size="10" value="#{order.qty}" /></td>
        </tr>
        </table>

        <h:commandButton value="Add" action="#{order.addAction}" />

    </h:form>
</h:body>
</html>



package Bean;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.*;
import javax.faces.bean.SessionScoped;


@ManagedBean(name="order")
@RequestScoped
public class PodMasterBean implements Serializable {

private static final long serialVersionUID = 1L;
private String orderNo;
private String productName;
private BigDecimal price;
private int qty;
private static final ArrayList<Order> orderList =
        new ArrayList<Order>(Arrays.asList(
        new Order("A0001", "Intel CPU",
        new BigDecimal("700.00"), 1),
        new Order("A0002", "Harddisk 10TB",
        new BigDecimal("500.00"), 2),
        new Order("A0003", "Dell Laptop",
        new BigDecimal("11600.00"), 8),
        new Order("A0004", "Samsung LCD",
        new BigDecimal("5200.00"), 3),
        new Order("A0005", "A4Tech Mouse",
        new BigDecimal("100.00"), 10)));

public ArrayList<Order> getOrderList() {

    return orderList;

}

public String addAction() {

    Order order = new Order(this.getOrderNo(), this.getProductName(), this.getPrice(), this.getQty());

    orderList.add(order);
    return null;
}

public String deleteAction(Order order) {

    orderList.remove(order);
    return null;
}


public String getOrderNo() {
    return orderNo;
}


public void setOrderNo(String orderNo) {
    this.orderNo = orderNo;
}


public String getProductName() {
    return productName;
}


public void setProductName(String productName) {
    this.productName = productName;
}


public BigDecimal getPrice() {
    return price;
}


public void setPrice(BigDecimal price) {
    this.price = price;
}


public int getQty() {
    return qty;
}


public void setQty(int qty) {
    this.qty = qty;
}



/// new class
public static class Order {

    private String orderNo;
    private String productName;
    private BigDecimal price;
    private int qty;

    public Order(String orderNo, String productName,
            BigDecimal price, int qty) {
        this.orderNo = orderNo;
        this.productName = productName;
        this.price = price;
        this.qty = qty;
    }

    public String getOrderNo() {
        return orderNo;
    }


    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }


    public String getProductName() {
        return productName;
    }


    public void setProductName(String productName) {
        this.productName = productName;
    }


    public BigDecimal getPrice() {
        return price;
    }


    public void setPrice(BigDecimal price) {
        this.price = price;
    }


    public int getQty() {
        return qty;
    }


    public void setQty(int qty) {
        this.qty = qty;
    }
}
}
4

1 回答 1

2

使用static变量意味着该 bean 的每个实例都将在整个应用程序中具有该值,即使是为每个请求重新实例化。您每次执行时都会向该列表添加一个订单addAction(),因此它会对每个最终用户产生影响。

于 2013-07-03T12:02:16.147 回答