使用Java的购物车应用程序

xyhw6mcr  于 2023-08-02  发布在  Java
关注(0)|答案(1)|浏览(125)

我正在做一个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' ?>
<!-- cart.xhtml -->
<!-- Display the user's shopping cart -->
<!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>Shopping Cart</title>
</h:head>
<h:body>
<h1>Shopping Cart</h1>
<h:dataTable value="#{cartBean.cart}" var="item">
<h:column>
<f:facet name="header">Book Title</f:facet>
#{item.title}
</h:column>
<h:column>
<f:facet name="header">Price</f:facet>
#{item.price}
</h:column>
</h:dataTable>
<p>Total price: #{cartBean.totalPrice}</p>
<p><h:outputLink value="index.xhtml">
Click here to continue shopping
</h:outputLink></p>
</h:body>
</html>

字符串
每当我编译我这个错误:
/index.xhtml:类“sessiontracking.SelectionsBean”没有属性“Size”。
我有点卡住了,在代码中看不到如何解决。

wpx232ag

wpx232ag1#

在你的index.html中,有问题的一行是:

<p>You have selected #{selectionsBean.Size} book(s).

字符串

selectionsBean.Size将抛出错误,因为您的SelectionsBean类中没有定义名为“Size”的属性。

看一下这个句子,我假设你是想显示客户所做的选择的数量,所以用途:

<p>You have selected #{selectionsBean.selections.size()} book(s).


为什么会成功?在SelectionsBean类中,你定义了一个名为“selections”的属性:

private List<String> selections;


“selections”是一个List类型,它有内置的方法,比如Size(),可以返回列表中元素的数量,所以你可以使用它。确保检查是否为null,因为您的选择变量仅在构造函数SelectionsBean()中初始化。

相关问题