Non User Interface tag - actionerror

actionerror : It is used to display errors of Action class. The Action error can be set using addActionError() method. It is action level errors and <s:actionerror/> tag is used to display action errors .

<s:actionerror />
<s:form .... >
....
</s:form>

Lets demostrate actionerror tag. I have created employee registration form to cater employee information. If user submit this form without any entry, then user will get error message on the same page.

Follow the following steps to create this project :
Step 1: Create a Dynamic Web Project in eclipse - "Struts2ActionError".

Step 2: The Struts2 library jar files required in WEB-INF\lib folder in your project. Please read Readme.txt file in the attcahed source code for more details.

Step 3: Create a View page index.jsp :
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Action Error Tag</title>
</head>
<body>
<h2>Non UI - actionError tag</h2>
<hr>
<font color='red'> <s:actionerror /> </font>
<s:form action="ActionError" method="post">
<s:textfield name="id" label="Enter Id" />
<s:textfield name="name" label="Enter Name" />
<s:textfield name="city" label="Enter City" />
<s:textfield name="salary" label="Enter Salary" />
<s:submit label="Submit" />
</s:form>
</body>
</html>

Step 4: Create action class ActionError.java :

In the action class, validate method will be execute before execute method. In the validate method use addActionError() to add error on action level based on business logic.
package techmyguru.nonuitags;
import com.opensymphony.xwork2.ActionSupport;
public class ActionError extends ActionSupport {
private String id, name, city;
private double salary;
// getter and setter methods
@Override
public String toString() {
return id + "\t" + name + "\t" + city + "\t" + salary;
}
@Override
public void validate() {
if (id.trim().length() == 0)
addActionError("Id can not be blank");
if (name.trim().length() == 0)
addActionError("Name can not be blank");
if (city.trim().length() == 0)
addActionError("City can not be blank");
if (salary== 0)
addActionError("Salary can not be blank");
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}

Step 5: Configure Action in struts.xml file :
<?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">
<action name="ActionError" class="techmyguru.nonuitags.ActionError">
<result name="input">/index.jsp</result>
<result name="success">/WEB-INF/pages/EmpSuccess.jsp</result>
</action>
</package>
</struts>


Step 6: Create target View page /WEB-INF/pages/EmpSuccess.jsp :
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h1>
Employee Details are<br>
<hr>
Id :<s:property value="id" /><br>
Name : <s:property value="name" /><br>
City : <s:property value="city" /><br>
Salary : <s:property value="salary" /><br>
</h1>
<hr>
<a href="index.jsp">Home</a>
</body>
</html>


Step 7: Verify the project directory structure :
Struts2 actionerror tag

Step 8: Run the project and get output :
Struts2 actionerror tag

Click Submit button without entry any value and get error in the same page as below :
Struts2 actionerror tag
Enter all the values and click submit button. You will get the below output :
Struts2 actionerror tag


Previous Download Source Code Next


Write your Comment

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