• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Struts 2 validation framework

July 2, 2012 By j2eereference Leave a Comment

Here let’s see how struts2 validation happens with XML configuration.

Lets take an example of a login page , here we will check any of the fields are blank using struts 2 validation framework.

For this we need the following files.

  • web.xml                                    (WEB-INF)
  • struts.xml                                 (WEB-INF\classes)
  • struts.properties(Optional)  (WEB-INF\classes)
  • LoginAction.java                     (WEB-INF\classes\com\j2eereference\struts2\action)
  • LoginAction-validation.xml  (WEB-INF\classes\com\j2eereference\struts2\action)
  • UserVO.java                              (WEB-INF\classes\com\j2eereference\struts2\vo)
  • login.jsp                                     (\)

The Struts 2 validation framework uses xml based configuration file. The file name should be < your action class name> -validation.xml. In our case our action class name is Login.java, so our validation configuration file will be Login-validation.xml.

One important thing we need to notice that the validation XML should be saved in the same location of your LoginActionClass ,  In our example its under WEB-INF\classes\com\j2eereference\struts2\action

Now lets see each of the above files closely.

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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_2_5.xsd"
&nbsp;id="WebApp_ID" version="2.5">
<display-name>j2eereference</display-name>
 
<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>

struts.xml

1
2
3
4
5
6
7
8
9
10
11
12
<!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="loginAction" method="validateUser">
     <result name="input">/login.jsp</result>
     <result name="success">/success.jsp</result>
     <result name="failure">/failure.jsp</result>
&nbsp; </action>
</package>
</struts>

 

LoginAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.j2eereference.struts2.action;
 
import javax.servlet.http.HttpSession;
import com.j2eereference.struts2.vo.UserVO;
import com.opensymphony.xwork2.ActionSupport;
 
public class LoginAction extends ActionSupport {
 
private static final long serialVersionUID = 1L;
private UserVO userVO;
private String returnValue;
 
HttpSession session;
 
public String validateUser()
{
String userName=userVO.getLoginUserName();
String password=userVO.getLoginUserName();
/* Call your business logic layer do the validation,as per the result assign
success or failure to the variable returnValue */
 
return returnValue;
    }
 
    public UserVO getUserVO() {
return userVO;
}
public void setUserVO(UserVO userVO) {
this.userVO = userVO;
}
 
}

 

LoginAction-validation.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="userVO.loginUserName">
<field-validator type="requiredstring">
<message>User Name is required.</message>
</field-validator>
</field>
 
<field name="userVO.password">
<field-validator type="requiredstring">
<message>Password is required.</message>
</field-validator>
</field>
</validators>

UserVo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.j2eereference.struts2.vo;
 
public class UserVO {
 
private String loginUserName;   
private String password;
 
public String getLoginUserName() {
return loginUserName;
}
public void setLoginUserName(String loginUserName) {
this.loginUserName = loginUserName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
 
}

login.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<html xmlns="http://www.w3.org/1999/xhtml">
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<head>
<title>j2eereference</title>
</head>
<body>
<s:form name="loginForm" method="post" action="loginAction">
<table&nbsp; width=50%>
<s:fielderror cssStyle="color: red;font-family:Arial, Verdana, sans-serif;font-size:12px;"/>
<tr>
<td>&nbsp;</td><td>User Name</td><td><s:textfield name="userVO.loginUserName"/></td>
</tr>
<tr>
<td>&nbsp;</td><td>Password</td><td><s:password name="userVO.password"/> </td>
</tr>
<tr>
<td>&nbsp;</td><td></td><td align=right><s:submit/></td>
</tr>
</table>
</s:form>
</body>
</html>

struts.properties     :  This file is for setting the theme . If we are not setting this, struts will set the default theme.

1
struts.ui.theme=simple

Now lets see the screen shot by running  the application

Here when you click on the submit button without entering the password , you will get an error message on top saying that password is required.

Here you can notice that

  • The error messages we are defining in the LoginAction-validation.xml .
  • Color and style of the message in jsp using the struts tag as below.
    1
    <s:fielderror cssStyle="color: red;font-family:Arial, Verdana, sans-serif;font-size:12px;"/>

Filed Under: Struts

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.