How to have ItemId in the URL in JSF based application

I have a JSF based application and I am using faces-config.xml to navigate and I am using /redirect to navigate form case to case.

The application lists products and upon selecting one, a product detail is displayed. I would like to have productId as part of the page URL so that URL can be emailed and the receiver can paste the received URL into browser and retrieve the product detail.

Currently the URL is not rewritten to have session id in it.

I am not ever sure where to start on this.

[518 byte] By [AsiaSuna] at [2007-11-26 15:18:10]
# 1

faces-config.xml<managed-bean>

<managed-bean-name>myBean</managed-bean-name>

<managed-bean-class>mypackage.MyBean</managed-bean-class>

<managed-bean-scope>request</managed-bean-scope>

<managed-property>

<property-name>productId</property-name>

<value>#{param.productId}</value>

</managed-property>

</managed-bean>

MyBeanprivate String productId; // + getter + setter

With this, the URL http://example.com/shop/products.jsf?productId=100 will put "100" in the productId property.

BalusCa at 2007-7-8 11:00:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Balus,

Is this line correct?

<value>#{param.productId}</value>

Or should it be:

<value>#{myBean.productId}</value>

Thanks for the tip (even though I didn't ask it)! I think it will help me with something I was playing with a few months ago. I got it working just fine, but this solution would be much better.

CowKing

IamCowKinga at 2007-7-8 11:00:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

This is clear and helpful. However I don't know how to create this URL (http://example.com/shop/products.jsf?productId=100) in the first place. The URL I have is in the form of http://example.com/shop/products.faces.

Is adding '?productId=100' to the URL something configurable or it has to be done programmatically via URL rewriting?

AsiaSuna at 2007-7-8 11:00:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

> Is this line correct?

> ><value>#{param.productId}</value>

Try it out yourself ;)

> This is clear and helpful. However I don't know how

> to create this URL

> (http://example.com/shop/products.jsf?productId=100)

> in the first place. The URL I have is in the form of

>http://example.com/shop/products.faces.

> Is adding '?productId=100' to the URL something

> configurable or it has to be done programmatically

> via URL rewriting?

You can implement it in several ways:

JSF<h:outputLink value="products.faces">

<h:outputText value="click here" />

<f:param name="productId" value="100" />

</h:outputLink>

<%-- or --%>

<h:outputLink value="products.faces?productId=100">

<h:outputText value="click here" />

</h:outputLink>

Or write a servlet listening on /shop/, scan the current request URI for product.faces, get the productId from the backing bean's session and rewrite the URL using request.getRequestDispatcher("product.faces?productId=" + productId).forward(request, response).

Maybe there are more clever solutions using configfiles, but I don't have much experience (yet) with such GET request thingies in conjunction with JSF.

BalusCa at 2007-7-8 11:00:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

What I've had to do in cases like this is to have the parameter value either from the URL or a hidden field on the form. Maybe this is only because the page has immediate buttons on it. My faces-config.xml has this in it:

<managed-bean>

<managed-bean-name>myBean</managed-bean-name>

<managed-bean-class>myBean</managed-bean-class>

<manged-bean-scope>request</managed-bean-scope>

<managed-property>

<property-name>reqProp1</property-name>

<value>#{param['reqProp1'] != null ? param['reqProp1'] : param['form1:reqProp1']}</value>

</managed-property>

</managed-bean>

Then the .jsp has:

<h:form id="form1">

<h:inputHidden id="reqProp1" value="#{myBean.reqProp1}" />

...

</h:form>

BrantKnudsona at 2007-7-8 11:00:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

This is working. However, here is a small issue:

The managed bean I have supporting this has session scope. And unless the scope of it is request using param gives error "javax.faces.FacesException: Property questionId references object in a scope with shorter lifetime than the target scope session".

Is there anything that can be done in passing ?questionId=23 into the managed bean form the URL short of 'redesigning the application' to sport the request scoped bean

AsiaSuna at 2007-7-8 11:00:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...