JSF selectOneRadio函数返回值为itemValue。用于循环创建selectItems

时间:2014-05-05 03:24:48

标签: java jsf facelets

我在这里有两个问题,代码如下。

第一个是,如何在selectItem中为selectOneRadio提供itemValue作为函数的返回值?我知道这适用于正常的itemValue,如果我只放" 0",它会出现在下一页,但任何getCaseValueByIndex()都不会返回任何内容。

我的第二个问题是,我可以动态创建它们,而不是逐个列出每个selectItem吗?我在想的是创建一个名为getCasesCount的函数,该函数将返回案例数组的大小(在本例中为6),然后运行for循环,如下所示:

for (int i = 0; i < casesCount; i++) {
    <f:selectItem itemValue="#{CustomBuild.getCaseValueByIndex(i)}" itemLabel="#  {CustomBuild.getCaseKey(i)}"/>
}

那么接下来会发生的事情是创建相同的代码,但是动态地创建,如果我在LinkedHashMap中添加或删除了项目,代码将反映这些更改,而不是因为outofbounds或null指针错误而崩溃。

的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");

    }
}

1 个答案:

答案 0 :(得分:1)

您可以创建一个包含键和值对的对象。

之后,您可以在bean中使用这些对象的列表,并在xhtmls中使用以下代码:

<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>