Tuesday, August 21, 2012

STRUTS 2 Programs 011 validation Interceptor

Login.jsp
-------------
<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

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

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Validation Interceptor Example</title>

<s:head />

</head>

<body bgcolor="lightblue">
<s:actionerror />
<s:form action="Validation">
<s:textfield name="userName" label="User Name"></s:textfield>
<br>
<s:password name="password" label="Password"></s:password>
<s:submit />
</s:form>
</body>
</html>
-----------------------------------------------------------------------------------------------------------------------------------
LoginAction.java
---------------------------
package kites;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

    private static final long serialVersionUID = 525429611271529243L;

    private String userName;

    private String password;

    public String getUserName() {

        return userName;
    }

    public void setUserName(String userName) {

        this.userName = userName;
    }

    public String getPassword() {

        return password;
    }

    public void setPassword(String password) {

        this.password = password;
    }

    public String execute() throws Exception {

        return SUCCESS;
    }

    public void validate() {

        if (getUserName().length() == 0) {

            addFieldError("userName", "");

        } else if (!getUserName().equals("Rose")) {

            addFieldError("userName", getText("userName.incorrect"));

        }

        if (getPassword().length() == 0) {

            addFieldError("password", getText("requiredpassword"));

        } else if (!getPassword().equals("RoseIndia")) {

            addFieldError("password", getText("password.incorrect"));
        }

    }

}
------------------------------------------------------------------------------------------------------------------------
LoginAction.properties
------------------------------------
username.incorrect = UserName is incorrect.

password.incorrect = Password is incorrect.

requiredstring = ${getText(fieldName)} is required.

requiredpassword = ${getText(fieldName)} should be more than 6 character.

------------------------------------------------------------------------------------------------------------------------------
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="default" extends="struts-default">



<action name="Validation" class="kites.LoginAction">

<interceptor-ref name="params">
    <param name="excludeParams">dojo\..*,^struts\..*</param>

</interceptor-ref>

<interceptor-ref name="validation">

    <param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>

<interceptor-ref name="workflow">

    <param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>

<result name="input">/Login.jsp</result>

<result name="success">/success.jsp</result>

</action>

</package>

</struts>
------------------------------------------------------------------------------------------------------------------------------
success.jsp
--------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Login Successful</title>

</head>

<body bgcolor="lightblue">

<h1>Login Successful</h1>

</body>

</html>
-----------------------------------------------------------------------------------------------------------------------------
web.xml
----------------
<?xml version="1.0" encoding="UTF-8"?>

<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">

<display-name>Struts 2</display-name>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
-------------------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment