Jacob
Jacob

Reputation: 14731

Pass Value From JSP To JSF Page Backing Bean

Is it possible to pass parameter from a JSP page to a JSF page's backing bean?

JSP page is popup window open when I invoke a button in JSF page and my selected value in JSP page, I should be able to pass to JSF's backing bean.

P.S. When I add comment and I put @anyname when someone replies, @namyname part is getting truncated.

Update 1

To get the selected value from JSP to bean I did a crude approach.

I added the the following in JSP

String str = request.getParameter("selectname");

and assigned string str to a hidden field

 <input type="hidden" name="hid" value="<%=str%>"  /> 

and in my bean I am getting the value like the following

logger.info("jsp value "+FacesContext.getCurrentInstance().getExternalContext()
               .getRequestParameterMap().get("hid"));

This almost works except I always gets the value which I previously selects. E.g. First time when I selects 1 and value returned in bean is null, second time when I selects 2, value returned is 1.

How could I get the currently selected value in my bean?

Upvotes: 0

Views: 6140

Answers (2)

FishGel
FishGel

Reputation: 1100

f:viewParam lets you associate bean properties with request parameters –

-This introduces several new capabilities

  • New tags that navigate via GET instead of POST and tags that navigate via GET instead of POST, and send parameters along with the address
  • Sending data from non-JSF forms to JSF pages
  • Make results pages results pages bookmarkable
  • This is a new feature in JSF 2.0
  • example: <f:viewParam name="fg" value="#{colorPreferences.foreground}" />
  • If the “fg” parameter is non-null, it is passed to setForeground before the page is rendered

    <f:metadata>
    <f:viewParam name="param1" value="#{bean.prop1}"/>
    <f:viewParam name="param2" value="#{bean.prop2}"/>
    </f:metadata>
    <h:head>…</h:head>
    <h:body>
    Blah Blah blah #{bean prop1} , blah, #{bean.prop1}
    </h:body>
    

    If the page is called with page.jsp?param1=foo&param2=bar, then “foo” and “bar” are passed to “setProp1” and “setProp2” before the page is rendered. If any of the parameters are null (i.e., no such request parameter exists), then the associated setter is not called at all, and the bean has its normal value for that property

You can find the answer from the JSF tutorial http://www.coreservlets.com/JSF-Tutorial/jsf2/

Upvotes: 1

Bozho
Bozho

Reputation: 597106

First, if your JSF view technology is JSP, then you can use the <h:> tags in the jsp and it becomes straightforward 0 just add a <h:commandButton action="#{yourBean.yourMethod}" />

Otherwise, you still can perhaps, but I'd suggest that you make your popup also a JSF page. JSF and JSP don't coexist well. If you really must retain the situation, then you can try to emulate a JSF POST request to the target jsf URL.

Upvotes: 2

Related Questions