Showing posts with label checkboxlist and select. Show all posts
Showing posts with label checkboxlist and select. Show all posts

Friday, June 25, 2010

Struts2 UI checkboxlist and select Tags


UI Form Tags – checkboxlist and select Tags

Aim:
To understand how to create checkboxlist and select tag in struts2 using list property in &lt s:checkboxlist list=” ” &gt and &lt s:select list=” “ &gt .

The checkboxlist tag is UI tag. It is used to create series of checkboxes from a list

We will understand the checkboxlist and select tags from following coding.

Requirement:

            Jsp files

                        a). home.jsp
                        b). checklist.jsp
                        c). result.jsp

            Action files

                        a). CheckAction.java

            configuration files

                        struts.xml

Directory structure of this project.
























 Note:
         You have already known how to create Project for Struts2 such as SureshS.



Procedure:

Step 1:

            Create home.jsp

            Coding for home.jsp


< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >

< title > Home Page < /title >
< /head >
< body >
< s:form action="check" >
< s:submit/ >
< /s:form >
< /body >
< /html >


Step 2:

            Create checklist.jsp

            Coding for checklist.jsp


< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta name="keywords" content="example for checkboxlist, select " >

< title > CheckboxList Page < /title >
< /head >
< body >

< s:form action="result" >

< s:checkboxlist list="colors" name="yourColor" value="defaultColor" / >
< s:select list="country" name="yourCountry"/ >
< s:submit/ >

< /s:form >
< /body >
< /html >

Step 3:

Create result.jsp


< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta name="keywords" content="example for checkboxlist, select " >

< title > CheckboxList Page < /title >

< /head >
< body >

select color: < s:property value="yourColor" / >
Your Country: < s:property value="yourCountry"/ >

< /body >
< /html >

Step 4:
CheckAction.java


package example;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
import org.apache.commons.logging.Log;

public class CheckAction extends ActionSupport
{
    private List colors;

    private String yourColor;

    private List country;
    private String yourCountry;

    public List getCountry() {
        return country;
    }

    public void setCountry(List country) {
        this.country = country;
    }

    public String getYourCountry() {
        return yourCountry;
    }

    public void setYourCountry(String yourCountry) {
        this.yourCountry = yourCountry;
    }
    public List getColors()
    {
        return colors;
    }
    public void setColors(List colors)
    {
        this.colors = colors;
    }
    public String getYourColor() {
        return yourColor;
    }
    public void setYourColor(String yourColor) {
        this.yourColor = yourColor;
    }
    public CheckAction()
    {
        colors=new ArrayList();
        colors.add("RED");
        colors.add("Blue");
        colors.add("Green");
        colors.add("Cycan");

        country=new ArrayList();
        country.add("India");
        country.add("Pakistan");

      
    }
    public String execute() throws Exception
    {
      
        return "success";
    }
}

Step 5:

            Made some changes in struts.xml

< ?xml version="1.0" encoding="UTF-8" ? >
< !DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
http://struts.apache.org/dtds/struts-2.0.dtd >

< struts >

< constant name="struts.devMode" value="true" / >

< package name="example" namespace="/example" extends="struts-default" >

< action name="check" class="example.CheckAction" >
< result name="input" > /example/home.jsp < /result >
< result name="success" > /example/checklist.jsp < /result >

< /action >
< action name="result" class="example.CheckAction" >

< result name="success" > /example/result.jsp < /result >

< /action >
< /package >

< /struts >


Step 6:

Run home.jsp

In home.jsp, the button will display. You click on the button, the control will go to checklist.jsp, where you select color from checkboxlist and select country from select tag. And click button the result.jsp will display. Where the selected colors and selected country will display.


You run home.jsp

You will get like below
















click Submit













where you may select any if you select RED and Green from checkboxlist

select India from select tag.

You will get output


UI Form Tags – checkboxlist and select Tags


UI Form Tags – checkboxlist and select Tags

Aim:
To understand how to create checkboxlist and select tag in struts2 using list property in &lt s:checkboxlist list=” ” &gt and &lt s:select list=” “ &gt .

The checkboxlist tag is UI tag. It is used to create series of checkboxes from a list

We will understand the checkboxlist and select tags from following coding.

Requirement:

            Jsp files

                        a). home.jsp
                        b). checklist.jsp
                        c). result.jsp

            Action files

                        a). CheckAction.java

            configuration files

                        struts.xml

Directory structure of this project.
























 Note:
         You have already known how to create Project for Struts2 such as SureshS.