File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes To map backing bean from a selected drop-down value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "To map backing bean from a selected drop-down value" Watch "To map backing bean from a selected drop-down value" New topic
Author

To map backing bean from a selected drop-down value

N Deepu
Greenhorn

Joined: Jul 28, 2011
Posts: 25

Hi all,

I've a requirement to map the backing bean class from the object that is selected from the drop-down in my jsf page. How can i do this?
Eg. I've chosen Person from the drop down - I should retrieve a class called Person.java.

Kindly help me with the same..

Thanks in advance
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

Hi,

This is what I tried with the Animal and Person bean.







Hope this helps
Thanks


The important thing is not to stop questioning.Curiosity has its own reason for existing.
N Deepu
Greenhorn

Joined: Jul 28, 2011
Posts: 25

Hi,

You mean to say u're calling person and animal class from the enum class?
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

Yes,

As per your question ,you want the instance of a class,that is selected in dropdown right?

Thanks


N Deepu
Greenhorn

Joined: Jul 28, 2011
Posts: 25

Hi,

I want to call the class from the object that is selected. In your example, If I select Person, I should call the Person class and perform the corresponding functionality there.

Thanks

N Deepu
Greenhorn

Joined: Jul 28, 2011
Posts: 25

Hi again,

I was trying to execute what you've replied here. But it is not mapping the value change listener class on change of the option even though I've given
"Onchange = submit()"
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16061
    
  21

You have a problem with definitions here, "Ns". You're not looking to "retrieve" or "call" a class. What you want to do is retrieve an instance of a class.

Beyond that there are several other difficulties.

First of all, you cannot specify a complex (class) object as the key part of a SelectItem. The SelectItem is rendered as HTML, and HTML is text-only, so if you provide a binary object as the key, it will be converted to a String, and by default, that string will be a meaningless sequence of characters of little or no practical use. A better approach is to put all your objects in a Map where each entry has a text key, use that key to instantiate the SelectItem, then have the action handler use that key to retrieve the actual object from the Map.

Secondly, you can't just "submit()". You need proper AJAX support if you want to be able to be notified when a selectOneMenu's selected value has changed.


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

Joined: Jul 28, 2011
Posts: 25

Hi,

Thanks. I'll try what you said..
N Deepu
Greenhorn

Joined: Jul 28, 2011
Posts: 25

Hi again Tim,

I follwed what you said. I've a Hashmap which has "Key-Label to be displayed" and "Value-The backing bean name". I'm able to retrieve the corresponding name after I select from the drop down. The value received is in the string format. How do I now create an instance of this and call the corresponding class?

Eg. I selected Address, getPropertiesMap().get(event.getNewValue() is giving me the fully qualified name - com.backingbean.Address which is now in string format.

Thanks in Advance
Tom mmeke
Greenhorn

Joined: Jul 19, 2011
Posts: 10

ns deep wrote:Hi again Tim,

I follwed what you said. I've a Hashmap which has "Key-Label to be displayed" and "Value-The backing bean name". I'm able to retrieve the corresponding name after I select from the drop down. The value received is in the string format. How do I now create an instance of this and call the corresponding class?

Eg. I selected Address, getPropertiesMap().get(event.getNewValue() is giving me the fully qualified name - com.backingbean.Address which is now in string format.

Thanks in Advance




Should look something like this. (Haven't tested it)
N Deepu
Greenhorn

Joined: Jul 28, 2011
Posts: 25

Hi,

I'm not getting the answer required. This is bugging me since 3 days. Kindly help me.


This is the exception I'm getting

java.lang.ClassNotFoundException: instance
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.infosys.reuse.Bean.UserBean.valueChanged(UserBean.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at javax.faces.event.MethodExpressionValueChangeListener.processValueChange(MethodExpressionValueChangeListener.java:140)
at javax.faces.event.ValueChangeEvent.processListener(ValueChangeEvent.java:130)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:773)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:746)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1161)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:72)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16061
    
  21

The server is looking for the following file in your WAR:

/WEB-INF/classes/instance.class

using the Class.forName("instance") as its directive.

I don't think you really wanted to do that, since class names shouldn't begin with lower-case letters and use of the default package is discouraged.
Tom mmeke
Greenhorn

Joined: Jul 19, 2011
Posts: 10

1. Every reference to ResourceLoader is static ?
2. Why are you first getting the KeySet, then iterate over the KeySet, to finally request the value from the properties map for each key ?
Why not just iterate the PropertiesMap from the beginning ?
3. Class.forname() expects a classname as parameter (duh), something like 'com.yourpackage.ResourceLoader', and not just the variable you gave an instance of a class ...

Besides, like Tim told you before, you can't expect JSF to give you an entire object as selected value.
Use my previous example, and change valueChanged to this.


Still not sure what your actually trying to achieve with this snippet.
Nevertheless, there is something seriously wrong with your code.
N Deepu
Greenhorn

Joined: Jul 28, 2011
Posts: 25

Thanks for the reply both of you.

This is what I changed and I'm now able to call the method of the Object being selected.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To map backing bean from a selected drop-down value