简体   繁体   English

JSF访问Bean类中的html元素值

[英]JSF access html element value in bean class

I have a JSF application in which I have a combo box. 我有一个JSF应用程序,其中有一个组合框。

<h:selectOneMenu id="collectorType"
                           value="#{activityDataSource.object.type}"
                           rendered="#{empty activityDataSource.object.id}"
                           disabled="#{!sp:hasRight(facesContext, 'ManageApplication')}"
                           readonly="#{!sp:hasRight(facesContext, 'ManageApplication')}"
                           onchange="$('editForm:selectTypeButton').click();">
             <f:ajax event="change" 
                    execute="@this" 
                    render="dsTransformationRule dsCorrelationRule"
                    listener="#{activityDataSource.handleCollectorTypeChange}" />
            <f:selectItem itemValue="" itemLabel="#{msgs.select_collector_type}"/>
            <f:selectItems value="#{activityDataSource.collectorTypes}"/>
          </h:selectOneMenu>

And I am getting selected value of that combo box in bean class like: 我在bean类中获得该组合框的选定值,例如:

public void setSelectedTransformationRule(String transformationRule)
        throws GeneralException {
    String collectorType = (String) getRequestParam().get("editForm:collectorType");
}

And I am successful in doing so. 我这样做很成功。 I am calling this method through ajax onchage event of combobox. 我通过combobox的ajax onchage事件调用此方法。

But if I try to get same combo box value in a different method i get null value. 但是,如果我尝试用不同的方法来获得相同的组合框值,则会得到空值。

public void handleCollectorTypeChange() throws GeneralException {
    String collectorType = (String) getRequestParam().get("editForm:collectorType");
}

Any help ! 任何帮助!

Because Process Events happens before Update Model Values you can retrieve the value from the component, from the UIViewRoot like this: 因为Process Events发生在Update Model Values之前,所以您可以从组件中从UIViewRoot检索值,如下所示:

HtmlSelectOneMenu collectorTypeSelectMenu = (HtmlSelectOneMenu) FacesContext.getCurrentInstance().getViewRoot().findComponent("editForm:collectorType");
String collectorType = (String) collectorTypeSelectMenu.getValue();

try put the attributes process and partialSubmit in your ajax call with the values you need process like this: 尝试将属性process和partialSubmit放入您的ajax调用中,并使用所需的值,如下所示:

<f:ajax event="change" 
   execute="@this" 
   render="dsTransformationRule dsCorrelationRule"
   process="@this, collectorType"
   partialSubmit="true"
   listener="#{activityDataSource.handleCollectorTypeChange}" />

In the process atrribute you can put all ids you need to process with the updated values (like you see in the screen. 在处理过程中,您可以将需要处理的所有ID放入更新后的值中(就像在屏幕上看到的那样)。

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

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