我想创建一个应用程序,将书籍列表显示为 h:selectOneRadio 元素。当用户提交表单时,将用户的选择存储在 @SessionScoped 托管 bean 中。允许用户返回图书列表并进行其他选择。提供查看购物车的链接。在购物车页面上,显示用户所做的选择列表、每本书的价格以及购物车中所有书籍的总数。
我遇到的问题是在应用程序中,当用户点击提交并选择单选按钮时,会发生以下错误:
转换错误为“null Converter”设置值“java”。
这适用于选择的单选按钮。我尝试过更改列表,但我似乎无法确定错误在哪里。
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 Map<String, Double> pricesMap;
private List<String> selections;
public SelectionsBean() {
// initialize prices map
pricesMap = new HashMap<>();
pricesMap.put("Java How to Program", 100.00);
pricesMap.put("C++ How to Program", 100.00);
pricesMap.put("iPhone for Programmers: An App-Driven Approach", 65.00);
pricesMap.put("Android for Programmers: An App-Driven Approach", 40.00);
// 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;
}
}
<?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">
<h:head>
<title>Book Selection Page</title>
</h:head>
<h:body>
<h1>Welcome to the Bookstore!</h1>
<p>You have made #{selectionsBean.selections.size()} selection(s)
</p>
<h3>Make a Selection and Press Submit</h3>
<h:form>
<h:selectOneRadio id="bookSelectOneRadio" required="true"
requiredMessage="Please choose a book, then press Submit"
value="#{selectionsBean.selections}">
<f:selectItem itemValue="java" itemLabel="Java $100"/>
<f:selectItem itemValue="cpp" itemLabel="C++ $100"/>
<f:selectItem itemValue="iphone" itemLabel="iPhone $65"/>
<f:selectItem itemValue="android" itemLabel="Android $40"/>
</h:selectOneRadio>
<p><h:commandButton value="Submit"/> </p>
</h:form>
<p><h:outputLink value="cart.xhtml">
Click here to view Shopping Cart
</h:outputLink></p>
</h:body>
</html>
<?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">
<h:head>
<title>Shopping Cart</title>
</h:head>
<h:body>
<h1>Shopping Cart</h1>
<h:dataTable value="#{selectionsBean.selections}" 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: #{selectionsBean.totalPrice}</p>
<p><h:outputLink value="index.xhtml">
Click here to continue shopping
</h:outputLink></p>
</h:body>
</html>