Get GET parameters in a managed JSF bean

Can someone tell me how to catch the parameters passed from the URI in a JSF managed bean? I have a navigation menu, all nodes of which are connected to some kind of navigation building. And I have two similar subjects: product acquisition and product release. They have the same page, but one other parameter: productType. I am trying to install it by simply adding it to the url in the to-view-id element as follows:

<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 cannot get this "productType" from my managed bean. I tried to get it through FacesContext as follows:

 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 managed bean parameter in faces-config.xml, and then get it through a regular 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 none of these methods helped me. They all returned null. How can I get this productType? Or how can I transfer it in another way?

+4
source share
1 answer

The default navigation rule does redirection. That is, it reuses the initial request. No matter how you try to access the request parameters in the redirected resource, it will always try to grab them from the initial and already processed request.

To fix this, you need to run a redirect instead of a redirect. It creates a new request (you also see that this is reflected in the address bar of the browser).

In jsf add

 <redirect/> 

in the case of navigation.

+3
source

Source: https://habr.com/ru/post/1305774/


All Articles