Passing Request Parameters from Managed Bean to jsf page

I am new to JSF 2.0. I am stuck in the following problem. I have a page from which a bean-driven action method. That I am doing some custom processing and this goes to another xhtml page. Now the problem is that before moving on to the next xhtml page, I also need to pass some parameters along with it. I used the following approaches

1- Put this variable in requestMap and try to access this page this way

<h:outputText value="#{param['userid']}"/> 

he does not work. Then someone told me that there is no standard way to pass JSF 2.0 request parameters along with the URL, so try passing it as a servlet request. I will die the code as follows

  ServletRequest request = (ServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest(); ServletResponse response = (ServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse(); //("user",user); //ServletRequest request = (ServletRequest)context.getRequest(); request.setAttribute("userid", "prithvi"); System.out.println("Came here"); RequestDispatcher rd = request.getRequestDispatcher("testf.xhtml"); try { rd.forward(request, response); } catch (ServletException ex) { Logger.getLogger(TestFlashScope.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(TestFlashScope.class.getName()).log(Level.SEVERE, null, ex); } 

I tried to access in the same way, but no luck. value is not displayed. Can someone help me how to achieve this solution.

BR, Prithvi

+4
source share
2 answers

Just set the result as a bean property.

page1.xhtml

 <h:commandButton value="Submit" action="#{bean.submit}" /> 

Bean:

 public String submit() { this.result = "blah"; return "page2" } 

page2.xhtml

 <h:outputText value="#{bean.result}" /> 

(assuming you are not using <redirect/> in <navigation-case> , if any)

+4
source

You must use Flash Scope. For more information, you can watch this blog post.

+1
source

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


All Articles