More in Struts2 Action

Struts2 framework provides to validate data in two ways. Firstly you can validate in action class and secondly using validation framework. Here I would cover validation in action class. To validate data in action calss, you create action class by extending ActionSupport class and override validate() method. The validate() method doesn't return any errors, instead it stores all the errors with the help of the ValidationAware interface.When any request goes from client to action, the framework execute validate() method first, if there is any errors then it returs Action.INPUT. You have to configure input result name in struts.xml file.

Add action level error messages :
  • addActionError("Employee code is not valid");
  • addActionError("Employee Name is not valid");
  • addActionError("City is not valid");
  • addActionError("Salary is not valid");
Add filed level error messages :
  • addFieldError("code", "Employee code is not valid");
  • addFieldError("name","Employee Name is not valid");
  • addFieldError("city","City is not valid");
  • addFieldError("salary","Salary is not valid");
Add action level messages :
addActionMessage("You have entered correct data");

Render action errors in View page :
<s:actionerror/>

Render fields error in View page :
<s:fielderror/>

Render action message in View page :
<s:actionmessage/>

Default action in Struts2

It is used when an action is requested in struts.xml configuration, framework does not map the the requested action name.You can specify default action. If no action matches in the struts.xml then the default action is used to display a JSP page.It is used, If you do not want to display error page.

<package name="default" extends ="struts-default">
<default -action-ref name="somedefault" />
<action name="somedefault">
<result>/WEB-INF/pages/defaultpage.jsp</result>
</action>
-----------
</package >

Other than execute() method in Action

If you other than execute() method in action class then you have to configure in struts.xml. Struts2 framework searches execute() method in action class by default, If method name is not passed with action tag in struts.xml configuration file. Here EmployeeAction2 action mapped with techmyguru.actions.EmployeeAction2 class and myexecute method. The framework execute myexecute() method instead on execute() method.

<action name="EmployeeAction2" class ="techmyguru.actions.EmployeeAction2" method="myexecute">
<result name="success">/WEB-INF/pages/EmployeeSuucess.jsp</result>
<result name="input">/index.jsp</result>
</action>

Find the complete running Struts2 example of Validate method, default action and other method other than execute() from action

Step 1: Create a dynamic web project "Struts2Actions1" as below :
Create Struts2 web project

Step 2: Edit deployment descriptor web.xml. [ Click here If you are not familiar with Struts2 application environment setup in Eclipse. ]

Step 3: Copy the required jar files in WEB-INF\lib folder
commons-collections-3.1.jar
commons-digester-2.0.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.8.jar
xwork-core-2.3.8.jar


Step 4: Create input page "index.jsp" :
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<s:head />
<title>More in Action class</title>
</head>
<body>
<h2>Employee Registration</h2>
<s:actionerror />
<s:fielderror />
<s:form action="EmployeeAction1" method="post">
<s:textfield label="Enter Code" name="code" />
<s:textfield label="Enter Name" name="name" />
<s:textfield label="Enter City" name="city" />
<s:textfield label="Enter Salary" name="salary" value="0" />
<s:submit label="Submit" />
<s:reset label="Reset" />
</s:form>
<hr>
Default action demo :
<a href="UnknownAction.action">Unknown Action</a>
<br>
<hr>
<h2>Other than execute method in Action class</h2>
<s:form action="EmployeeAction2" method="post">
<s:textfield label="Enter Code" name="code" />
<s:textfield label="Enter Name" name="name" />
<s:textfield label="Enter City" name="city" />
<s:textfield label="Enter Salary" name="salary" value="0" />
<s:submit label="Submit" />
<s:reset label="Reset" />
</s:form>
</body>
</html>

Step 5: Create action class EmployeeAction1.java :
Here I am adding action level error message based on condition that none of the field should left blank
package techmyguru.actions;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class EmployeeAction1 extends ActionSupport {
private String code;
private String name;
private String city;
private Double salary;
// all getter/setter methods
@Override
public void validate() {
if (getCode() == null || getCode().trim().length() == 0) {
addActionError("Employee code is not valid");
}
if (getName() == null || getName().trim().length() == 0) {
addActionError("Employee Name is not valid");
}
if (getCity() == null || getCity().trim().length() == 0) {
addActionError("City is not valid");
}
if (getSalary() <= 0) {
addActionError("Salary is not valid");
}
}
@Override
public String execute() throws Exception {
addActionMessage("You have entered correct data");
return SUCCESS;
}
}

Step 6: Create action class EmployeeAction2.java :
In action you can define other than execute() method. Here I define myexecute() method in action class
package techmyguru.actions;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class EmployeeAction2 extends ActionSupport {
private String code;
private String name;
private String city;
private Double salary;
// all getter/setter methods
@Override
public void validate() {
if (getCode() == null || getCode().trim().length() == 0) {
addFieldError("code", "Employee code is not valid");
}
if (getName() == null || getName().trim().length() == 0) {
addFieldError("name","Employee Name is not valid");
}
if (getCity() == null || getCity().trim().length() == 0) {
addFieldError("city","City is not valid");
}
if (getSalary() <= 0) {
addFieldError("salary","Salary is not valid");
}
}
public String myexecute() throws Exception {
addActionMessage("You have entered correct data");
return "success";
}
}

Step 7: Create struts.xml :
The action tags has many attributes, but three are most common, name is mandatory and name of the action which is requested by the client, class must be fully qualified Action class name if any, method- if action class has different method name other than execute(), then method name has to be mapped in struts.xml.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" extends="struts-default">
<default-action-ref name="somedefault" />
<action name="somedefault">
<result>/WEB-INF/pages/defaultpage.jsp</result>
</action>
<action name="EmployeeAction1" class="techmyguru.actions.EmployeeAction1">
<result name="success">/WEB-INF/pages/EmployeeSuucess.jsp</result>
<result name="input">/index.jsp</result>
</action>
<action name="EmployeeAction2" class="techmyguru.actions.EmployeeAction2"
method="myexecute">
<result name="success">/WEB-INF/pages/EmployeeSuucess.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>

Step 8: Create view component page EmployeeSuccess.jsp :
The OGNL expression language is in build feature in Struts2. It is used to get the value from ValueStack by putting property name in the property tag.
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Employee Suucess Page</title>
</head>
<body>
<h2>Employee details are<br>
<s:actionmessage/>
Employee Code :<s:property value="code"/><br>
Employee Name :<s:property value="name"/><br>
Employee City :<s:property value="city"/><br>
Employee Salary :<s:property value="salary"/><br>
<a href="index.jsp"> Try Agaian</a>
</h2>
</body>
</html>

Step 9: Create defaut view page "WEB-INF\pages\defaultpage.jsp"
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Default Action</title>
</head>
<body>
<h2>Not valid request</h2>
<a href="index.jsp">Home</a>
</body>
</html>

Step 10: Run the application and see the output
It is action level error messages


It is filed level error messages


It is success page


It is default action page


Previous Download Source Code Next

Comment by : amit June 18th, 2016
Comment : i getting this error, Caused by: java.lang.ClassNotFoundException: com.opensymphony.xwork2.config.FileManagerProvider. can u please help me out on this?

Comment by : michael kors factory May 21st, 2014
Comment : Thanks for another wonderful post. Where else could anybody get that type of information in such an ideal way of writing? I have a presentation next week, and Im on the look for such information. [


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code