views:

1280

answers:

4

I'm pretty new to the jsp and struts way of doing things and so far I like what I see.

My current question is with the use of the struts select tag.

I have a page that displays a number of dropdown boxes using struts select currently the options are hard coded in the jsp. I would like to populate them based on a properties file. However I have no idea where to start.

I assume I need to take the contents of a properties file into an Array (of some sort) and assign that to the select tag. My questions are:

  1. Where does the code t build the array go?
  2. How do I connect that array to the select tag?
A: 

The code goes in your "Action" Java class that sits behind the JSP page. You configure this in the Struts XML configuration file.

You connect the array by using the list, listKey,and listValue attributes of the Struts 2 select tag. See here

kazanaki
Hi, Thanks for the help. I'm now working on just that. However, I am using annotations for most of the work instead of struts.xml, not sure if this will have any impact at this stage.Thanks for your help.Nathan
nathj07
A: 

Okay, here's another question for you and hopefully I'll get it clear.

I have mainpage.jsp which includes include/config.jsp. In config.jsp I have line:

<s:select label="Data Source" id="cbo_school" name="cbo_school"
     headerValue="--- Please Select ---" headerKey="0"
     list="dataSource"
     onchange="toggleDisplayStateByValue('cbo_school','ipentry', \"1\")" />

The list dataSource is defined in ConfigAction.java:

@Action(value="/config")
public String setDataSource(){
 Properties dataConfig = new Properties();
 String returnStatus = "success";
 try {
  dataConfig.load(this.getClass().getResourceAsStream("properties/dataSource.properties"));
  dataConfig.size();

  for(String key : dataConfig.stringPropertyNames())
  {
   String value = dataConfig.getProperty(key);
   this.dataSource.put(key, value);
  }

 } catch(IOException ioEx){
  System.out.println(ioEx.getMessage());
  returnStatus = "error";
 }
 return returnStatus;
}

public HashMap getDataSource()
{
 return dataSource;
}

The project setup is:

src/org.esf.adapters.serco.ui.ConfigAction

WebContent/jsp/mainpage.jsp

WebContent/jsp/include/config.jsp

As you can see from these code samples I am attempting to do all of this using annotations and not XML.

If anyone can point me in the right direction with this that would be great and very helpful.

Many thanks Nathan

nathj07
A: 

Here is a couple of tutorials with XML annotations for Struts 2.

See also this.

Is something wrong with your solution above? It does not work? Is there an error message?

kazanaki
A: 

The setter and getter for the DataSource is not the appropriate place for that code. Populate DataSource from your action method (show, edit, save, etc) or if you need it in many action methods, then use the Preparable interface and add a prepare().

You don't really need a setter unless you are posting values and creating the Map from the request or injecting it with something like Spring.

Struts is just going to call the getter when it processes the s:select tag and runs into list="dataSource".

Dusty Pearce