Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Using Properties Files to Populate Drop-Downs

Options
  • 13-07-2006 8:26am
    #1
    Registered Users Posts: 2,492 ✭✭✭


    Hi Guys....

    I have a very simple JSP page and I want to populate the drop-downs on the page with values from properties files...can someone tell me how I can do this?

    Is there some javascript I can insert in to the JSP page to load the properties? Is there much configuration involved?

    Did a google but there's not much coming up on it...

    Thanks,

    Tommy


Comments

  • Registered Users Posts: 2,492 ✭✭✭trotter_inc


    Anyone?


  • Closed Accounts Posts: 25,848 ✭✭✭✭Zombrex


    I have a very simple JSP page and I want to populate the drop-downs on the page with values from properties files...can someone tell me how I can do this?

    Is there some javascript I can insert in to the JSP page to load the properties? Is there much configuration involved?

    Are you sure you are using JSP, as in Java Server Pages? Because you would be using Java in the back end, rather than JavaScript I'd imagine.

    Do you want to do this at the server end (the web server), or at the client end (the web browser)?


  • Registered Users Posts: 1,996 ✭✭✭lynchie


    Have a look here for info on loading property files. Once loaded, you can create your drop down list from them


  • Registered Users Posts: 2,492 ✭✭✭trotter_inc


    lynchie wrote:
    Have a look here for info on loading property files. Once loaded, you can create your drop down list from them

    That looks like something I could use... thanks lynchie...


  • Registered Users Posts: 2,492 ✭✭✭trotter_inc


    Wicknight wrote:
    Are you sure you are using JSP, as in Java Server Pages? Because you would be using Java in the back end, rather than JavaScript I'd imagine.

    Do you want to do this at the server end (the web server), or at the client end (the web browser)?

    sorry, excuse me, I meant java alright.... I came up with this solution... it works fine but seems like a bit of an overkill so please criticise and suggest any cleaner means if you wish... thanks guys...

    Properties locations = new Properties();
    locations.load(new FileInputStream("c:\\LinuxOracle10G.properties"));
    Enumeration locationNames = locations.propertyNames();

    while (locationNames.hasMoreElements()){

    String name = (String)locationNames.nextElement();
    String value = locations.getProperty(name);
    StringTokenizer st = new StringTokenizer(name,".");
    String propCategory = st.nextToken();
    String propName = st.nextToken();

    if(propCategory.equals("location")){ %>

    <option value="<%=propName%>"><%=value%></option>

    <%} // end if
    } // end while names
    %>


  • Advertisement
  • Registered Users Posts: 2,492 ✭✭✭trotter_inc


    one of the main problems is that I have to put all my properties (for all the drop-downs) in one single property file, and for each drop-down I have to parse the entire contents to retrieve the subset I want for that particular drop-down...


  • Registered Users Posts: 1,996 ✭✭✭lynchie


    You have loads of different choices.

    1: Store them in a DB and bring back only the ones you need
    2: Create multiple property files containing only the properties for each drop down
    3: Create a singleton, get it to parse your properties file once, and separate all the different types of items in your properties file into a Map.
    4: Take an MVC approach like struts which simplifies a lot of what you are trying to accomplish such as building the <select> and <option> elements by hand. It can also prepopulate it using a list of values too.


  • Registered Users Posts: 2,426 ✭✭✭ressem


    It's fairly simple to carry out this sort of thing with spring-framework or apache digester

    i.e. calling a class constructor using values loaded within a file.

    http://www.springframework.org/docs/reference/beans.html

    All that's needed then to call a classes constructor using a particular set of values from a file (spring bean)
    is
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.FileSystemResource;
    
    ...
    SerialParameters serialParams; // My Class that I want to construct using values from a file
    
    XmlBeanFactory beanFactory;
    FileSystemResource classPathResource;
    		
    // Configure terminal parameters from spring beans
    fileSystemResource = new FileSystemResource("./lib/spring-config.xml");
    beanFactory = new XmlBeanFactory(fileSystemResource);
    serialParams = (SerialParameters) beanFactory.getBean("serialParameters");
    	
    

    Where 'serialParameters' in 'spring-config.xml'
    looks like
    	<bean id="serialParameters" class="com.example.SerialParameters">
    		<property name="portName">
    			<value>/dev/ttyS0</value>
    		</property>
    		<property name="baudRate">
    			<value>19200</value>
    		</property>
    ...
    		<property name="parity">
    			<value>0</value>
    		</property>		
    	</bean>
    

    As you'll see from the link above (3.3.3.3), you can use lists and maps just as easily.


Advertisement