Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass url parameters to JSF?

Tags:

jsf

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&param2=value2

Could someone point me at the right direction with this?

like image 479
Ben Avatar asked Mar 16 '10 16:03

Ben


1 Answers

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}")

like image 75
BalusC Avatar answered Sep 19 '22 15:09

BalusC