1. Create the project directory structure.
2. Create the View pages and forwarded the request to Action class.
index.jsp
[java]<%@ taglib uri="/struts-tags" prefix="s" %>
<s:form action="loginForm">
<s:textfield name="name" label="User Name"></s:textfield>
<s:textfield name="email" label="Email Id"></s:textfield>
<center>
<s:submit value="Sign In"></s:submit>
</center>
</s:form> [/java]
Here just created two text fields such as
User Name and
Email Id and also created
submit button.
success.jsp
[java]<%@ 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">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h1>Welcome to SPLessons</h1>
You have successfully signed in Itoolsinfo.com.
Employee Name : <s:property value="name" />
Employee Email : <s:property value="email" />
</body>
</html><span style="line-height: 1.5;"> [/java]
After enter the details if every thing is fine success message will be displayed.
3. Create the
Action class by extending
ActionSupport class.
CustomValidations.java
[java]package com.splessons;
import com.opensymphony.xwork2.ActionSupport;
public class CustomValidations extends ActionSupport
{
private String name;
private String email;
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public void validate()
{
if(name.length()<1 || name.equals(" Joshaf "))
{
addFieldError("name"," User name is Wrong ") ;
}
if(email.length()<1)
{
addFieldError("email", "Email can't be empty");
}
}
public String execute()
{
return "success";
}
}
[/java]
Here just performed validation code that is name should be Joshaf and user must need to enter Email id.
4. Configure the Action class in struts.xml file.
struts.xml
[xml]<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="loginForm" class="com.splessons.CustomValidations" >
<result name="success">success.jsp</result>
<result name="input">index.jsp</result>
</action>
</package>
</struts>
[/xml]
Where
success is a predefined result type.
action element is the sub component of package. It speaks to an activity to be conjured for the approaching request. It has name, class and method attributes. In the event that you don’t determine name property as a matter of course execute() technique will be summoned for the predetermined action class.
web.xml
[xml]<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
[/xml]
The
web.xml web application descriptor file represents the core of the Java web application, so it is appropriate that it is also part of the core of the Struts framework. In the web.xml file, Struts defines its FilterDispatcher, the Servlet Filter class that initializes the Struts framework and handles all requests. This filter can contain initialization parameters that affect what, if any, additional configuration files are loaded and how the framework should behave.
5. Run the application in the server, the browser display the output.
6.Enter all the required fields, then the browser display the output like.
7. If the values are not entered, then the browser display the output as some error message.