简体   繁体   English

h:selectBooleanCheckbox没有将结果传递给Map

[英]h:selectBooleanCheckbox is not passing the result to a Map

I want to pass check boxes to a backing CDI-Bean. 我想将复选框传递到支持的CDI-Bean。 The postTest.values are just a List of Longs. postTest.values只是一个Longs列表。

<h:form>
    <h:dataTable value="#{postTest.values}" var="val">
        <h:column>
            <h:outputLabel value="#{val}"/>
        </h:column>
        <h:column>
            <h:selectBooleanCheckbox value="#{postTest.checked[val]}"/>
        </h:column>
    </h:dataTable>
    <h:commandButton action="#{postTest.process}"/>
</h:form>

The action method should print out the checked values. 操作方法应打印出检查值。 But it is just empty. 但这只是空的。

@Named
@RequestScoped
public class PostTest {

    List<Long> values;
    Map<Long, Boolean> checked;

    ... 
    public String process() {
        logger.info(this.toString() + "Processing");
        for (Long l : checked.keySet()) {
            logger.info(this.toString() + "\t" + l + ". checked: " + checked.get(l));
        }

        return "index2";
    }
    ...
}

When I add logging to the getChecked() method, I can see, that it is just retrieved once per column and its content isn't changed at all. 当我将日志添加到getChecked()方法时,可以看到,每列只检索了一次日志,并且其内容完全没有改变。

The problem seem to be related to the point that the postTest.values is not initialized when the form passes the values. 问题似乎与表单传递值时未初始化postTest.values的问题有关。 Because if I initialize the postTest.values in the constructor (or a @PostConstruct) the checked items are passed correctly. 因为如果我在构造函数(或@PostConstruct)中初始化postTest.values ,则检查的项目将正确传递。

Why do I need to initialize the postTest.values at all after the POST request is executed? 为什么在执行POST请求后需要全部初始化postTest.values

Is there a way to prevent that? 有办法防止这种情况吗?

Or do I have another option? 还是我还有其他选择? Eg make sure that the postTest.values are initialized properly not using the constructor nor the @PostConstruct, cause I want to pass values to it before initialization (I tried listeners, but they don't seem to solve that). 例如,确保不使用构造函数或@PostConstruct正确初始化postTest.values ,这是因为我想在初始化之前将值传递给它(我尝试使用侦听器,但似乎无法解决问题)。

Thanks! 谢谢!

Tim 提姆

You can also use Myfaces CODI @ViewAccessScoped annotation if you don't want to play with @ConversationScoped and the start() and end() methods. 如果您不想使用@ConversationScoped以及start()end()方法,也可以使用Myfaces CODI @ViewAccessScoped批注。 it is a CDI extension that is kind of equivalent to @ViewScoped of JSF managed bean. 它是一个CDI扩展,有点@ViewScoped JSF托管bean的@ViewScoped I did not check the implementation of this annotation but I see many developers tell that it is even better than @ViewScoped annotation. 我没有检查此批注的实现,但是我看到许多开发人员告诉我它甚至比@ViewScoped批注还要好。

Just download Myfaces CODI and drop it in your classpath and use the annotation. 只需下载Myfaces CODI并将其放在您的类路径中并使用注释即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM