Starter Tutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » Advanced Java » Struts » Program to demonstrate java struts validation
Suryateja Pericherla Categories: Struts. No Comments on Program to demonstrate java struts validation
Join Our Newsletter - Tips, Contests and Other Updates
Email
Name

Aim

Write a program to demonstrate struts validation.

 

Theory

Struts Validation

We can define our own validation logic (custom validation) in struts 2 by implementing the Validateable interface in the action class. The workflow interceptor is used to get information about the error messages defined in the action class.

 

The workflow interceptor checks if there are any validation errors or not. It doesn’t perform any validation. It is applied when action class implements the Validateable interface. The input is the default parameter for this interceptor that determines the result to be invoked for the action or field error.

 

Validateable interface

The Validateable interface must be implemented to perform validation logic in the action class. It contains only one method validate( ) that must be overridden in the action class to define the validation logic. Signature of the validate method is:

public void validate();

 

ActionSupport class implements Validateable and ValidationAware interfaces, so we can inherit the ActionSupport class to define the validation logic and error messages.

 

Steps to perform custom validation

  1. Create the form to get input from the user
  2. Define the validation logic in action class by extending the ActionSupport class and overriding the validate method
  3. Define result for the error message by the name input in struts.xml file

 

Program

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
    
<struts>
	<package name="default" extends="struts-default">
		<action name="studentinfo" class="Student">
			<result name="input">/index.jsp</result>
			<result name="success">/success.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="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" id="WebApp_ID" version="5.0">
  <display-name>StrutsDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>
  
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

 

index.jsp

import com.opensymphony.xwork2.ActionSupport;

public class Student extends ActionSupport{
	
	private String rollno;
	private String name;
	
	public String execute() {
		return "success";
	}
	
	public String getRollno() {
		return rollno;
	}
	
	public String getName() {
		return name;
	}
	
	public void setRollno(String rollno) {
		this.rollno = rollno;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void validate() {
		if(rollno == null || rollno.trim().equals("")) {
			addFieldError("rollno","Regd. No. is required");
		}
	}
}

 

success.jsp

<html>
	<head>
	<title>Success</title>
	</head>
	<body>
		<p>Student information processed successfully.</p>
	</body>
</html>

 

Student.java

import com.opensymphony.xwork2.ActionSupport;

public class Student extends ActionSupport{
	
	private String rollno;
	private String name;
	
	public String execute() {
		return "success";
	}
	
	public String getRollno() {
		return rollno;
	}
	
	public String getName() {
		return name;
	}
	
	public void setRollno(String rollno) {
		this.rollno = rollno;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void validate() {
		if(rollno == null || rollno.trim().equals("")) {
			addFieldError("rollno","Regd. No. is required");
		}
	}
}

 

Input and Output

index.jsp

struts validation login page

 

With validation error

struts validation invalid details

 

Without validation error

struts validation login success

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory