Home Struts Struts Struts2.2.1 Using Non Field Validators

Ask Questions?

View Latest Questions


 
 

Using Non Field Validators
Posted on: February 14, 2011 at 12:00 AM
Advertisement
In this tutorial you will learn how do validation on server side using struts non-field validator

Using Non Field Validators

You can use non-field validators for server side validation in your application as.

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

An Example using non field validators is goven below

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>

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>

SampleInterfaceImp.java

package net.roseindia.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";
	}
}

LoginAction.java

package net.roseindia.action;

import net.roseindia.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>

SampleInterfaceImp.java

<?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="roseindia" namespace="/" extends="struts-default">

<action name="login" method="display" class="net.roseindia.model.LoginModel">
<result name="input">resources/login.jsp</result>
</action>

<action name="nonField" class="net.roseindia.action.LoginAction">
<result name="success">resources/home.jsp</result>
<result name="input">resources/login.jsp</result>
</action>

</package> 

</struts>


When you run this application it will display message as shown below:


 
 

Download Select Source Code

Related Tags for Using Non Field Validators :


DMCA.com