Friday, July 2, 2010

Struts2 Data control Text tag Example


Struts2 Text Tag Example


Aim:
Text tag is a generic tag that is used to get values from properties. The properties file is saved where the java file is saved.

There are three ways to use text tag.

1). To get values from *.properties file

2). If the body of the text tag is not specified, the name of the text tag will display.

3). If the body of the text tag is specified, the body of the tag will display.


Procedure:

          Step 1:

          Create Project name as DataTextPro

          Step 2:

          Create Action as HelloWorld.java

          Include following coding within the


package example;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport
{

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

Step 3:

Create jsp file

HelloWorld.jsp



< %@ page contentType="text/html; charset=UTF-8" % >

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

< html >
< head >
< title > Text tag Example < / title >

< /head >

< body >
< s:text name="body not.message" > < /s:text >


< s:text name="message" > sureshkumar, welcome< /s:text >


< /body >
< /html >

Step 4:

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="textAction" class="example.HelloWorld" >
< result > /example/HelloWorld.jsp < /result >
< /action >
< /package >
< /struts >


Step 5:

          Run the HelloWorld.jsp

Directory structure of the project

Thursday, July 1, 2010

Struts2 Data control Bean Tag Example

Struts2 Bean Tag Example

Aim:
Bean tag is used to instantiates the bean class and set the value and gets the value.

Procedure:

Step 1:

Create Project name as BeanPro

Step 2:

Create Action class name as BeanTag

package example;

import java.io.Serializable;

public class BeanTag implements Serializable
{
private String name;

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

}

Step 3:

Create success.jsp

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

< title > JSP Page < /title >
< /head >
< body >
< s:bean name="example.BeanTag" id="uid" >
< s:param name="name" > SureshKumar < /s:param >
< s:property value="%{name}"/ >
< /s:bean >
< /body >
< /html >


Step 4:

Made some changes in struts.xml file

< !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="actionTag" class="example.actionTag" >
< result name="success" > /example/success.jsp < /result >
< /action >
< /package >
< /struts >



step 5:

Run the success.jsp

Directory structure of this project

Struts2 Data control Bean Tag Example

Struts2 Bean Tag Example

Aim:
            Bean tag is used to instantiates the bean class and set the value and gets the value.

Procedure:

            Step 1:

                        Create Project name as BeanPro

            Step 2:

                        Create Action class name as BeanTag

package example;

import java.io.Serializable;

public class BeanTag implements Serializable
{
    private String name;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

}

Step 3:

            Create success.jsp



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

< title > JSP Page < /title >
< /head >
< body >
< s:bean name="example.BeanTag" id="uid" >
< s:param name="name" > SureshKumar < /s:param >
< s:property value="%{name}"/ >
< /s:bean >
< /body >
< /html >

step 5:

            Run the success.jsp

Directory structure of this project


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