Meaningless Drivel is fun!*
The moose likes Struts and the fly likes html:select and html:options 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 » Frameworks » Struts
Bookmark "html:select and html:options" Watch "html:select and html:options" New topic
Author

html:select and html:options

Cory Max
Ranch Hand

Joined: Jul 20, 2005
Posts: 83
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 ]

There are only 10 types of people in this world... Those who understand binary and those who don't.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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 htmlptionsCollection instead of htmlptions. 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 htmlptionsCollection:

<html:select name="ADMINFORM" property="currentDealer">
<htmlptionsCollection 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 htmlptions, 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 htmlptions requires a bean:define before the statement. Here's how you'd have to do it with htmlptions:

<bean:define id="dealersCollection" name="ADMINFORM" property="dealers" />
<html:select name="ADMINFORM" property="currentDealer">
<htmlptions collection="dealersCollection" property="dealerNumber" labelProperty="dealerName" />
</html:select>
[ January 29, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Cory Max
Ranch Hand

Joined: Jul 20, 2005
Posts: 83
Wow... thanks. Your help is much appreciated.

Cheers,

Cory
D Sheth
Greenhorn

Joined: May 22, 2006
Posts: 2
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

Joined: Feb 15, 2005
Posts: 4864
Change property="systemList" to property="systems"
[ May 22, 2006: Message edited by: Merrill Higginson ]
Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 231
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..!


God Gave Me Nothing I Wanted, He Gave Me Everything I Needed.
OCPJP6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: html:select and html:options