Monday, September 3, 2012

STRUTS2 Examples 014 Non Field Validators

home.jsp
--------------
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Home Page</title>
</head>
<body bgcolor="lightblue"><br><br><br><br><br>
<h1>Welcome </h1><s:property value="userName"/>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------
login.jsp
-------------
<%@ taglib prefix="s" uri="/struts-tags"%>
<link href="<s:url value="css/style.css"/>" rel="stylesheet"
type="text/css" />
<s:actionerror />
<center>
<h1>Please Login</h1>
</center>
<s:form method="POST" action="nonField">
<s:textfield name="userName" label="User Name" />
<s:textfield name="password" label="Password" />
<s:submit label="Submit" />
</s:form>
----------------------------------------------------------------------------------------------------------------------------------
LoginAction.java
---------------------------
package com.kites.action;

import com.kites.model.LoginModel;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LoginAction extends ActionSupport implements
        ModelDriven<LoginModel> {

    private static final long serialVersionUID = 1L;
    LoginModel model;

    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        if (!model.getUserName().equals("")) {
            return SUCCESS;
        }
        return INPUT;
    }

    @Override
    public LoginModel getModel() {
        // TODO Auto-generated method stub
        model = new LoginModel();
        return model;
    }

}

---------------------------------------------------------------------------------------------------------------------------
LoginAction-validation.xml
-------------------------------------------
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Home Page</title>
</head>
<body bgcolor="lightblue"><br><br><br><br><br>
<h1>Welcome </h1><s:property value="userName"/>
</body>
</html><!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>

<validator type="expression">
<param name="fieldName">userName</param>
<message>You must enter the User Name</message>
</validator>

<validator type="expression">
<param name="fieldName">password</param>
<message>You must enter Password</message>
</validator>

</validators>

--------------------------------------------------------------------------------------------------------------------------------
LoginModel.java
----------------------------
package com.kites.model;

import java.io.Serializable;

public class LoginModel implements Serializable {

    private static final long serialVersionUID = 1L;
    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 display(){
        return "input";
    }
}
-------------------------------------------------------------------------------------------------------------------------------
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.custom.i18n.resources" value="global" />
<constant name="struts.devMode" value="true" />

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

<action name="login" method="display" class="com.kites.model.LoginModel">
<result name="input">login.jsp</result>
</action>

<action name="nonField" class="com.kites.action.LoginAction">
<result name="success">home.jsp</result>
<result name="input">login.jsp</result>
</action>

</package>

</struts>
-------------------------------------------------------------------------------------------------------------------------------
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