leejeok

The Remarkable Everyday

Struts: HTML Options Tag Sample Code

Posted by leejeok on May 14, 2008

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&#8221; 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&#8221; 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

 

Advertisements

8 Responses to “Struts: HTML Options Tag Sample Code”

  1. […] Struts: HTML Options Tag Sample Code […]

  2. kyb struts said

    ah! worked like a charm! at last!!!!!!!!!!!!! thank you so so so so much!!! i was looking all over, you saved me.

    Cheers.
    Benny

  3. Arun said

    Thanx a lot,
    u have done a great work.

    I appreciate your time.
    Thanx again.

  4. vijay said

    this code saved my time… thanks!

  5. vinod said

    Really nice work …

    Following is the example of struts-config.xml for the above example:

  6. Manicka Selvam said

    That was a good article.

    Really helped me a lot.

    Thanks
    Manick

  7. uber_twin said

    even better with the label list being stored in the request scope:

    request.setAttribute( “menuList”, menuList);

  8. Chris said

    Nice, simple but most important complete and easy to understand example.Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: