get GET parameters in JSF's managed bean

mykola picture mykola · Apr 1, 2010 · Viewed 7.8k times · Source

Can someone tell me how to catch parameters passed from URI in JSF's managed bean? I have a navigation menu all nodes of which link to some navigation case. And i have two similar items there: Acquiring products and Issuing products. They have the same page but one different parameter: productType. I try to set it just by adding it to URL in "to-view-id" element like this:

<navigation-case>
    <from-outcome>acquiring|products</from-outcome>
    <to-view-id>/pages/products/list_products.jspx?productType=acquiring</to-view-id>
</navigation-case>

<navigation-case>
    <from-outcome>issuing|products</from-outcome>
    <to-view-id>/pages/products/list_products.jspx?productType=issuing</to-view-id>
</navigation-case>

But i can't get this "productType" from my managed bean. I tried to get it through FacesContext like this:

FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("productType")

And like this:

HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
    request.getParameter("productType");

And i tried to include it as a parameter of managed bean in faces-config.xml and then getting it through ordinary setter:

 <managed-bean>
        <managed-bean-name>MbProducts</managed-bean-name>
        <managed-bean-class>my.package.product.MbProducts</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
     <managed-property>
         <property-name>productType</property-name>
         <value>#{param.productType}</value>
     </managed-property>
    </managed-bean>
...
public class MbProducts {
...
 public void setProductType(String productType) {
  this.productType = productType;
 }
...
}

But neither of these ways have helped me. All of them returned null. How can i get this productType? Or how can i pass it some other way?

Answer

BalusC picture BalusC · Apr 1, 2010

The navigation rule by default does a forward. I.e. it reuses the initial request. Whatever way you try to access the request parameters in the forwarded resource, it will always try to grab them from the initial and already-processed request.

To fix this, you need to fire a redirect instead of forward. It creates a brand new request (you also see this reflecting back in the browser address bar).

In JSF, adding

<redirect/>

to the navigation case should do.