Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf h:selectOneRadio组嵌套ui中的问题:重复元素_Jsf_Jsf 2.3 - Fatal编程技术网

Jsf h:selectOneRadio组嵌套ui中的问题:重复元素

Jsf h:selectOneRadio组嵌套ui中的问题:重复元素,jsf,jsf-2.3,Jsf,Jsf 2.3,我目前正在尝试实现一个动态调查页面,其中包含用户定义的页面、部分和问题(大型应用程序的一部分)。 用户可以定义不同的问题类型,这将呈现不同的组件(收音机、textarea、textfield、selectionlist等)。 该应用程序目前部署在Wildfly 16/Java 8/JSF 2.3/Servlet 4中 由于用户可以为单选按钮定义一组特定的值和相关图像,因此我需要自定义单选按钮输出。因此,我的选择是使用selectoneradio中提供的新group属性。 在多级ui:repeat

我目前正在尝试实现一个动态调查页面,其中包含用户定义的页面、部分和问题(大型应用程序的一部分)。 用户可以定义不同的问题类型,这将呈现不同的组件(收音机、textarea、textfield、selectionlist等)。 该应用程序目前部署在Wildfly 16/Java 8/JSF 2.3/Servlet 4中

由于用户可以为单选按钮定义一组特定的值和相关图像,因此我需要自定义单选按钮输出。因此,我的选择是使用
selectoneradio
中提供的新
group
属性。 在多级
ui:repeat
中使用此新属性会导致行为不稳定

为了说明问题,简化并创建了以下示例

课程

package test;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named("test")
@ViewScoped
public class Test implements Serializable
{
    private static final long serialVersionUID = 1L;

    private static Map<String, Map<String, String>> questionMap = new TreeMap<>();

    private static Map<String, String> fixedValuesMap = new TreeMap<>();

    private Map<String, String> answerMap = new TreeMap<>();

    private List<String> sections = Arrays.asList("Section_1", "Section_2", "Section_3", "Section_4");

    static
    {
        fixedValuesMap.put("1", "Value 1");
        fixedValuesMap.put("2", "Value 2");
        fixedValuesMap.put("3", "Value 3");
        fixedValuesMap.put("4", "Value 4");
        fixedValuesMap.put("5", "Value 5");

        Map<String, String> sec1questions = new TreeMap<>();
        sec1questions.put("1", "Question 1");
        sec1questions.put("2", "Question 2");
        sec1questions.put("3", "Question 3");
        questionMap.put("Section_1", sec1questions);

        Map<String, String> sec2questions = new TreeMap<>();
        sec2questions.put("4", "Question 4");
        questionMap.put("Section_2", sec2questions);

        Map<String, String> sec3questions = new TreeMap<>();
        sec3questions.put("5", "Question 5");
        questionMap.put("Section_3", sec3questions);

        Map<String, String> sec4questions = new TreeMap<>();
        sec4questions.put("6", "Question 6");
        questionMap.put("Section_4", sec4questions);
    }

    public Test()
    {
    }

    @PostConstruct
    private void init()
    {
        answerMap.put("1", null);
        answerMap.put("2", null);
        answerMap.put("3", null);
        answerMap.put("4", null);
        answerMap.put("5", null);
        answerMap.put("6", null);
    }

    public String getQuestionType(String index)
    {
        switch(index)
        {
            case "1":
                return "RADIO_BUTTON";
            case "2":
                return "RADIO_BUTTON";
            case "3":
                return "RADIO_BUTTON";
            case "4":
                return "TEXT_AREA";
            case "5":
                return "RADIO_BUTTON";
            case "6":
                return "FREE_TEXT";
            default:
                return "FREE_TEXT";
        }
    }

    public Map<String, String> getQuestions(String section)
    {
        return questionMap.get(section);
    }

    public Map<String, String> getAnswerMap()
    {
        return answerMap;
    }

    public Map<String, String> getFixedValues()
    {
        return fixedValuesMap;
    }

    public List<String> getSections()
    {
        return sections;
    }

    public void changeRadio(String questionKey, String questionValue)
    {
        answerMap.put(questionKey, questionValue);
    }

    public String submit()
    {
        for(Map.Entry<String, String> entry : answerMap.entrySet())
            System.out.println("ENTRY: " + entry.getKey() + " - " + entry.getValue());

        return null;
    }
}

封装测试;
导入java.io.Serializable;
导入java.util.array;
导入java.util.List;
导入java.util.Map;
导入java.util.TreeMap;
导入javax.annotation.PostConstruct;
导入javax.faces.view.ViewScoped;
导入javax.inject.Named;
@命名(“测试”)
@视域
公共类测试实现可序列化
{
私有静态最终长serialVersionUID=1L;
私有静态映射questionMap=newtreemap();
私有静态映射fixedValuesMap=newtreemap();
私有映射answerMap=newtreemap();
私有列表sections=Arrays.asList(“Section_1”、“Section_2”、“Section_3”、“Section_4”);
静止的
{
固定价值对应卖出价(“1”、“价值1”);
固定价值对应卖出价(“2”、“价值2”);
固定价值对应卖出价(“3”、“价值3”);
固定价值对应卖出价(“4”、“价值4”);
固定价值对应卖出价(“5”、“价值5”);
Map sec1questions=newtreemap();
第1条问题。提出(“1”、“问题1”);
第二部分问题。提出(“2”,“问题2”);
第1条问题。付诸表决(“3”,“问题3”);
问题图。付诸表决(“第1节”,第1条问题);
Map sec2questions=newtreemap();
第二部分问题。提出(“4”,“问题4”);
问题图。付诸表决(“第2节”,第2个问题);
Map sec3questions=newtreemap();
第3条问题。付诸表决(“5”,“问题5”);
问题图。付诸表决(“第三节”,第三节问题);
Map sec4questions=newtreemap();
第4条问题。付诸表决(“6”,“问题6”);
问题图。付诸表决(“第4节”,第4条问题);
}
公开考试()
{
}
@施工后
私有void init()
{
answerMap.put(“1”,空);
answerMap.put(“2”,空);
answerMap.put(“3”,空);
answerMap.put(“4”,空);
answerMap.put(“5”,空);
answerMap.put(“6”,空);
}
公共字符串getQuestionType(字符串索引)
{
开关(索引)
{
案例“1”:
返回“单选按钮”;
案例“2”:
返回“单选按钮”;
案例“3”:
返回“单选按钮”;
案例“4”:
返回“文本区域”;
案例“5”:
返回“单选按钮”;
案例“6”:
返回“自由文本”;
违约:
返回“自由文本”;
}
}
公共地图问题(字符串部分)
{
返回问题图。获取(部分);
}
公共地图getAnswerMap()
{
返回应答图;
}
公共映射getFixedValues()
{
返回固定值映射;
}
公共列表getSections()
{
回流段;
}
public void changeRadio(字符串questionKey、字符串questionValue)
{
输入(questionKey,questionValue);
}
公共字符串提交()
{
对于(Map.Entry:answerMap.entrySet())
System.out.println(“条目:”+ENTRY.getKey()+“-”+ENTRY.getValue());
返回null;
}
}
XHTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
        xmlns:f="http://xmlns.jcp.org/jsf/core"
        xmlns:h="http://xmlns.jcp.org/jsf/html"
        xmlns:a="http://xmlns.jcp.org/jsf/passthrough">

    <h:head>
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
        <meta name="description" content="My Test"/>

        <link rel="stylesheet" href="#{request.contextPath}/resources/bootstrap/css/bootstrap.min.css" />
        <title>My Test</title>
    </h:head>

    <h:body>

        <div class="container-fluid">

            <div class="row">

                <main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">

                    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                        <h1 class="h2">UI Repeat Test</h1>
                    </div>

                    <h:form role="form" prependId="false">

                        <div class="row mx-2">

                            <ui:repeat var="sec" value="#{test.sections}">

                                <div class="col-12">

                                    <ui:repeat var="question" value="#{test.getQuestions(sec)}">

                                        <div>
                                            <div class="col-12">
                                                <div class="form-group">
                                                    <label for="answer">#{question.key} - #{question.value}</label>

                                                    <div class="col-12">

                                                        <h:panelGroup rendered="#{test.getQuestionType(question.key) eq 'FREE_TEXT'}" layout="block">
                                                            <h:inputText value="#{test.answerMap[question.key]}" styleClass="form-control"/>
                                                        </h:panelGroup>

                                                        <h:panelGroup rendered="#{test.getQuestionType(question.key) eq 'TEXT_AREA'}"  layout="block">
                                                            <h:inputTextarea value="#{test.answerMap[question.key]}" styleClass="form-control" rows="2" />
                                                        </h:panelGroup>

                                                        <h:panelGroup rendered="#{test.getQuestionType(question.key) eq 'RADIO_BUTTON'}"  layout="block">

                                                            <table class="radio-label checkbox">
                                                                <tbody>
                                                                    <tr>
                                                                        <ui:repeat var="item" value="#{test.fixedValues.entrySet()}">
                                                                            <td class="text-center grid-margin">    
                                                                                <h:selectOneRadio group="#{question.key}" value="#{test.answerMap[question.key]}" layout="lineDirection" styleClass="checkbox"  >
                                                                                        <f:selectItem itemValue="#{item.key}" itemLabel="#{item.value}" />
                                                                                </h:selectOneRadio>
                                                                            </td>   
                                                                        </ui:repeat>
                                                                    </tr>
                                                                </tbody>
                                                            </table>

                                                        </h:panelGroup>

                                                    </div>  
                                            </div>
                                            </div>
                                        </div>

                                    </ui:repeat>

                                </div>

                            </ui:repeat>

                        </div>

                        <div class="row mx-2 my-2">
                            <div class="col-12">
                                <h:commandButton value="Test Me" type="submit" action="#{test.submit}" styleClass="btn btn-primary" />
                            </div>
                        </div>

                    </h:form>

                </main>

            </div>

        </div>

        <script src="#{request.contextPath}/resources/jquery/jquery.slim.min.js"></script>
        <script src="#{request.contextPath}/resources/bootstrap/js/bootstrap.bundle.min.js"></script>

    </h:body>

</html>

我的测试
用户界面重复测试
#{question.key}-#{question.value}
<table class="radio-label checkbox">
  <tbody>
    <tr>
      <ui:repeat var="item" value="#{test.fixedValues.entrySet()}">
        <td class="text-center grid-margin">    
          <h:selectOneRadio group="#{question.key}" value="#{test.answerMap[question.key]}" layout="lineDirection" styleClass="checkbox"  >
            <f:selectItem itemValue="#{item.key}" itemLabel="#{item.value}" />
          </h:selectOneRadio>
        </td>   
      </ui:repeat>
    </tr>
  </tbody>
</table>
<h:selectOneRadio value="#{test.answerMap[question.key]}" layout="lineDirection" >
  <f:selectItems value="#{test.fixedValues.entrySet()}" var="item" itemLabel="#{item.key}" itemValue="#{item.value}" />
</h:selectOneRadio>
package my.pkg;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class MyBean {
    private List<String> items;

    private List<Number> numbers;

    private Map<String, Object> mapVal;

    @PostConstruct
    public void init() {
        mapVal = new LinkedHashMap<>();
        items = new ArrayList<>();
        numbers = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            items.add("group_" + i);
            numbers.add(i);
        }
    }

// getters/setters ...

}
<!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:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">

<h:head />

<h:body>

    <h:form id="frm1">

        <ui:repeat var="grp" value="#{myBean.items}">

            <ui:repeat var="num" value="#{myBean.numbers}" varStatus="stat">

                <h:selectOneRadio id="radioButton" value="#{myBean.mapVal[grp]}"
                    group="myGroup_#{grp}">
                    <f:selectItem itemValue="#{num}" itemLabel="#{grp}: #{num}" />
                </h:selectOneRadio>

                <br />

            </ui:repeat>

            <hr />
        </ui:repeat>

        <h:commandButton value="submit" />

        <h:outputText value="myBean.mapVal: #{myBean.mapVal}" />
    </h:form>

</h:body>
</html>
myBean.mapVal: {group_0=0, group_1=1, group_2=2}
myBean.mapVal: {group_2=2}
myBean.mapVal: {group_0=0, group_1=0, group_2=0}
myBean.mapVal: {group_0=1, group_1=1, group_2=1}
myBean.mapVal: {group_0=2, group_1=2, group_2=2}
{group_0=0, group_1=1, group_2=2}
1)
expected: {group_0=0, group_1=0, group_2=0}
but is: {group_2=0}

2)
expected: {group_0=0, group_1=0, group_2=2}
but is: {group_1=0, group_2=2}

3)
expected: {group_0=1, group_1=1, group_2=0}
but is: {group_1=1, group_2=0}
    <ui:repeat var="grp" value="#{myBean.items}">
    <c:forEach var="grp" items="#{myBean.items}">