How to pass url parameters to JSF?

前端 未结 1 2009
礼貌的吻别
礼貌的吻别 2020-12-30 08:13

I haven\'t managed to find a way to pass parameters to JSF pages through URL parameters.

http://www.example.com/jsfApp.jsp?param1=value1¶m2=value2


        
相关标签:
1条回答
  • 2020-12-30 08:36

    As you're using JSPs, I'll assume that you're using JSF 1.x.

    To create a link with query parameters, use h:outputLink with f:param:

    <h:outputLink value="page.jsf">
        <f:param name="param1" value="value1" />
        <f:param name="param2" value="value2" />
    </h:outputLink>
    

    The value can be set dynamically with help of EL.

    To set them in the managed bean automagically, you need to define each as managed-property in faces-config.xml:

    <managed-bean>
        <managed-bean-name>bean</managed-bean-name>
        <managed-bean-class>com.example.Bean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
        <managed-property>
            <property-name>param1</property-name>
            <value>#{param.param1}</value>
        </managed-property>
        <managed-property>
            <property-name>param2</property-name>
            <value>#{param.param2}</value>
        </managed-property>
    </managed-bean>
    

    The imlicit EL variable #{param} refers to the request parameter map as you know it from the Servlet API. The bean should of course already have both the param1 and param2 properties with the appropriate getters/setters definied.

    If you'd like to execute some logic directly after they are been set, make use of the @PostConstruct annotation:

    @PostConstruct
    public void init() {
        doSomethingWith(param1, param2);
    }
    

    For more hints about passing parameters and that kind of stuff around in JSF, you may find this article useful.

    The JSF 2.x approach would be using either @ManagedProperty in the backing bean class, or <f:viewParam> in the target view. See also this question: ViewParam vs @ManagedProperty(value = "#{param.id}")

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