Wednesday, June 30, 2010

Struts2 Data control Action Tag Example


Struts2 Action Tag Example

Aim:
            Action tag is used to call action directly from jsp in struts2 framework.

Note:
            I have already explained how to create struts2 project and folders for storing jsp file and for stroing action class file. I also explained where we configure the project.

Procedure:
           
Step 1:

            Create Project name as ActionTag

Step 2:

            Create Action class HelloWorld

package example;

import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport
{
    String greetings = null;
    public String execute() throws Exception
    {
        setGreetings("Hello World");
        return SUCCESS;
    }
    public String getGreetings()
    {
        return greetings;
    }
    public void setGreetings(String greetings)
    {
        this.greetings = greetings;
    }
}

                       
Step 3:

            Create index.jsp file

Struts2 Data control Action Tag Example


Struts2 Action Tag Example

Aim:
            Action tag is used to call action directly from jsp in struts2 framework.

Note:
            I have already explained how to create struts2 project and folders for storing jsp file and for stroing action class file. I also explained where we configure the project.

Procedure:
           
Step 1:

            Create Project name as ActionTag

Step 2:

            Create Action class HelloWorld

package example;

import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport
{
    String greetings = null;
    public String execute() throws Exception
    {
        setGreetings("Hello World");
        return SUCCESS;
    }
    public String getGreetings()
    {
        return greetings;
    }
    public void setGreetings(String greetings)
    {
        this.greetings = greetings;
    }
}

                       
Step 3:

            Create index.jsp file

< %@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"% >
< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" http://www.w3.org/TR/html4/loose.dtd >
< %@ taglib prefix="s" uri="/struts-tags" % >
< html >
< head >
< meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" >
< meta content="keyword" name="struts2 action tag example" >
< title > Struts 2 Example < /title >
< /head >
< body >
< s:action name="HelloWorldAction" executeResult="true" > < /s:action >

< /body >
< /html >


Step 4:

Create HelloWorld.jsp

< %@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"% >
< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" http://www.w3.org/TR/html4/loose.dtd >
< %@ taglib prefix="s" uri="/struts-tags" % >
< html >
< head >
< meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" >
< meta content="keyword" name="struts2 action tag example" >
< title > Struts 2 Example < /title >
< /head >
< body >


< s:property value="greetings"/ >

< /body >
< /html >


Step 5:

Made some changes in struts.xml

< !DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
http://struts.apache.org/dtds/struts-2.0.dtd >

< struts >

< package name="example" namespace="/example" extends="struts-default" >
< action name="HelloWorldAction" class="example.HelloWorld" >
< result > /example/HelloWorld.jsp < /result >
< /action >
< /package >
< /struts >



Step 6:

Run the index.jsp

Saturday, June 26, 2010

Struts2-Validation using annotation


Struts2 Validation using annotation

Problem:

            Create Login page with validation and using annotation.

Procedure:

            You have known how to create project for struts2 using Netbean6.8.

Step 1:
            Create project name as AnnotationPro

Step 2:
            Create LoginAction

            Add following coding into LoginAction.java

package example;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validation;

@Validation
public class LoginAction extends ActionSupport
{
    private String username;
    private String password;

    @RequiredStringValidator(message="Supply Password")
    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }
    @RequiredStringValidator(message="Supply Username")
    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public LoginAction()
    {
    }
    public String execute() throws Exception
    {
        if((getUsername().equals("suresh"))&&(getPassword().equals("kumar")))
        {
            return  "success";
        }
        else
        {
           return "error";
        }
    }

}
           
Step 3:
            Create home.jsp



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

< title > Hom Page < /title >
< /head >
< body >
< s:form action="LoginAction" method="post" >

< s:actionerror/ >
< s:fielderror/ >

< s:textfield label="UserName" name="username"/ >
< s:password label="PassWord" name="password"/ >

< s:submit value="Login"/ >

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

Step 4:
Create success.jsp

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

< title > Hom Page < /title >
< /head >
< body >

< h1 > Hello World! < /h1 > you are valid
< /body >
< /html >

Step 5:

Made some changes in struts.xml

< !DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
http://struts.apache.org/dtds/struts-2.0.dtd >

< struts >

< package name="example" extends="struts-default" >
< action name="LoginAction" class="example.LoginAction" >
< result name="input" > /example/home.jsp < /result >
< result name="error" > /example/home.jsp < /result >
< result name="success" > /example/success.jsp < /result >
< /action >
< /package >
< /struts >

Step 6:

Run the home.jsp















You give username as suresh and password as kumar

The success.jsp file will display.

You don’t give username




















you don’t give password




















you don’t give both username and password


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.








Tuesday, June 15, 2010

Struts2-ActionMessage and ActionError


Struts2 ActionMessage and ActionError

AIM:

            From this post we will learn how to validate the user input .

Purpose:

            If the error occur , the actionmessage will display.

Procedure:

Step 1:

            Create Action class Named as (nonui.java)

            You know how to create action class using netbean6.8.

            If you forget, please you will remind the previous post and continue.

            Include following coding into nonui.java

nonui.java

package example;
import com.opensymphony.xwork2.ActionSupport;
public class nonui extends ActionSupport
{
    private String username;
    private String password;

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }


    public nonui()
    {
    }

    public String execute() throws Exception
    {
        if((getUsername().equals("suresh")) &&((getPassword().equals("suresh"))))
        {
            addActionMessage("Valid User");
            return "success";
        }
        else
{
            addActionError(“Invalid User”);
           return "error";
}
    }
}

Step 2:

            Create jsp file such as NonForm.jsp and error.jsp and success.jsp

            NonForm.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta http-equiv="KEYWORDS" content="struts2 non form UI,Struts2 actionmessage, struts2 actionerror" >
< title > Login Page < /title >
< /head >
< body >
< s:form action="nonui" method="post" >
< s:textfield label="UserName" name="username" size="10" maxLength="20"/ >
< s:password label="PassWord" name="password" size="10" maxLength="20"/ >
< s:submit value="OK" / >
< /s:form >
< /body >
< /html >


error.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta http-equiv="KEYWORDS" content="struts2 non form UI,Struts2 actionmessage, struts2 actionerror" >
< title > ErrorPage < /title >
< /head >
< body >

< s:actionerror/ >

< /body >
< /html >

success.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta http-equiv="KEYWORDS" content="struts2 non form UI,Struts2 actionmessage, struts2 actionerror" >
< title > Login Page < /title >
< /head >
< body >
< s:actionmessage/ >
< /body >
< /html >

Step 3:

Made some changes in struts.xml

< !DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
http://struts.apache.org/dtds/struts-2.0.dtd >

< struts >
< package name="default" extends="struts-default" >
< action name="nonui" class="example.nonui" >
< result name="input" > /example/NonForm.jsp < /result >
< result name="error" > /example/error.jsp < /result >
< result name="success" > /example/success.jsp < /result >
< /action >

< /package >
< /struts >


Step 4:

Run the NonForm.jsp

You give suresh as username and suresh as password and the valid user message will display,otherwise the invalid user message will display.

Directory Structure of the above project






















Conclusion:
                 From this post we have learned how to use actionmessage and actionerror.

Struts2-UI Tags-III


Struts2-UI-TAGS-II

Aim:
            We will examine the textarea and doubleselect and file UI Tags.

Procedure:

Step 1:

            Create project for struts2.

Step 2:
           
            Create Action class

Step 3:

            Made some changes in struts.xml

Step 4:

            Create home.jsp and display.jsp file

Step 5:

            Run the home.jsp file
Step 1:

            Create project name as UI-tag III


Open the netbean 6.8

File—New Project New Project window will open.

Select java web under categories and select web Application under Projects.

Next

New Web Application window will open.

Give project name ( UI-Tags-III)
Next and also Next

Select Framework

You select struts2

Finish

The project UI-Tags-III has created successfully.


NOTE:

Delete all files under example folder for web pages and source packages

Structure of the Struts2 Project



       














     Store all jsp files under web pages

    Store all action class under sources packages


Step 2:

Create Action class name as uiaction

Right click on example under Source Packages

New—Other—select Struts2 under categories and select Struts2 Action under File types.

Next

Give Action class name (uiaction)

Finish

The Action class uiaction has created successfully.
 
Include following coding into uiaction

package example;
import java.util.*;

import com.opensymphony.xwork2.ActionSupport;
public class UIAction extends ActionSupport
{
    private String address;

    private String dishes;

    public String getDishes() {
        return dishes;
    }

    public void setDishes(String dishes) {
        this.dishes = dishes;
    }

    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

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

}

Step 3:

            Made some changes in struts.xml



< !DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
http://struts.apache.org/dtds/struts-2.0.dtd >

< struts >
< package name="example" namespace="/example" extends="struts-default" >
< action name="uiaction" class="example.uiaction" >
< result name="success" > /example/display.jsp < /result >
< result name="input" > /example/home.jsp < /result >
< /action >

< /package >
< /struts >

Step 4:

Create home.jsp

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

< title & gt JSP Page < /title >

< /head >
< body >
< s:form action="uiaction" method="post" >
< s:file label="browse" accept="text/*"/ >
< s:textarea label="Address" name="address" cols="15" rows="5"/ >

< s:doubleselect label="Select Item"
headerValue="--- Please Select ---"
headerKey="1" list="{'Color','Fruits'}"
doubleName="dishes"
doubleList="top == 'Color' ? {'Black','Green','White',
'Yellow','Red','Pink'} : { 'Apple','Banana','Grapes','Mango'}" / >
< s:submit/ >
< /s:form >
< /body >
< /html >

Create display.jsp

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

< s:property value="address" / >

< s:property value="dishes"/ >

< s:property value="file1"/ >

< /body >
< /html >


Step 5:

Run the display.jsp

Output


Saturday, June 12, 2010

Struts2-UI TAGS-II


Struts2-UI-TAGS-II

Aim:
            We will learn the combobox and radio button and reset.


Procedure:

            Step 1:

            Create project for struts2.

            Step 2:
           
            Create Action class

            Step 3:

            Made some changes in struts.xml

            Step 4:

            Create home.jsp and display.jsp file

            Step 5:

            Run the home.jsp file
  
Step 1:

            Create project name as UI-tag II
 
Open the netbean 6.8

File—New Project New Project window will open.

Select java web under categories and select web Application under Projects.

Next

New Web Application window will open.

Give project name ( UI-Tags-II)
Next and also Next

Select Framework

You select struts2

Finish

The project UI-Tags-II has created successfully.

NOTE:

            Delete all files under example folder for web pages and source packages

Structure of the Struts2 Project


















Note:

            Store all jsp files under web pages

            Store all action class under sources packages


Step 2:

            Create Action class name as uiaction

Right click on example under Source Packages

New—Other—select Struts2 under categories and select Struts2 Action under File types.

Next

Give Action class name (uiaction)

Finish

The Action class uiaction has created successfully.


Include following coding into uiaction


package example;

import com.opensymphony.xwork2.ActionSupport;
public class uiaction extends ActionSupport
{
    /* combo box */
    private String country;

    /* radio button */
    private String sex;

    public String getSex()
    {
        return sex;
    }

    public void setSex(String sex)
    {
        this.sex = sex;
    }

    /* check box */
    public String getCountry()
    {
        return country;
    }
    public void setCountry(String country)
    {
        this.country = country;
    }
    public uiaction()
    {
    }
    public String execute() throws Exception
    {
        return "success";
    }
}

Step 3:

            Made some changes in struts.xml

< !DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
http://struts.apache.org/dtds/struts-2.0.dtd >

< struts >
< package name="example" namespace="/example" extends="struts-default" >
< action name="uiaction" class="example.uiaction" >
< result name="success" > /example/display.jsp < /result >
< result name="input" > /example/home.jsp < /result >
< /action >

< /package >
< /struts >

Step 4:

Create home.jsp


< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< title > JSP Page < /title >
< /head >
< body >
< s:form action="uiaction" method="post" >
< s:combobox label="Country" name="country"
list="{'India','Japan','England','Australia','Newzeland'}"
headerValue="--Select Country--"
headerKey="1"/ >

< s:radio label="Sex" list="{'Male','FeMale'}" name="sex"/ >


< s:submit value="OK"/ >

< s:reset label=”Clear” / >
< /s:form >
< /body >
< /html >

Create display.jsp

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

< title > JSP Page < /title >
< /head >
< body >
Country:< s:property value="country"/ >
< br >
Sex: < s:property value="sex"/ >
< br >

< /body >
< /html >


Step 5:

Run the display.jsp

Struts2-UI TAGS-II

Thursday, June 10, 2010

Struts2-UI TAGS-I


Struts2-UI Tags Example-I

AIM:

To understand how to use some UI tags such as label, textfield, password, form, submit.

Problem:

Create textfield for username and create password for password.

If you give correct username and password , the display.jsp file will display.

Otherwise the failure.jsp will display.

PROCEDURE:

Step 1:

            Create project Named UITags-I

Step 2:

            Create Action class named UIAction

            Include following coding into UIAction

package example;
import com.opensymphony.xwork2.ActionSupport;
public class UIAction extends ActionSupport
{
    private String username;
    private String password;

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public UIAction()
    {
    }

    public String execute() throws Exception
    {
       if((getUsername().equals("suresh"))&&(getPassword().equals("kumar")))
       {
           return "success";
       }
       else
       {
           return "failure";
       }

    }

}

Step 3:

            Made some changes in struts.xml

< !DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
http://struts.apache.org/dtds/struts-2.0.dtd >

< struts >

< package name="example" namespace="/example" extends="struts-default" >
< action name="uiaction" class="example.UIAction" >
< result name="input" > /example/register.jsp < /result >
< result name="success" > /example/display.jsp < /result >
< result name="failure" > /example/failure.jsp < /result >
< /action >
< /package >
< /struts >


Step 4:

Create register.jsp

Include following code into register.jsp


< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< title > Register Page < /title >
< /head >
< body >
< s:form action="uiaction" method="post" >
< s:textfield name="username" label="UserName"/ >
< s:password name="password" label="PassWord"/ >
< s:submit value="OK"/ >
< /s:form >
< /body >
< /html >


Step 5:

            Create display.jsp


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

< h1 > WELCOME < /h1 > < h2 > < s:property value="username" > < /s:property > < /h2 >

< /body >
< /html >


Step 6:

            Create failure.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< title > Register Page < /title >
< /head >
< body >
< h1 > Hello ! < s:property value="username"/ > -- Who Are You? < /h1 >
< h2 > Enter Correct Username and password < /h2 >
< /body >
< /html >


Step 7:

            Run register.jsp

Conclusion:

            Form this , we have understood how to use textfield, password and form and submit tags.

Directory Structure of this project