4
votes

I am new to JSF 2.0. I am stuck at the following problem. I have a page from which a managed bean action method is called. In that i do some custom processing and the it navigates to another xhtml page. Now the problem is that, before moving to next xhtml page, i need to pass some parameters along with it as well. I have used the following approaches

1- Put that variable in requestMap and tried to access in the page this way

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

it doesn't work. Then someone told me there is no standard way in JSF 2.0 to pass request parameters along with a URL, so try to pass it as a servlet request. I ammened 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 same way but no luck. no value is displayed. Can someone help me how to achieve this solution.

BR, Prithvi

2

2 Answers

4
votes

Just set the result as 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 that you aren't using <redirect/> in <navigation-case> if any)

1
votes

you should use Flash Scope. For more information you can look at this blog post.