• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

html:select and html:options

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

My name is cory and I have a problem... no... its not alcoholism, not smoking, not drugs.... my problem is this darned html select control. Here is my scenario:

I have a class called AdminForm:

public class AdminForm extends ActionForm {
private ArrayList dealers;
private String currentDealer;

public void setDealers(ArrayList dealers) { this.dealers = dealers; }
public ArrayList getDealers() { return dealers; }
public void setCurrentDealer(String currentDealer) {this.currentDealer = currentDealer; }
public String getCurrentDealer() { return currentDealer; }
}

the ArrayList is a collection of dealers:

public class Dealer
{
public Dealer() { }
private String dealerNumber;
private String dealerName;
public void setDealerNumber(String dealerNumber)
{ this.dealerNumber = dealerNumber; }
public String getDealerNumber() { return dealerNumber; }
public void setDealerName(String dealerName)
{ this.dealerName = dealerName; }
public String getDealerName() { return dealerName; }
}



In my action class, I do something like the following:

AdminForm adminForm = new AdminForm();
ArrayList dealers = service.getVisibleDealers();
adminForm.setDealers(dealers);
request.getSession().setAttribute("ADMINFORM",adminForm);

How do I output a html:select/html ptions box containing a list of the dealers? This wont work (below) Please help... this has taken me wayyyyyyy to long.

<pdk-struts-html:select name="ADMINFORM" property="currentDealer">
<pdk-struts-html ptions collection="dealers" property="dealerNumber" labelProperty="dealerName" />
</pdk-struts-html:select>

This seems soooo simple... to have stumped me for sooo long.
[ January 28, 2006: Message edited by: Cory Max ]
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cory,

Believe me, you're not alone. There are a lot of posts on this forum dealing with html:select.

My best advice to you is to use the html:optionsCollection instead of html:options. You can accomplish what you need to with either tag, but I find that the former is more intuitive and easier to use.

The documentation on both these tags can be found at:

http://struts.apache.org/struts-doc-1.2.x/userGuide/struts-html.html

Here's how you'd do it using html:optionsCollection:

<html:select name="ADMINFORM" property="currentDealer">
<html:optionsCollection property="dealers" value="dealerNumber" label="dealerName" />
</html:select>

What makes optionsCollection more intuitive is that the property is more like the property attribute for the other html:xxx tags. It's the property of a form bean that represents the collection of options.

With html:options, property means something entirely different: The property of the bean that will represent that "value" of each individual option. Collection represents the name of a bean that represents the collection of objects to be iterated over. Since your collection is actually a property of the form bean, using html:options requires a bean:define before the statement. Here's how you'd have to do it with html:options:

<bean:define id="dealersCollection" name="ADMINFORM" property="dealers" />
<html:select name="ADMINFORM" property="currentDealer">
<html:options collection="dealersCollection" property="dealerNumber" labelProperty="dealerName" />
</html:select>
[ January 29, 2006: Message edited by: Merrill Higginson ]
 
Cory Max
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow... thanks. Your help is much appreciated.

Cheers,

Cory
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have done the exactly what you provided. I think I am missing something though.

I am getting an error - [ServletException in:/jsp/searchBySystem.jsp] Cannot retrieve definition for form bean null'

Form is:
public final class SearchBySystemForm extends ActionForm {

private ArrayList systems;
private int systemId;

public int getSystemId() {
return systemId;
}

public ArrayList getSystems() {
return systems;
}

public void setSystemId(int i) {
systemId = i;
}

public void setSystems(ArrayList list) {
systems = list;
}

}

My Bean is:
public class System implements java.io.Serializable {
private int systemId;
private String systemName;
/**
* @return
*/
public int getSystemId() {
return systemId;
}

/**
* @return
*/
public String getSystemName() {
return systemName;
}

/**
* @param i
*/
public void setSystemId(int i) {
systemId = i;
}

/**
* @param string
*/
public void setSystemName(String string) {
systemName = string;
}

}

My Action Class is:
public class SearchBySystemAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {

String forward = "error";

ArrayList systemList = new ArrayList();
SearchBySystemForm systemForm;
systemForm = new SearchBySystemForm();

System system = new System();
system.setSystemId(1);
system.setSystemName("MARS");
systemList.add(system);

system = new System();
system.setSystemId(2);
system.setSystemName("JUPITER");
systemList.add(system);

systemForm.setSystems(systemList);

request.getSession().setAttribute("SYSTEMFORM",systemForm);
forward = "showSystemList";

return (mapping.findForward(forward));

}

My jsp is:
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<html>
<title>System List</title>
</head>
<body>
<html:errors/>
<html:form action="/searchBySystem">
<table>
<tr>
<td>System:</td>
<td>
<html:select name="SYSTEMFORM" property="systemId">
<html ptionsCollection property="systemList" value="systemId" label="systemName"/>
</html:select>
</td>
<td><html:submit>Ok</html:submit></td>
<td><html:cancel>Cancel</html:cancel></td>
</tr>

</table>
</html:form>
</body>
</html>
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change property="systemList" to property="systems"
[ May 22, 2006: Message edited by: Merrill Higginson ]
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know it is bad thing to write on old posts.. But I really could not stop to give big Thanks to Merrill for such a useful post... which will always remain helpful..!
 
reply
    Bookmark Topic Watch Topic
  • New Topic