Showing posts with label actionmessage. Show all posts
Showing posts with label actionmessage. Show all posts

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.