| Author |
How can I bind JSF forms directly to JAXB objects
|
Vindia Hari
Greenhorn
Joined: Jan 21, 2005
Posts: 9
|
|
Dear JSF Developers and Experts, As part of my project I have to populate JAXB objects using JSF forms. When I include my JAXB interfaces in my faces-config.xml as my managed beans, I see a "java.lang.InstantiationException", because JAXB requires the Object factory class to create the jaxb objects before they can be used. My question is, how could I bind my jsf forms to jaxb objects without creating my own custom component classes for each input field. Is there a better solution to do this. Your help is greatly appreciated. Thank You, Vindya.
|
 |
Varun Khanna
Ranch Hand
Joined: May 30, 2002
Posts: 1400
|
|
I am not an expert of JAXB, but what I can gather from you post is that - since managed beans are loaded "lazily", you are not able to get the instance of the binded object. Right? So if you can get the instance up will that solve your problem? You want all the managed bean's instances up or few (depending upon certain condition) ?
|
- Varun
|
 |
Vindia Hari
Greenhorn
Joined: Jan 21, 2005
Posts: 9
|
|
Thanks for your response. To set the Jaxb generated javabeans(that are my managed beans for my forms), we need to use the ObjectFactory to create them. And that is exactly my problem. My page throws me a "Class Instantiation Exception" every time my page comes up. I would like instances of all my managed beans available for each form. I appreciate your help, Thank You, Vindia
Originally posted by K Varun: I am not an expert of JAXB, but what I can gather from you post is that - since managed beans are loaded "lazily", you are not able to get the instance of the binded object. Right? So if you can get the instance up will that solve your problem? You want all the managed bean's instances up or few (depending upon certain condition) ?
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
I am not sure if I completely understand your problem because I have never worked with JAXB before, but it sounds to me like you shouldn't use your JAXB object as your backing bean? Here is what I am assuming. You are using JAXB to generate, at runtime, a managed bean for your JSF form. Is this correct? If so, I can try and make a suggestion. Create a ServletFilter that you define in your WEB.XML file. Use this filter to generate the managed bean and put that bean in whatever scope. Then, when your page is accessed the filter creates the bean via JAXB and then your JSF page will use whatever is currently in whatever scope. Does that make sense? Option number 2. Create a minimal managed-bean for each page. All this will do is use JAXB to initialize your real bean. For example, I have a form that accepts a bunch of information about an Issue. I have a JavaBean called Issue. I didn't want to have to collect all my information in a backing bean and then move this info over into my Issue class. So when the page is requested I immediatly call a method called create() and in this method I initialize my Issue object and put it in the session. Then all my JSF properties access this bean for holding and manipulating properties instead of anyting in the managed-bean itself. It basically just saves me from copying all my data from one set of fields to another. [ January 24, 2005: Message edited by: Gregg Bolinger ]
|
 |
Vindia Hari
Greenhorn
Joined: Jan 21, 2005
Posts: 9
|
|
Thanks Greg, Both the options look technically possible. Let me try them. Vindia
|
 |
Vindia Hari
Greenhorn
Joined: Jan 21, 2005
Posts: 9
|
|
Gregg, Thanks so much for pointing me in the right direction. I used your opiton 1, but din't use the ServletFilter class, instead I used JSF's phase listener. It seems to do what I need and that's to use JaxB's object factory to instantiate my jaxbObject (which is my managed bean). For people with a similar problem who use a factory to instantiate your managed beans, there is a great example in the book called "Core Java Server Faces" by David Geary and Cay Horstmann browse to Chapter 7 and there's an example there.. Thanks again, Vindia
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
Originally posted by Vindia Hari: Gregg, Thanks so much for pointing me in the right direction. I used your opiton 1, but din't use the ServletFilter class, instead I used JSF's phase listener. It seems to do what I need and that's to use JaxB's object factory to instantiate my jaxbObject (which is my managed bean). For people with a similar problem who use a factory to instantiate your managed beans, there is a great example in the book called "Core Java Server Faces" by David Geary and Cay Horstmann browse to Chapter 7 and there's an example there.. Thanks again, Vindia
Glad you were able to get it going. I have that book, but havan't really read through much of it yet. Thanks for the tip.
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
|
Hey, could you explain a bit on the benefit of using JAXB with JSF? Does it make your life easier? What's the deal?
|
 |
Varun Khanna
Ranch Hand
Joined: May 30, 2002
Posts: 1400
|
|
yeah even i am curious to see what are you trying to do and what makes you join JAXB with JSF
|
 |
Vindia Hari
Greenhorn
Joined: Jan 21, 2005
Posts: 9
|
|
Sorry..for taking so long to reply to you. My project requires to interface the jsf pages directly with JAXb objects and Not use POJO's. These jaxb objects have been generated by the middlegen layer. So, the problem that I was facing when I posted my last question was how do I instantiate a new object using JAXB's object factory. There were several ways of doing this, and since I'm pertty new with JSF, I was having a pretty hard time. But, of all the help, I still think that the init servlet at startup -defined in web.xml -- is the best way to instantiate the objects. It's pretty easy to interace directly with JaxB, but there is a learning curve here. The problem that I am having is how do I use my Converter classes to bind to <h:selectOneMenu> .. It works fine with input text boxes but not with selectOne...I posted my question and hopefully will get some help.. Thanks..if you need any code posting relating to JAXB, I'd be happy to help... Vindya
|
 |
 |
|
|
subject: How can I bind JSF forms directly to JAXB objects
|
|
|