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


Sunday, June 6, 2010

Struts2-Dispatch Action Example-II


Struts2- Dispatch Action

AIM:

Dispatch action in struts2 is used to group the related function into the single action.

Problem:

            Index.jsp file contain two button one is One. Another is Two.

            If we click on One ,the one.jsp file will display.

            If we click on Two,thw\e two.jsp file will display.

Requirements:

            A). Action class(DisAction)

            B). jsp files(index.jsp,one.jsp,two.jsp)

            C). struts.xml


Procedure:

            Step 1:

                        Create Action

            Step 2:

                        Create  jsp files.(index.jsp,one.jsp,two.jsp)

            Step 3:

                        Made change in struts.xml

            Step 4:

                        Run index.jsp


Explanation:

Step 1:
            Create Action such as  DisAction

            Note:

            You follow the steps that have already posted in previous post.

            Include following coding into DisAction


package example;
import com.opensymphony.xwork2.ActionSupport;
public class DisBean extends ActionSupport
{

    public DisBean()
    {
    }

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

}

Step 2:

            Create index.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< title > Index < /title >
< /head >
< body >
< s:form action="User" method="post" >
< s:submit action="one" method="one" value="ONE"/ >
< s:submit action="two" method="two" value="TWO"/ >
< /s:form >
< /body >
< /html >


Create one.jsp


< html >
< head >
< title > Index < /title >
< /head >
< body >

< h1 > Hello World! < /h1 >

< b > You Are ONE < /b >

< /body >
< /html >



Create two.jsp

< html >
< head >
< title > Index < /title >
< /head >
< body >

< h1 > Hello World! < /h1 >

< b > You Are Two < /b >

< /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="example" namespace="/example" extends="struts-default" >
< action name="User" class="example.DisBean" >

< /action >
< action name="one" method="one" class="example.DisBean" >
< result name="one" > /example/one.jsp < /result >
< /action >
< action name="two" method="two" class="example.DisBean" >
< result name="two" > /example/two.jsp < /result >
< /action >
< /package >
< /struts >

Step 4:

Run the index.jsp


















If you click on ONE the output is

















If you click on TWO



Saturday, June 5, 2010

Struts2-Date Flicker Example


Struts2- Date Flicker Example

AIM:
            To perform Date Flicker using Struts2 and Netbean6.8 IDE.

Procedure:

            There are four steps to create this example

            Step 1:

                        Create Action

            Step 2:
                       
                        Create jsp files

            Step 3:

                        Made changes in struts.xml

            Step 4:

                        Run the file


           
Step 1:

Create action Named as (DateFlickerPro)

Already you have known how to create action-using netbean.

And include following coding into the DateFlickerPro

package example;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class DateBean extends ActionSupport
{
    private Date todate;

    public Date getTodate()
    {
        return todate;
    }

    public void setTodate(Date todate)
    {
        this.todate = todate;
    }

    public DateBean()
    {
    }

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

}

Step 2:

            Create jsp files.

            Create index.jsp

            Include following coding into index.jsp

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

< title > Date FlickerExample < /title >
< s:head theme="ajax"/ >

< /head >
< body >

< s:datetimepicker name="todate" label="Format (yyyy-MM-dd)" displayFormat="yyyy-MM-dd"/ >

< s:datetimepicker name="todate" label="Format (dd-MMM-yyyy)" displayFormat="dd-MMM-yyyy"/ >

< /body >
< /html >


Step 3:

            Made some change 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="FormDate" class="example.DateBean" >
< result name="success" > /example/index.jsp < /result >
< /action >
< /package >
< /struts >


Step 4:
             Run the index.jsp
You get output like below

Friday, June 4, 2010

Struts2-Date Example


Struts 2- Date Example


Aim:
To format the date. To learn how to format the date. There are many format available in struts2.

            There are many forms:

           
Format:
                        A). yyyy-MM-dd
                        B). MM/dd/yyyy 'at' hh:mm a
                        C). dd-MMM-yyyy 'at' hh:mm a
                        D). dd/MM/yyyy hh:mm
                        E). yyyy-MM-dd
Procedure:

Step 1:

            Create action support.

          Name: DateBean

          Right  click on example under source packages.

          New – other – struts2 – struts2 action.

          Next—give class name like DateBean.


You know how to create struts2 action using netbean6.8. if you forget the steps to create the struts2 action. Refer the previous post and continue this post.

Include the following code into the DateBean

package example;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class DateBean extends ActionSupport
{
    private Date todate;

    public Date getTodate()
    {
        return todate;
    }

    public void setTodate(Date todate)
    {
        this.todate = todate;
    }

    public DateBean()
    {
    }

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

}


Step 2:

          Create  jsp file.

          Name: index.jsp

          Right click on example under web pages.

New – other—select web under catogories – select JSP under file types

Next

Give jsp file name like index


            Include following the code into the index.jsp

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

< title > Date Example < /title >
< s:head theme="ajax"/ >
< /head >
< body >

< s:datetimepicker name="todate" label="Format (yyyy-MM-dd)" displayFormat="yyyy-MM-dd"/ >

< s:datetimepicker name="todate" label="Format (dd-MMM-yyyy)" displayFormat="dd-MMM-yyyy"/ >

< /body >
< /html >


Step 3:

          Change in struts.xml like below

 

 


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


Step 4:

            Run the index.jsp


Conclusion:

To understand how to display the date from this post. You will to try the examine the different format
           

Tuesday, June 1, 2010

Struts2-Double Validation


                              Struts 2 – Validation-Double Validation

Aim:
        The Double validator of Struts 2 Framework checks if the given input is double or not. If the input is not double, it generates the error message. Double validator can be used to check the input range.
Step 1:
         Create Project(named DoubleValidatePro)
         File—New Project
















New Project window will open like below.

















Where you select Java web under Categories and also select Web Application under Projects.
Next
New Web Application will open.
Where you give project name (DoubleValidatePro)
















Next
New web Application window will open.



















Where you select Server as Apache tomcat 6.0.20 and Java EE Version into Java EE 5.
Next
New Web Application window will open.
















Where you select Struts2
Finish.













You remove example under Web Pages and example under Source Packages and example.xml under
Step 2:
            Create Folder validate under web pages and also create Folder validate under Source Packages
Step 2.1:
            Right Click on Web Pages and New and Folder.
 












Where you give Folder name as validatedouble.
Step 2.2:
            Right Click on Source Packages and New – Folder.
New Folder window will open. Where you give Folder name as validatedouble.

Step 3:
            Create jsp file











Right click on validatedouble and New – other – New File window will open. Where you select web under categories and select JSP under file type.
Next

home.jsp file have created.
Insert following code into home.jsp

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

< html >
< head > < title > Home < /title > < /head >
< body >
< s:form action="doublevalidate" method="post" >
< s:textfield label="Applicant Hight" name="height"/ >
< s:submit >
< /s:form >
< /body >
< /html >


Step 4:
         Create Action (give action name as doublevalide)
         Right click on validatedouble under Source Packages.
New – Other – under categories – Struts2 / under file type Struts2 Action
Next
 New Struts2 Action window will open.
 Where you give action name as doublevalide.
Finish.
Include following coding into doublevalide


package validatedouble;
import com.opensymphony.xwork2.ActionSupport;
public class doublevalide extends ActionSupport
{
    private double height;

    public double getHeight()
    {
        return height;
    }

    public void setHeight(double height)
    {
        this.height = height;
    }


    public doublevalide()
    {
    }

    public String execute() throws Exception
    {
       if(getHeight()>155.5)
       {
           return "success";
       }
       else
       {
           return "error";
       }
    }

}
Step 5:
            Create xml file for validation.
Format


< Action class name > -validation.xml

doublevalide-validation.xml

include following coding into doublevalide-validation.xml

< ?xml version="1.0" encoding="UTF-8"? >
< !DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd >

< validators >

< field name="height" >
< field-validator type="double" >
< param name="minInclusive" > 155.5 < /param >
< param name="maxInclusive" > 165.5 < /param >
< message > Height need to between
${minInclusive} and ${maxInclusive} < /message >
< /field-validator >
< /field >

< /validators >


Step 6:
           Create success.jsp file

< html >
< head > < title > Success < /title > < /head >
< body >
You Are valid candidate
< body >
< html >


Step 7:
            Run the home.jsp