Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.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 2 如何将值从一个bean传递到另一个bean?(@ManagedProperty-help)_Jsf 2_Primefaces - Fatal编程技术网

Jsf 2 如何将值从一个bean传递到另一个bean?(@ManagedProperty-help)

Jsf 2 如何将值从一个bean传递到另一个bean?(@ManagedProperty-help),jsf-2,primefaces,Jsf 2,Primefaces,我正在使用PrimeFace4.0、JSF2.2、Java和XHTML。我有一个字符串值需要从一个bean传递到另一个bean。在一个XHTML页面上选择该值,当用户单击选择时,将启动一个新网页。我可以从服务器输出中看到,第一个bean(TestSelectBean)成功地收集了该值,但我似乎无法将其放入下一个bean(TargetBeanFranz)中。当字符串硬编码到bean中时,它工作正常。然而,当我尝试使用托管属性根据用户输入调用它时,我在试图使用它的代码行(85)处得到一个空指针 第一

我正在使用PrimeFace4.0、JSF2.2、Java和XHTML。我有一个字符串值需要从一个bean传递到另一个bean。在一个XHTML页面上选择该值,当用户单击选择时,将启动一个新网页。我可以从服务器输出中看到,第一个bean(TestSelectBean)成功地收集了该值,但我似乎无法将其放入下一个bean(TargetBeanFranz)中。当字符串硬编码到bean中时,它工作正常。然而,当我尝试使用托管属性根据用户输入调用它时,我在试图使用它的代码行(85)处得到一个空指针

第一个HTML:testselect.xhtml

//irrelevant code
<p:layoutUnit position="center">
                    <h:form>
                        <p:selectOneRadio id="Test"  value="#{testSelectBean.selectedNameOfExperiments}" >
                            <f:selectItems id="selectedNameOfExperiments" value="#{testSelectBean.nameofexperiments}" var="entry" itemValue="#{entry.key}" itemLabel="#{entry.value}" />
                        </p:selectOneRadio>

                        <p:commandLink id="nonAjax"  action="open" actionListener="#{testSelectBean.getParameters}" style="margin-right:20px;" ajax="false">
                            <h:outputText value="Submit" />
                        </p:commandLink>

                    </h:form>
                </p:layoutUnit>
但是,除非将
实验
硬编码到TargetBeanFranz中,否则XHTMLfranzwpoup不会启动

//irrelevant code
<p:layoutUnit position="center">

                    <p:panelGrid columns="2" style=" font-size: 14">
                        <h:outputLabel  value="Description" style="font-size: 20"/>
                        <h:outputText value="Neutron-induced production of protons, deuterons and tritons in Copper and Bismuth."/>

                        <h:outputLabel value="Reference" style="font-size: 20"/>
                        <h:outputText value="Nuclear Physics A510 (1990) 774-802 (Franz, et. al.)"/>
                    </p:panelGrid>

                    <form action="http://localhost:8080/primefaces/faces/handle.xhtml" method="get" name="login" onsubmit="process();
                            return false;" style="overflow-y: scroll">
                        Click to display chart data in a table.<br />
                        <input type="submit" value="Display"/>
                    </form>

                    <h:form>

                        <h:panelGrid columns="4" cellpadding="5">
                            <p:selectCheckboxMenu id="menu1" value="#{targetBeantFranz.selectedTargets}" label="Targets" filter="true" filterMatchMode="startsWith"
                                                  panelStyle="width:220px">
                                <f:selectItems value="#{targetBeantFranz.targets}" />
                            </p:selectCheckboxMenu>
                            <p:selectCheckboxMenu id="menu2" value="#{targetBeantFranz.selectedSecondaries}" label="Secondaries" filter="true" filterMatchMode="startsWith"
                                                  panelStyle="width:220px">
                                <f:selectItems value="#{targetBeantFranz.secondaries}" />
                            </p:selectCheckboxMenu>
                            <p:selectCheckboxMenu id="menu3" value="#{targetBeantFranz.selectedReactions}" label="Reactions" filter="true" filterMatchMode="startsWith"
                                                  panelStyle="width:220px">
                                <f:selectItems value="#{targetBeantFranz.reactions}" />
                            </p:selectCheckboxMenu>
                            <p:selectCheckboxMenu id="menu4" value="#{targetBeantFranz.selectedBeamEnergies}" label="Beam Energy" filter="true" filterMatchMode="startsWith"
                                                  panelStyle="width:220px">
                                <f:selectItems value="#{targetBeantFranz.beamenergies}" />
                            </p:selectCheckboxMenu>

                        </h:panelGrid>

                        <p:panel header="Resulting Plots"> 



                            <p:commandLink id="nonAjax" actionListener="#{targetBeantFranz.getParameters}" style="margin-right:20px;" ajax="false">
                                <button id="change">Submit Query</button>
                            </p:commandLink>

                            <div id="container" style="min-width: 310px; margin: 0 auto"></div>
                        </p:panel>
                    </h:form>

                </p:layoutUnit>
//more irrelevant 

我认为您出现问题的原因是,您的命令链接中的操作侦听器的签名不匹配

bean中的方法必须是以下类型:

public void getParameters(javax.faces.ActionEvent event) {
   .....
}
如果您使用的是JavaEE7,那么还应该避免包javax.faces.bean中的注释,因为它们已被放弃。使用CDI beans,可以让生活更轻松:

@Named
@SessionScoped
public class TestSelectBean {
 ...
}

@Named
@SessionScoped  
public class TargetBeanFranz {

    @Inject
    private TestSelectBean testSelectBean;

    ....
}
有关CDI主题的更多信息,请参见此处:

add process=“@name\u of_your\u form”在命令链接中单击以转到下一页。我认为您没有处理该表单,这就是为什么在访问第二个bean时该属性为null
public void getParameters(javax.faces.ActionEvent event) {
   .....
}
@Named
@SessionScoped
public class TestSelectBean {
 ...
}

@Named
@SessionScoped  
public class TargetBeanFranz {

    @Inject
    private TestSelectBean testSelectBean;

    ....
}