Badr Draifi
Badr Draifi

Reputation: 31

How to get a request parameter from URL in JSF 1.2?

I have a Spring + JSF 1.2 application which sends an email with a link to the user after subscription. The link contains a request parameter like:

www.myapp.com/register.jsf?var=1

How can I get this request parameter in my JSF backing bean?

Upvotes: 3

Views: 3620

Answers (1)

BalusC
BalusC

Reputation: 1109745

It's not clear if you're using Spring or JSF to manage your beans, but in standard JSF 1.2, you need to register it as <managed-property> of the <managed-bean> with a value of #{param.var} where var is the exact request parameter name:

<managed-bean>
    ...
    <managed-property>
        <property-name>var</property-name>
        <value>#{param.var}</value>
    </managed-property>
</managed-bean>

So if your bean has a property

private String var; // +setter (getter is not mandatory)

then you can access and process it in a @PostConstruct method:

@PostConstruct
public void init() {
    System.out.println("var is: " + var);
}

As a completely different alternative, you can also obtain it directly from the request parameter:

String var = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("var");
// ...

Upvotes: 4

Related Questions