对于下面的代码,我有两个问题。
第一个是,我怎么能有一个itemValue在selectItem为我的selectOneRadio从一个函数的返回值?我知道这是一个正常的itemValue工作,如果我把"0",它显示在下一页,但任何getCaseValueByIndex()返回什么。
我的第二个问题是,我可以动态地创建它们,而不是一个接一个地列出每个selectItem吗?我想的是创建一个名为getCasesCount的函数,该函数将返回case数组的大小(在本例中为6),然后运行for循环,如下所示:
for (int i = 0; i < casesCount; i++) {
<f:selectItem itemValue="#{CustomBuild.getCaseValueByIndex(i)}" itemLabel="# {CustomBuild.getCaseKey(i)}"/>
}
那么会发生什么是,将创建相同的代码,我有,但动态地,如果我添加或删除项目在我的LinkedHashMap,代码将反映这些变化,而不是崩溃的出界或空指针错误。
index.xhtml
<!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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
<title>Jadestar's PC Solutions</title>
</head>
<body>
<h4>Please choose your components.</h4>
<h4>To be eligible for the system builder's discount,
you must select at least <span class ='highlight'> 1 </span> component from each category.</h4>
<br></br>
#{CustomBuild.initialize()}
<h3>Computer Case</h3>
<h:form>
<h:selectOneRadio value="#{CustomBuild.chosenCase}">
<f:selectItem itemValue="#{CustomBuild.getCaseValueByIndex(0)}" itemLabel="#{CustomBuild.getCaseKeyByIndex(0)}"/>
<f:selectItem itemValue="#{CustomBuild.getCaseValueByIndex(1)}" itemLabel="#{CustomBuild.getCaseKeyByIndex(1)}"/>
<f:selectItem itemValue="#{CustomBuild.getCaseValueByIndex(2)}" itemLabel="#{CustomBuild.getCaseKeyByIndex(2)}"/>
<f:selectItem itemValue="#{CustomBuild.getCaseValueByIndex(3)}" itemLabel="#{CustomBuild.getCaseKeyByIndex(3)}"/>
<f:selectItem itemValue="#{CustomBuild.getCaseValueByIndex(4)}" itemLabel="#{CustomBuild.getCaseKeyByIndex(4)}"/>
<f:selectItem itemValue="#{CustomBuild.getCaseValueByIndex(5)}" itemLabel="#{CustomBuild.getCaseKeyByIndex(5)}"/>
</h:selectOneRadio>
<br></br>
<h:commandButton id="submit" value="submit" action="responsePage" />
</h:form>
</body>
</html>
responsePage.xhtml
<!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://xmlns.jcp.org/jsf/html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
<title>Response</title>
</head>
<body>
<h4><h:outputText escape="false" value="#{CustomBuild.chosenCase}"/></h4>
<h:form prependId="false">
<h:commandButton id="backButton" value="Back" action="index" />
</h:form>
</body>
</html>
CustomBuild.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Part1;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
/**
*
* @author Administrator
*/
@ManagedBean(name = "CustomBuild")
@SessionScoped
public class CustomBuild implements Serializable {
LinkedHashMap <String, String> cases = new LinkedHashMap<String, String>();
String chosenCase;
public String getChosenCase() {
return chosenCase;
}
public void setChosenCase(String chosenCase) {
this.chosenCase = chosenCase;
}
public float getCaseValueByKeyFloat(String key) {
float caseValueByValue = Float.parseFloat(cases.get(key));
return caseValueByValue;
}
public String getCaseKeyByIndex(int index) {
Object newKey = cases.keySet().toArray()[index];
//String tempKey = getCaseValueByIndex(index);
//String newKey = getCaseKeyByValue(tempKey);
return newKey.toString();
}
public String getCaseValueByIndex(int index) {
String caseValue = (new ArrayList<String>(cases.values())).get(index);
return caseValue;
}
public void initialize() {
cases.put("59.95" ,"Eleo 500 $59.95");
cases.put("79.95" ,"Eleo 700 $79.95");
cases.put("99.95" ,"Star Maker $99.95");
cases.put("104.95" ,"Anzooc 1200 $104.95");
cases.put("119.95" ,"Eleo 900 $119.95");
cases.put("139.95" ,"Criticase 1000 $139.95");
}
public CustomBuild() {
System.out.println("Custom Computer");
}
}
你可以创建一个对象来保存你的键和值对。
之后可以在bean中使用这些对象的列表,并在xhtml中使用以下代码:<h:selectOneRadio value="#{CustomBuild.chosenCase}">
<f:selectItems value="#{CustomBuild.getCases}" var="case" itemValue="#{case.value}" itemLabel="#{case.key}" />
</h:selectOneRadio>
注意:如果你在selectItem的值中使用一个对象,你需要一个转换器,为了设置合适的值,你需要在bean中使用一个合适的等号。
如果你想坚持使用hashmap,这里有一个可能的解决方案:
<h:selectOneRadio value="#{CustomBuild.chosenCase}">
<f:selectItems value="#{CustomBuild.cases.keySet()}" var="key" itemValue="#{CustomBuild.getValueByKez(key)}" itemLabel="#{key}" />
</h:selectOneRadio>