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

Monday, May 24, 2010

Strut2-String Validation


Struts 2 – String Validation


Aim:

            It is very important one to validate user input when we are going to create the web project. So we will learn how to validate the user input such as String , integer, Email etc,.

Procedure:

            Step_1:

                        Create ActionSupport

                       Create Folder example under source packages(if it dose not).

                       If it is, we use already created Folder example.

                        Right click on example

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

Next where you give class name and click Finish.


Include following coding into the action support

package example;
import com.opensymphony.xwork2.ActionSupport;

public class StringVaLidationAction extends ActionSupport{

  private String username;

  public String execute() throws Exception
  {
    if (getUsername() != null)
    {
      return SUCCESS;
    }
    else
    {
      return ERROR;
    }
  }

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



Step_2:

Create jsp file

Create Folder example under web pages

Right click on example . New other web jsp you give jsp name ( I have used jsp name as stringInputForm.jsp.

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

< html >

< head >
< title > Input form < /title >
< s:head/ >
< /head >

< body >

< s:form method="POST" action="stringValidation" >

< s:textfield label="Enter User Number" name="username" maxlength="10" / >

< s:submit / >
< /s:form >

< /body >

< /html >


Step_3:

Create stringSuccess.jsp

Include following code into the string Success.jsp

< %@page language="java" %>
< html >

< head >
< title > Correct entry < /title >
< /head >

< body >
< b > Welcome to < /b > < %=request.getParameter("username") % > !
< /body >

< /html >


Step_4:

Made come changes in struts.xml.

Source package
|
|
< default package >
|
|
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 >

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

< action name="stringValidation" class="example.StringVaLidationAction" >

< result name="input" > /example/stringInputForm.jsp < /result >
< result name="error" > /example/stringInputForm.jsp < /result >
< result name="success" > /example/stringSuccess.jsp < /result >
< /action >

< /package >

< /struts >









Step_5:


Structure of the application


Step_6:

Run stringInputForm.jsp

Right click on stringInputForm.jsp

Conclusion:


From this post we have learnt how to validate String input. You have any doubt, you will post comments.


Saturday, May 22, 2010

Struts 2 - Login Application


Struts2 Application-Login page Application
Aim:
          To develop login application using struts2 framework.

Software Requirements:
           A). NetBean 6.8
                 www.netbeans.org/

                B). Download plugin for struts2  
                http://plugins.netbeans.org/PluginPortal/

Requirement:
              1). HelloWorld.jsp
             2). Success.jsp
             3). Failure.jsp
             4). Made some changes in example.xml
             5). Include example.xml into struts.xml
             6). Create Helloworld.java


Procedure:

        Step_1: Open NetBean 6.8



Step_2: Goto Fileà New Project. The New Project window will open. Where you select  under categories Java Web and under Projects web Application . And Click Next. The New Web Application window will open. Where you give project name ( I have given project name as LoginproA). Click Next and you select Apache server and click Next and the New web Application window will open. Where you select checkbox for struts 2. Click Finish. Now you have successfully created the struts project.

            Step_3: Structure of the project after creating 






Step_4: Creating jsp file

Web pages
|
|
example
|
|
*.jsp file



Step_4.1:Create HelloWorld.jsp

    Right click on example under web pages.
    Newàotherà under categories web – under File types jsp
    Give jsp file name as HelloWorld.jsp
    Include following coding into HelloWorld.jsp

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

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

< html >
< head >
< title > Login Application < /title >
< /head >
< body >
< s:form action="login" >
< s:textfield name="username" label="UserName"/ > < br >
< s:password name="password" label="PassWord"/ > < br >
< s:submit value="Login" label="Login"/ >
< /s:form >
< /body >
< /html >

Step_4.2: Create success.jsp

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

< html >
< head >
< title > Login Application < /title >
< /head >
< body >
< h2 > Success < /h2 >
< /body >
< /html >


Step_4.3: Create failure.jsp



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

< html >
< head >
< title > Login Application < /title >
< /head >
< body >
< h2 > Failure < /h2 >
< /body >
< /html >


Step_5: Creating action file

                                Source Package
                                                |
                                                |
                                         example
                                                |
                                                |
                                        *.java
Step_5.1:Create HelloWorld.java under source package
Include following coding
        
package example;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld 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 String execute() throws Exception
    {
        if(getUsername().equals("suresh")&&(getPassword().equals("kumar")))
        {
            return "success";
        }
        else
        {
            return "failure";
        }
    }
 }

Step_6: Configuration file

           There are two configuration file
           One is example.xml
           Another one is struts.xml

Source Package
|
|
< default-package >
|
|
example.xml
struts.xml

example.xml

include following code into example.xml


…….
…….

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


struts.xml

include following coding into struts.xml

……
……

< struts >
< include file="example.xml"/ >
< !-- Configuration for the default package. -- >
< package name="default" extends="struts-default" >
< /package >
</struts >


Step_7:

Run HelloWorld.jsp à right click on HelloWorld.jspà run file

Conclusion:

From this post, how to create structure and how to create jsp file and class file .

How to run the struts2 application.You have any doubt give comments.