Richard Chase
Richard Chase

Reputation: 407

JSF selectOneRadio function return value as itemValue. For loop to create selectItems

I have 2 questions here for the code below.

The first one is, how can I have an itemValue in selectItem for my selectOneRadio as a return value from a function? I know this is working for a normal itemValue, if I put just "0", it shows up on the next page, but any of the getCaseValueByIndex() returns nothing.

My second question is, instead of listing each selectItem one by one, can i dynamically create them? What I was thinking is creating a function called getCasesCount which would return the size of the cases array (in this case, its 6) and then run a for loop like so:

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

So then what would happen is that would create the same code I have but dynamically so that If i added or removed items in my LinkedHashMap, the code would reflect those changes rather than crashing for an outofbounds or null pointer error.

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

    }
}

Upvotes: 0

Views: 2152

Answers (1)

Tushee
Tushee

Reputation: 251

You can create an object which holds your key and value pairs.

After that you can use a list of these objects in your bean and use the following code in the xhtmls:

<h:selectOneRadio value="#{CustomBuild.chosenCase}">
    <f:selectItems value="#{CustomBuild.getCases}" var="case" itemValue="#{case.value}" itemLabel="#{case.key}" /> 
</h:selectOneRadio>

Note: if you use an object in selectItem's value, you will need a converter, and to set the proper value you will need a proper equals in your bean.

If you want to stick with the hashmap, here a possible solution for you:

<h:selectOneRadio value="#{CustomBuild.chosenCase}">
    <f:selectItems value="#{CustomBuild.cases.keySet()}" var="key" itemValue="#{CustomBuild.getValueByKez(key)}" itemLabel="#{key}" /> 
</h:selectOneRadio>

Upvotes: 1

Related Questions