• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to capture xhtml input data

 
harish shankar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to capture all the field value(input fields) information for any xhtml in a Map,one way is i can get the values from the UI bean via reflection .Are there any other ways of doing this?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid I don't know what you mean. I'm not even sure if you mean xhtml in the HTML-standard way or the "xhtml" that servers as the JSF View Template Language.

As far as JSF goes, whenever you submit a form (as defined in a VTL template), JSF automatically receives all of the input control properties for the controls in that form. JSF then validates them, and if each and every one of them is determined to be valid, JSF itself will update the backing bean properties automatically. No user code is required.
 
harish shankar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes its the xhml for jsf.
I was looking some way to capture the data other than from backing bean.one approach I tried was reading all the child components of a form.but this does not give what values are assaigned for each component id
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harish

What are your reasons for trying to avoid using backing beans?
 
harish shankar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to check whether of mandatory fields in the form at the business layer,for example the XHTML has name,age,address,occupation fields I need to check if name and age details are entered but at the same point I do not want hard code the check for the mandatory fields in code instead It will be configurable.one way I can think of is to covert the managed bean into a map and pass it to business layer this I would have use reflection or JSON , I am thinking of not using reflection ,so trying to explore other options.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use "required=" attribute on the control elements to require data entry for that control. This can be configured dynamically on the backing bean as a POJO boolean property value, although it's usually not a good idea to make that sort of stuff too dynamic, as it can confuse users.

Always remember that JSF is designed to provide POJO-based solutions. If you are writing compicated code that digs into JSF's internals, you are usually doing it wrong.
 
harish shankar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. But we have to change the mandatory fields like add one would require code change. .
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soooo... It's OK to build a complex and fragile mechanism to play around with the internals of JSF, but it's NOT OK to add a single simple binary property to the UI model object? An object whose sole reason for existence is to support the UI? Not even considering that you could reference a supporting parallel UI model object if it is unacceptable to modify the primary UI model object?

 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harish

Sounds like an unreasonable requirement to me. A field is either mandatory or it isn't.

The best solution I see is to hold the values of the required attribute (as suggested by Tim) in a resource bundle properties file. That way, when you need to 'switch' a field from mandatory to optional (or vice versa), you simply change the value in this file and re-start the app server.
 
harish shankar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also need to check on the field value also...like name should be only 'xyz' address is 'asd'.i tried reading the child components but I could not see values associated with the components
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Attempting to read data by reading the child components is (again) doing it the hard way.

I'm going to assume that the name isn't ALWAYS going to be "xyz", since that would be pointless. But perhaps it can always only be "xyz" if the city is "Chennai".

The simple way to handle context-sensitive validation is to do it in the action method:



JSFUtils is a class that I create for my webapps to make interactions with JSF more abstract and less framework-dependent. The JSFUtils addErrorMessage() is simply a method that obtains the JSF messages object from the FacesContext, constructs a new Message and adds it the the JSF messages. It's about 5 lines long, but it keeps my mainline code from having to know about FacesContext and JSF's internal message management.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic