首页 > 解决方案 > 为什么单独的 RequestScoped bean 没有注入到 JSF commandButton 动作调用的 bean 中?

问题描述

出于什么原因,使用相同范围的注入辅助 bean 不包含来自提交的表单数据?使用来自(JSF)commandButton 动作的参数的显式方法调用..

我已经求助于在调用 bean 中包含相同的参数,然后它就可以正常工作了。但这很烦人,因为我宁愿 A. 不要像疯了一样传递东西, B. 在几个地方重用一组特定的设置。

运行我的应用程序的环境是 Wildfly 11,以下问题发生在 Windows 机器上,但将在 Linux 上的生产环境中运行。

我的网页和相关 bean 的相关代码:

<ui:composition template="/WEB-INF/template.xhtml" ...
                xmlns:h="http://xmlns.jcp.org/jsf/html">
...
<h:form>
<h:selectBooleanCheckbox id="box1" value="#{mainBean.someBoolean}"/>
<h:selectBooleanCheckbox id="box2" value="#{secondBean.someBoolean}"/>
<h:commandButton id="cmdButton" value="Btn Text" action="#{mainBean.actionMethod(sessionBean.value)}"/>
</h:form>
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
...
@Named
@RequestScoped
public class MainBean implements Serializable {
    @Inject
    private SecondBean secondBean;

    private boolean someBoolean;
    // with getter and setter

    public void actionMethod(Object value) {
        // In here, someBoolean and value are properly set, while secondBean.isSomeBoolean() always returns default/false regardless of the checkbox on the page.
    }
...
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import java.io.Serializable;
...
@Named
@RequestScoped
public class SecondBean implements Serializable {
    private boolean someBoolean;
    // with getter and setter
...

为什么一个 bean 正确地填充了表单数据而不是另一个?

标签: jsfcdirequestscope

解决方案


推荐阅读