开发者

Passing request Parameters from Managed Bean to A jsf page

开发者 https://www.devze.com 2023-02-08 06:37 出处:网络
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

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开发者_StackOverflow中文版 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


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)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消