JAVA购物车应用

问题描述 投票:0回答:0

我正在参加 JAVA 挑战。我要创建一个应用程序,将书籍列表显示为 h:selectOneRadio 元素。当用户提交表单时,将用户的选择存储在 @SessionScoped 托管 bean 中。允许用户返回图书列表并进行其他选择。提供查看购物车的链接。在购物车页面上,显示用户所做的选择列表、每本书的价格以及购物车中所有书籍的总数。

这是我到目前为止所拥有的:

SelectionsBean.java

package sessiontracking;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean( name="selectionsBean" )
@SessionScoped
public class SelectionsBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private final Map<String, Double> pricesMap;

    private List<String> selections;

    public SelectionsBean() {
        pricesMap = new HashMap<>();
        pricesMap.put("Java How to Program", 139.95);
        pricesMap.put("C++ How to Program", 119.95);
        pricesMap.put("iPhone for Programmers: An App-Driven Approach", 49.95);
        pricesMap.put("Android for Programmers: An App-Driven Approach", 49.95);

        // initialize selections list
        selections = new ArrayList<>();
    }

    public void addToCart(String selection) {
        selections.add(selection);
    }

    public void removeFromCart(String selection) {
        selections.remove(selection);
    }

    public double getTotalPrice() {
        double totalPrice = 0.0;
        for (String selection : selections) {
            totalPrice += pricesMap.get(selection);
        }
        return totalPrice;
    }

    public List<String> getSelections() {
        return selections;
    }

    public void setSelections(List<String> selections) {
        this.selections = selections;
  
}


index.html

<?xml version='1.0' encoding='UTF-8' ?>
<!-- index.xhtml -->
<!-- Allow the user to select a book -->
<!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">
<h:head>
<title>Book Selection Page</title>
</h:head>
<h:body>
<h1>Welcome to the Bookstore!</h1>
<p>You have selected #{selectionsBean.Size} book(s).
</p>
<h3>Select a Book and Press Submit</h3>
<h:form>
<h:selectOneRadio id="bookSelectOneRadio" required="true"
requiredMessage="Please choose a book, then press Submit"
value="#{selectionsBean.selectedBook}">
<f:selectItem itemValue="java" itemLabel="Java How to Program"/>
<f:selectItem itemValue="cpp" itemLabel="C++ How to Program"/>
<f:selectItem itemValue="iphone"
itemLabel="iPhone for Programmers: An App-Driven Approach"/>
<f:selectItem itemValue="android"
itemLabel="Android for Programmers: An App-Driven Approach"/>
</h:selectOneRadio>
<p><h:commandButton value="Submit"/></p>
</h:form>
<p><h:outputLink value="cart.xhtml">
Click here to view your shopping cart
</h:outputLink></p>
</h:body>
</html>

cart.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- index.xhtml -->
<!-- Allow the user to select a book -->
<!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">
<h:head>
<title>Book Selection Page</title>
</h:head>
<h:body>
<h1>Welcome to the Bookstore!</h1>
<p>You have selected #{selectionsBean.Size} book(s).
</p>
<h3>Select a Book and Press Submit</h3>
<h:form>
<h:selectOneRadio id="bookSelectOneRadio" required="true"
requiredMessage="Please choose a book, then press Submit"
value="#{selectionsBean.selectedBook}">
<f:selectItem itemValue="java" itemLabel="Java How to Program"/>
<f:selectItem itemValue="cpp" itemLabel="C++ How to Program"/>
<f:selectItem itemValue="iphone"
itemLabel="iPhone for Programmers: An App-Driven Approach"/>
<f:selectItem itemValue="android"
itemLabel="Android for Programmers: An App-Driven Approach"/>
</h:selectOneRadio>
<p><h:commandButton value="Submit"/></p>
</h:form>
<p><h:outputLink value="cart.xhtml">
Click here to view your shopping cart
</h:outputLink></p>
</h:body>
</html>

每当我编译时,都会出现此错误:

/index.xhtml:类“sessiontracking.SelectionsBean”没有属性“Size”。

我有点卡住了,在代码中看不到如何解决。

java xhtml
© www.soinside.com 2019 - 2024. All rights reserved.