tags:

views:

206

answers:

1

Hi,

I haven't managed to find a way to pass parameters to JSF/JSP web pages through url parameters.

http://.../jsfApp.jsp?param1=value1&param2=value2

Could someone point me at the right direction with this?

Thanks!

+3  A: 

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.

BalusC
Great! Thanks!!
Ben
You're welcome.
BalusC