JSF action method with variable parameter [duplicate]

送分小仙女□ 提交于 2019-12-10 19:12:34

问题


How do I call method with variable parameters in JSF?

I tried something like this:

<h:commandButton value="Send" action="#{myBean.checkPIN(someOtherBean.PIN)}" />

However, this doesn't work.


回答1:


If you are using EL 2.2+, it's possible.

If you are using older version ot EL, you can use do the following:

<h:commandButton value="Send" action="#{myBean.checkPIN}" />
   <f:param name="parameter" value="123" />
</h:commandButton>

In the managed bean you can retrieve it like:

public void checkPIN() {
   ...
   Map<String, String> parameterMap = (Map<String, String>) externalContext.getRequestParameterMap();
   String param = parameterMap.get("parameter");
   ...
}



回答2:


Yes it is possible if you are using > EL 2.2 which is part of Servlet 3.0.

See @BalusC's suggetions here Invoke direct methods or methods with arguments / variables / parameters in EL




回答3:


It does work with EL 2.2. Which is probably the version you're using, since you're using JSF 2 (Even though it might not be the case).

You can do a very simple test. You can have an OtherMB such as this:

@ManagedBean(name = "otherMB")
public class OtherMB{

    public String getValue(){
        return "Other Managed Bean Value";
    }

}

And a method in your MainMB like this:

@ManagedBean(name = "mainMB")
public class MainMB{

    public void method(String str){
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(str));
    }

}

And in your xhtml you can just invoke the function using a button:

<h:commandButton action="#{mainMB.method(otherMB.value)}" value="Click Me!" />

Just remember that the h:commandButton needs to be inside an h:form, and that you need a component to show the message. Or you can just change the implementation to print the message in the console



来源:https://stackoverflow.com/questions/16485487/jsf-action-method-with-variable-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!