Passing parameter to JSF action

前端 未结 1 1073
天命终不由人
天命终不由人 2020-12-03 09:19

I\'m using GlassFish 3.1, and trying to pass parameter to commandButton action. Following is my code:

beans.xml



        
相关标签:
1条回答
  • 2020-12-03 09:51

    The <f:param> sets a HTTP request parameter, not an action method parameter. To get it, you would need to use <f:viewParam> or @ManagedProperty. In this particular case, the latter is more suitable. You only have to replace CDI annotations by JSF annotations in order to get @ManagedProperty to work:

    @ManagedBean(name="bean")
    @RequestScoped
    public class ActionParam {
    
        @ManagedProperty("#{param.param}")
        private Integer param;
    
        public String submit() {
            System.out.println("Submit using value " + param);
            return null;
        }
    
    }
    

    When you're targeting a Servlet 3.0 container (Tomcat 7, Glassfish 3, JBoss AS 6, etc) with a web.xml whose <web-app> root declaration definies Servlet 3.0, then you should be able to just pass the parameter straight into the action method by EL as that's supported by EL 2.2 (which is part of Servlet 3.0):

    <h:commandButton id="actionButton" value="Submit"
        action="#{bean.submit(123)}">
    </h:commandButton>
    

    with

    public String submit(Integer param) {
        System.out.println("Submit using value " + param);
        return null;
    }
    

    If you target an old Servlet 2.5 container, then you should still be able to do this using JBoss EL. See also this answer for installation and configuration details: Invoke direct methods or methods with arguments / variables / parameters in EL

    0 讨论(0)
提交回复
热议问题