在Java中创建购物车。对使用Hashmap感到困惑
选择豆 包sessiontracking在Java中创建购物车。对使用Hashmap感到困惑,java,html,jsf,Java,Html,Jsf,选择豆 包sessiontracking import java.io.Serializable; import java.util.HashMap; import java.util.Set; import java.util.TreeSet; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean( name="selectionsBean" ) @SessionSco
import java.io.Serializable;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean( name="selectionsBean" )
@SessionScoped
public class SelectionsBean implements Serializable
{
// map of topics to book titles
private static final HashMap< String, Double > booksMap =
new HashMap< String, Double >();
double total;
// initialize booksMap
static
{
booksMap.put( "java", 19.99 );
booksMap.put( "cpp", 29.99 );
booksMap.put( "iphone",
32.99 );
booksMap.put( "android",
15.99 );
} // end static initializer block
// stores individual user's selections
private Set< String > selections = new TreeSet< String >();
private String selection; // stores the current selection
// return number of selections
public int getNumberOfSelections()
{
return selections.size();
} // end method getNumberOfSelections
// returns the current selection
public String getSelection()
{
return selection;
} // end method getSelection
// store user's selection
public void setSelection( String topic )
{
selection = booksMap.get( topic );
selections.add( selection );
} // end method setSelection
// return the Set of selections
public String[] getSelections()
{
return selections.toArray( new String[ selections.size() ] );
} // end method getSelections
} // end class SelectionsBean
index.html:
<!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>Topic Selection Page</title>
</h:head>
<h:body>
<h1>Welcome to Sessions!</h1>
<p>You have made #{selectionsBean.numberOfSelections} selection(s)
</p>
<h3>Make a Selection and Press Submit</h3>
<h:form>
<h:selectOneRadio id="topicSelectOneRadio" required="true"
requiredMessage="Please choose a book, then press Submit"
value="#{selectionsBean.selection}">
<f:selectItem itemValue="java" itemLabel="Java How to Program"/>
<f:selectItem itemValue="cpp" itemLabel="How to C++"/>
<f:selectItem itemValue="iphone" itemLabel="Learn All About iPhone"/>
<f:selectItem itemValue="android" itemLabel="Everything to learn Apps"/>
</h:selectOneRadio>
<p><h:commandButton value="Submit"/></p>
</h:form>
<p><h:outputLink value="recommendations.xhtml">
Click here for book recommendations
</h:outputLink></p>
</h:body>
</html>
建议.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Recommended Books</title>
</h:head>
<h:body>
<h1>Book Recommendations</h1>
<ul>
<ui:repeat value="#{selectionsBean.selections}" var="book">
<li>#{book}</li>
</ui:repeat>
</ul>
<p><h:outputLink value="index.xhtml">
Click here to choose another topic
</h:outputLink></p>
</h:body>
</html>
我试图做的是,当我运行index.html时,它将显示四本java书籍,用户可以像购物车一样选择和提交这些书籍,并在您结账时显示购物车和总价。我的问题是,使用hashmap,我试图将它的第二个值更改为double,但没有效果。我是否错误地使用了hashmap?将其第二个值更改为double,但没有效果。这是什么意思;博士,这是太多的代码了。把范围缩小到SSCCE,问题是什么?发生了什么你没有预料到的事?