wood burning stoves*
The moose likes Java in General and the fly likes How to read instance variable dynamically? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to read instance variable dynamically?" Watch "How to read instance variable dynamically?" New topic
Author

How to read instance variable dynamically?

Simpson Kumar
Ranch Hand

Joined: Mar 19, 2008
Posts: 261
I have a requirement to read the properties file, and according to properties from properties file I need to display them in the JSP. for this I need to point the exact matching of property with the instance variable from Javabean (The data for this object is coming from DB). If the properties in the properties file are fixed, then its not a big deal we can read each property and match with javabean's variable directly.
But in future the properties file can be changed with different properties, without changing the code I need to match them and need to display them with the data in JSP. But here the Javabean is reading all fields data from database.

Here is the properties file.


Here is the Javabean class:



Could some one please help on this


Thanks,
Kumar
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61458
    
  67

Please take the time to choose the correct forum for your posts. This forum is for questions on advanced Java.

For more information, please read this.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Simpson Kumar
Ranch Hand

Joined: Mar 19, 2008
Posts: 261
This is not type of UI question, and its more like programming side. Hence I used java advanced forum
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61458
    
  67

It is not an advanced Java question. In fact, I'm not sure exactly what you are asking.

Are you looking for a way to have any changed values appear on already displayed JSPs? or something else?
Simpson Kumar
Ranch Hand

Joined: Mar 19, 2008
Posts: 261
actually our admin has to decide what UI elements needs to display on JSP, for this he is gonna change the properties file by including the new properties as I mentioned above or remove the existing properties. The thing is, we don't need to change the code every time, we need to display the properties and corresponding values from DB.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61458
    
  67

Ah, I think I see what you may be saying. The specific names of the properties may be fluid, so using a JavaBean is problematic because you don't want to have to modify the members of the JavaBean as the properties in the properties files change? Is that it?

If so, then yes, a JavaBean is not a good solution. Investigate the use of a Map instead, which is just as easy to deal with on a JSP as a bean (assuming that you are using the EL and not scriptlets).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61458
    
  67

P.S. The fact that java.util.Properties implements Map may make this very simple for you.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
And that there are taglibs out which can read properties files without any need to write extra Java code: the JSTL's fmt tagilb.
Simpson Kumar
Ranch Hand

Joined: Mar 19, 2008
Posts: 261
I can display the values from properties file, but here, if the admin wants to change the name of UI element and type to "radio", then we need to show them correspondingly, see the image here ,
So we need to change the givenname to completename and corresponding value from DB.

Here is the java code Im using to set the values . see the comment in the code.

(
Simpson Kumar
Ranch Hand

Joined: Mar 19, 2008
Posts: 261
JSP code:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61458
    
  67

Did you even read the replies?
Simpson Kumar
Ranch Hand

Joined: Mar 19, 2008
Posts: 261
I read the replies, but I didn't get what I expected.

JavaBean is fine because it needs to have all fields from DB, but on UI side we need to show some fields those are decided by our UI maintenance team by changing the properties in properties file.

Ex: right now they wanted to display First Name, Last Name , and telephone number. In case in future, if they wanted to display First Name, Full Name, and Address (instead Last Name and telephone number), then without changing our java code can we get replacing data from JavaBean(this is complete field list with data from DB) in order to display on UI side? (I can read properties, but the corresponding data how to pick dynamically)... Can we do it from java?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Instead of those nasty scriptlets in JSP, use JSTL/EL. The JSTL core taglib provides you under each the <c:if> and <c:choose> tags which will be very useful here.

If you want to go a step further --doing this stuff in a Java class and controlling it at a higher level-- consider a component based MVC framework like JSF.
Maris Orbidans
Ranch Hand

Joined: Mar 08, 2004
Posts: 149
Sudhakar Kumar wrote: (I can read properties, but the corresponding data how to pick dynamically)... Can we do it from java?


Do you want to specify in config file which property of UserDN will be shown in each UI field ?

You need to use reflection or use Commons BeanUtils.




 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to read instance variable dynamically?