aspose file tools*
The moose likes JSF and the fly likes passing parameter from non JSF URL to JSF backing bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "passing parameter from non JSF URL to JSF backing bean" Watch "passing parameter from non JSF URL to JSF backing bean" New topic
Author

passing parameter from non JSF URL to JSF backing bean

wong sp
Greenhorn

Joined: Jan 30, 2008
Posts: 1
Hi, I need to pass parameters from a non JSF URL to JSF backing bean. I tried this way but it is not working. Please advice.

passing parameter from


at a.jsf


at backing bean
Praful Sinha
Greenhorn

Joined: Jan 25, 2008
Posts: 18
You need to take a use this code:

HttpServletRequest request=(HttpServeltRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();

String value=request.getPerameter("id");

This will retrieve your http request prameter value.

Thanks
Praful sinha
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Praful,
I doubt your approach will work...
Of course your code is legal 100% but how to trigger it upon typing:
http://localhost:8080/test/a.jsf?id=1
in your browser?
JBoss Seam offers a solution for such situations.
Praful Sinha
Greenhorn

Joined: Jan 25, 2008
Posts: 18
I have tested it in tomcat application and its working fine here.

hi wrong,

on which server you are working.

And if the protocal you are using is Http then i dont think you will get any issue to retrieving data from request parameter.

Thanks
Praful Sinha
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

How do you ignite getNewList() method upon typing a URL in your browser?
Ketan Jambhekar
Ranch Hand

Joined: Mar 26, 2007
Posts: 37
Hi,
There are atleast two approaches by which you can call your required method.
if the managed bean associated with your class is in request scope you can call the method from the constructor of the managed bean.
Or you can bind a component to your backing bean using the HTML Form and call the method from there.

and for managed Bean



Hope this Helps !

Ketan.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16058
    
  21

The cleanest approach is actually to realize that a "backing bean" isn't a special type of object - it's a POJO that's defined to JSF for management purposes.

What I'm doing in cases like this is to simply write a small servlet or JSP to catch the parameter request. The servlet/JSP then captures the parameter in the usual way, resolves the backing bean in its usual way, and calls the backing bean's setter for that property.

As a rough sketch:



Doing this will avoid using a lot of esoteric features that might change in future releases. I think the servlet and JSP definitions are pretty well stabilized by now.


Customer surveys are for companies who didn't pay proper attention to begin with.
Praful Sinha
Greenhorn

Joined: Jan 25, 2008
Posts: 18
I have tested it in tomcat application and its working fine here.

hi wrong,

on which server you are working.

And if the protocal you are using is Http then i dont think you will get any issue to retrieving data from request parameter.

Thanks
Praful Sinha
Cindy Smith
Greenhorn

Joined: Nov 26, 2007
Posts: 18
Tim,
I tried your approach but I am not able to get the backing bean from the session object in the servlet.

BackingBean myBean = (BackingBean)request.getSession().getAttribute("mybean");

myBean is always null.

I also tried getting the Faces Context but it also comes back as null:
FacesContext fctx = FacesContext.getCurrentInstance();

It seems when I am in the servlet I am not able to access objects normally available when I go through the Faces Servlet. Any suggestions or am I missing something?
mohammed moin
Greenhorn

Joined: Dec 01, 2007
Posts: 10
Hi Praful,

Can you provide me solution for this problem, "passing parameter from non JSF URL to JSF backing bean", on tomcat server?

Any help will be greatly appreciated.


Thanks.
Praful Sinha
Greenhorn

Joined: Jan 25, 2008
Posts: 18
Hi moin,

Can you check the same code which i have suggested earlier. Its working fine on Tomcat 6.0

Thanks
Praful Sinha
Java Lanka
Greenhorn

Joined: Feb 13, 2008
Posts: 1
I think the main problem is that a method wont get triggered once the parameter is passed. For instance we have a url like example.jsf?id=10 and a method like setViewID should be called in the managed bean.
We can use the <managed-property> to do this
Example is as follows
* <managed-bean>
* <managed-bean-name> manageBeanEx</managed-bean-name>
* <managed-bean-class>urldata.web.ManageBeanEx</managed-bean-class>
* <managed-bean-scope> request</managed-bean-scope>
* <managed-property>
* <property-name>viewID</property-name>
* <property-class>java.lang.Integer</property-class>
* <value>#{param.id}</value>
* </managed-property>
* </managed-bean>

"id" which is getting passed as the URL parameter is mapped to the viewID of the bean. This will call setViewID method in the managed bean. Note that you need to access the managed bean for this to happen.
So something like
<hutputText value="#{manageBeanEx.viewID}"></hutputText>
Should trigger it.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16058
    
  21

Originally posted by Reggie Rodrigues:
Tim,
I tried your approach but I am not able to get the backing bean from the session object in the servlet.

BackingBean myBean = (BackingBean)request.getSession().getAttribute("mybean");

myBean is always null.

I also tried getting the Faces Context but it also comes back as null:
FacesContext fctx = FacesContext.getCurrentInstance();

It seems when I am in the servlet I am not able to access objects normally available when I go through the Faces Servlet. Any suggestions or am I missing something?


Reggie, I'm sorry I didn't see this earlier.

If your servlet attempts to get a session object before JSF has created it, you'll get back null. In order for it to work, you have to have created a session and you have to have initialized the bean. I haven't checked the spec, but as far as I know, a JSF session bean isn't created until the first time a JSF reference is made to that bean. If a session doesn't exist yet, the session will be created and then the session object will be created and stored in the session.

If you're calling the external servlet before this happens, you can have the servlet create the session object (and the session as well). However, the JSF management parameters won't be taken into consideration, so you'd have to handle all the initialization yourself (assuming there is any).

The JSF context is created and destroyed by the JSF servlet each time an HTTP request is made for JSF. So you can't reference it in non-JSF code.
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
http://www.thoughtsabout.net/blog/archives/000033.html


MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Lincoln Baxter
Greenhorn

Joined: Aug 25, 2008
Posts: 24
Try PrettyFaces extension. It solves a lot of these problems.

http://ocpsoft.com/prettyfaces/


Lincoln Baxter, III | [url]http://ocpsoft.com/[/url] | Creator of: PrettyFaces - Bookmarks, Navigation, and Dynamic Parameters for JSF
Vincent Zhao
Greenhorn

Joined: Jul 09, 2008
Posts: 28
Hello all, I have a same issue as this one. Do you guys have any good solution? Thank you in advance.
Vincent Zhao
Greenhorn

Joined: Jul 09, 2008
Posts: 28
I've tried this way:
webpage
but no lucky.
Rajesh K. Ilango
Greenhorn

Joined: Jun 27, 2005
Posts: 11
<%
CustomerController controller = new CustomerController();
controller.createCustomer();

request.getSession(true).setAttribute("customerCtrl", controller);
%>

<jsp:forward page="/pages/createcustomer.jsf" />

This works


Regards,
Rajesh K Ilango


Rajesh Kumar Ilango
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16058
    
  21

Sigh. This thread comes back more than the original "Alien".

http://faq.javaranch.com/java/DontWakeTheZombies
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing parameter from non JSF URL to JSF backing bean