Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing parameter from non JSF URL to JSF backing bean

 
wong sp
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you ignite getNewList() method upon typing a URL in your browser?
 
Ketan Jambhekar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18212
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Praful Sinha
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18212
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.thoughtsabout.net/blog/archives/000033.html
 
Lincoln Baxter
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try PrettyFaces extension. It solves a lot of these problems.

http://ocpsoft.com/prettyfaces/
 
Vincent Zhao
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I have a same issue as this one. Do you guys have any good solution? Thank you in advance.
 
Vincent Zhao
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried this way:
webpage
but no lucky.
 
Rajesh K. Ilango
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%
CustomerController controller = new CustomerController();
controller.createCustomer();

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

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

This works


Regards,
Rajesh K Ilango
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18212
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sigh. This thread comes back more than the original "Alien".

http://faq.javaranch.com/java/DontWakeTheZombies
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic