-1

我不知道为什么我的表单会这样。

这是我的 JSF 页面:

<h:body>
  <h:form>
    <h:form>
      <h:selectOneMenu value="#{productBean.product}" converter="#{productConverter}" validator="com.jsf.ProductAvailableValidator">
        <f:selectItems value="#{productBean.pizza}" var="pizza" itemValue="#{pizza}" itemLabel="#{pizza.name}" />
        <h:commandButton value="Dodaj" action="#{productBean.addToOrder(productBean.product.name)}" /></h:selectOneMenu>
    </h:form>
    <h:form>
      <h:selectOneMenu value="#{productBean.product}" converter="#{productConverter}" validator="com.jsf.ProductAvailableValidator">
        <f:selectItems value="#{productBean.drink}" var="drink" itemValue="#{drink}" itemLabel="#{drink.name}" />
        <h:commandButton value="Dodaj" action="#{productBean.addToOrder(productBean.product.name)}" /></h:selectOneMenu>
    </h:form>
    <h:form>
      <h:selectOneMenu value="#{productBean.product}" converter="#{productConverter}" validator="com.jsf.ProductAvailableValidator">
        <f:selectItems value="#{productBean.other}" var="other" itemValue="#{other}" itemLabel="#{other.name}" />
        <h:commandButton value="Dodaj" action="#{productBean.addToOrder(productBean.product.name)}" /></h:selectOneMenu>
    </h:form>
    <messages />
    <h:outputText value="#{productBean.order}" />
    <h:commandButton value="Wyczyść" action="#{ProductBean.clearOrder()}" /></h:form>
</h:body>

这是我的 ProductBean:

@ManagedBean
@SessionScoped
public class ProductBean extends Connector
{

    private List<Product> products;
    private List<Product> pizza;
    private List<Product> drink;
    private List<Product> other;
    boolean first = true;
    private StringBuilder order = new StringBuilder();

    public String getOrder() {
        return order.toString();
    }
    private Product product;

    public Product getProduct() {
        return product;
    }
    public void setProduct(Product product) {
        this.product = product;
    }
    public void addToOrder(String prod)
    {
        System.out.println("dodaje");
        if(first)
        {
            first = false;
            this.order.append(prod);  
        }
        else
            this.order.append(" + ").append(prod);
    }
    public void clearOrder()
    {
        this.order = null;
        first = true;
    }
    public void setProducts(List<Product> products) {
        this.products = products;
    }
    public ProductBean() throws SQLException
    {
        resultSet = statement.executeQuery("SELECT * FROM dbo.products");
        products = new ArrayList<Product>();
        while(resultSet.next())
        {
            product = new Product();
            product.setId_product(resultSet.getInt("id_product"));
            product.setName(resultSet.getString("name"));
            product.setCategory(resultSet.getInt("category_id"));
            product.setIs_available(resultSet.getInt("is_available"));
            products.add(product);
        }
    }
    public Product getProductById(int id)
    {
        Iterator<Product> it = products.iterator();
        while(it.hasNext()) 
        {
            Product prod = it.next();
            if(prod.getId_product() == id)
                return prod;
        }
        return null;
    }

    public List<Product> getPizza() throws SQLException
    {   
        Iterator<Product> it = products.iterator();
        pizza = new ArrayList<Product>();
        while(it.hasNext()) 
        {
            Product prod = it.next();
            if(prod.getCategory() == 1)
                pizza.add(prod);
        }
        return pizza;
    }
    public List<Product> getDrink() throws SQLException
    {   
        Iterator<Product> it = products.iterator();
        drink = new ArrayList<Product>();
        while(it.hasNext()) 
        {
            Product prod = it.next();
            if(prod.getCategory() == 2)
                drink.add(prod);
        }
        return drink;
    }
    public List<Product> getOther() throws SQLException
    {   
        Iterator<Product> it = products.iterator();
        other = new ArrayList<Product>();
        while(it.hasNext()) 
        {
            Product prod = it.next();
            if(prod.getCategory() == 3)
                other.add(prod);
        }
        return other;
    }
    public List<Product> getProducts() {
        return products;
    }

}

我还在这里发送了一个屏幕截图,以使代码更容易和更快地分析: 在此处输入图像描述

这里发生的情况是,只有第一个按钮“Dodaj”(意思是“添加”)有效,并在 outputlabel 中正确添加了字符串。其余的人什么都不做。当我更改顺序时,再次只有第一个有效。为什么?

4

1 回答 1

1

您有多个嵌套/级联<h:form>的,这在 HTML 中是不允许的!要么制作一个<h:form>并将所有元素放在该形式中,要么制作多个<h:form>,但不要嵌套/级联它们!

于 2013-05-15T16:05:43.713 回答