I have been doing some research on the internet recently and notice not much of the sample code been provided to build the drop down menu using struts html options tag.
Here are the sample program which I develop using Struts framework by applying the html options tag. The screenshot will look similar as below.
Here are the list of pieces code been used.
index.jsp
<%@ taglib uri=”http://struts.apache.org/tags-html” prefix=”html” %>
<html>
<head>
<title>HTML Options Tag</title>
</head>
<body>
<h1>Struts: Using HTML Options Tag Example</h1>
<html:link action=”login”>Begin</html:link>
</body>
</html>
menu.jsp
<%@ taglib uri=”http://jakarta.apache.org/struts/tags-html” prefix=”html” %>
<html>
<head>
<title>HTML Options Tag</title>
</head>
<body>
<h1>Struts: Using HTML Options Tag Example</h1>
Please select the menu.
<html:form action=”login”>
<html:select property=”menu” >
<html:option value=””>–SELECT–</html:option>
<html:options collection=”menuList” property=”value” labelProperty=”label” />
</html:select>
</html:form>
</body>
</html>
MenuForm.java
package com.selection.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class MenuForm extends org.apache.struts.action.ActionForm {
public String menu;
public String getMenu() {
return menu;
}
public void setMenu(String menu) {
this.menu = menu;
}
public MenuForm() {
super();
// TODO Auto-generated constructor stub
}
}
MenuAction.java
package com.selection.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
public class MenuAction extends Action {
private final static String SUCCESS = “success”;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ArrayList menuList = new ArrayList();
MenuData menuData = new MenuData();
menuList = menuData.menuList();
HttpSession session = request.getSession();
session.setAttribute( “menuList”, menuList);
return mapping.findForward(SUCCESS);
}
}
In real world programming, some developer would prefer to display the options to be retrieved from the database. Here, I will hardcode the options and you may want to change the code to suit yourself.
MenuData.java
package com.selection.struts;
import java.util.ArrayList;
public class MenuData {
public MenuData() {
}
public ArrayList menuList() {
ArrayList menuList = new ArrayList();
Menu menu = null;
menu = new Menu(“1”, “First Option”);
menuList.add(menu);
menu = new Menu(“2”, “Second Option”);
menuList.add(menu);
menu = new Menu(“3”, “Third Option”);
menuList.add(menu);
menu = new Menu(“4”, “Forth Option”);
menuList.add(menu);
return menuList;
}
}
Menu.java
package com.selection.struts;
public class Menu {
/** Creates a new instance of Menu */
public Menu() {
}
String value;
String label;
public Menu(String value, String label){
this.value = value;
this.label = label;
}
public String getValue() {
return value;
}
public void setLabel(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
}
Each code contains some comments inside. Hope it helps. I have been using NetBeans 5.5 to build this sample. You can get the full code here or deploy the war to your web services.
Click Here