Showing posts with label struts2 xml based validation. Show all posts
Showing posts with label struts2 xml based validation. Show all posts

Tuesday, April 28, 2015

STRUTS2 VISITOR VALIDATOR COMPLETE EXAMLE




Login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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=ISO-8859-1">
<title>Login</title>
<style>
    .errorMessage{
        color:red;
        font-size: 11px;
        text-align:right;
    }
</style>
</head>
<body>
    <center>
        <s:form action="loginAction" method="post" validate="true" >
            <s:textfield     name="user[0].username" label="Username"    />
            <s:textfield     name="user[0].password" label="Password"    />
            <s:textfield      name="mobileNumber" label="Mobile Number"    />          
            <s:submit/>
        </s:form>
    </center>
</body>
</html>

LoginAction.java

import java.util.List;

import mod.User;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;

public class LoginAction extends ActionSupport{
   
    private String mobileNumber;
    private List<User> user;
   
    public String getMobileNumber() {
        return mobileNumber;
    }
   
    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }
   
    @VisitorFieldValidator(message = "Userssss")
    public List<User> getUser() {
        return user;
    }

    public void setUser(List<User> user) {
        this.user = user;
    }
}
 

LoginAction-validation.xml
 
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
   
<validators>
   
    <field name="mobileNumber"  >
        <field-validator type="requiredstring" short-circuit="true" >
            <message>Empty Mobile Number !</message>            
        </field-validator>
    </field>
   
</validators> 

User.java

package mod;

public class User {
   
    private String username;
    private String password;
    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }
    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }
    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }
    /**
     * @param password the password to set
     */
   
    public void setPassword(String password) {
        this.password = password;
    }
}
 
welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    WELCOME TO CUSTOM VALIDATION DEMO PROJECT
</body>
</html>

Thanks
Keep Visiting. Be happy :)





Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...