You are on page 1of 9

Struts : Validation Framework (Validation Method in Action Form)

Jsp page :

<html:errors/>

<html:form action="/Login">

User Name <html:text property="userName" />


Password : <html:password property="password"/> <br>

<html:submit value="Login" />

</html:form>

Action Form :
package com.login;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginForm extends ActionForm {

private String userName;


private String password;

//Getter & Setter Method


public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

ActionErrors actionErrors = new ActionErrors();

if (userName == null || userName.equalsIgnoreCase("")) {

actionErrors.add("userName", new ActionMessage("error.name"));

if (password == null || password.equalsIgnoreCase("")) {

actionErrors.add("password", new ActionMessage("error.password"));

return actionErrors;

public void reset(ActionMapping mapping, HttpServletRequest request) {

userName = "";
password = "";
}

ApplicationResource.properties

error.name=<b>User Name </b>can not be empty.

error.password=Password can not be empty.


Struts : Validation Framework ( Validation.xml )

Jsp page :

<body>

<html:javascript formName="loginForm" dynamicJavascript="false"/>

<br/>

<html:errors/>

<html:form action="/Login" onsubmit="return validateLoginForm(this);">

User Name <html:text name="loginForm" property="userName" />

Password : <html:password name="loginForm" property="password"/> <br>

<html:submit value="Login" />

</html:form>

</body>

Action Form :
package com.login;
import org.apache.struts.validator. ValidatorForm;
import org.apache.struts.action.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginForm extends ValidatorForm {

/** Creates a new instance of LoginForm */

private String userName;

private String password;

//Getter & Setter Method

ApplicationResource.properties

loginForm.userName = User Name

loginForm.password = Password

Struts-config.xml
Validation.xml
DYNA ACTION FORM & Server Side Error Handling

Jsp page :

<html:form action="/Login">

User Name <html:text property="name" />


Password : <html:password property="email"/> <br>

<html:submit value="Login" />

</html:form>

Struts-Config.xml
Action Class

You might also like