Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Managing Error Handling with Struts Actions

In this article, we will see how to manage Error Handling in struts framework. Also we will learn why we need to validate the web application activities and their types.

Struts action class is one of the most important classes in struts framework. The action class holds the main execution piece of code. So in this framework, the action class is also used to handle errors. In this article we will discuss the error handling part of struts action class.

Web application is being very complicated day by day with their performance point of view. It should be very light-weighted to be loaded. While user interacts with the web application, it is necessary to validate each and every activities is made for the valid information by the user. This validation can be made at the client side or at the server side. The client side validation helps to remove occurrence of error message before hitting the application server. But The server side validation are made, while the information has been submitted to the server. The Struts framework helps us to handle server side error into the web application.

Struts Action Error Handing in Details:

Struts framework is very easy and light-weight mechanism to handle server side errors occurred in a web application. Struts defines action library to manage these errors. It provides two main classes that help us to manage error occurred into the web application named ActionError and ActionMessage.

ActionError class is used to send error status to the user using struts tag library, where is uses tag <s:actionerror/> to inform user about error status.

Listing 1: ActionError Error Tag Example

<s:if test="hasActionErrors()">
   <div class="errors">
      <s:actionerror/>
   </div>
</s:if>

Listing 1 defines an example to handle the error against the invalid information input the user into the web application form. It checks for the error status using the method “hasActionError()” method and displays the error message, if there is an error is returned into the “error” div.

ActionMessage class is used to send message or information to the user using struts tag library, where tag <s:actionmessage/> is used to place the message to the user.

Listing 2: ActionMessage Information Tag Example

<s:if test="hasActionMessages()">
   <div class="welcome">
      <s:actionmessage/>
   </div>
</s:if>

Listing 2 defines the ActionMessage example that validates the message status using “hasActionMessage()” method, if there is a message to be displayed, it is rendered using tag <s:actionmessage> and displayed into div.

Let’s go with an example that will manage login of the user and will display the welcome message to the user with its name, if successful, otherwise an “invalid login information” error message will be displayed to the user.

Now let’s create a properties file for the application that will use specific description for the user as in Listing 3:

Listing 3: global.peoperties

#Global messages
global.username = Username
global.password = Password
global.submit = Submit
global.reset = Reset

Listing 3 defines global.properties file that defines username, password, submit and reset variables to its specific pre-defined values.

Now let’s define a LoginAction.java java action class file into Listing 4 as:

Listing 4: LoginAction.java

package com.test.actionManager;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

	private String username;
	private String password;
	 
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	//business logic
	public String execute() {

		return "SUCCESS";

	}

	//simple validation
	public void validate(){
		if("umesh".equals(getUsername()) && "umesh".equals(getPassword())){
			addActionMessage("Valid User Information!");
		}else{
			addActionError("Invlaid Login Information!");
		}
		
	}	
}

Listing 4 defines LoginAction.java class file that defines two String type class member variables that should have the same name as defined into .jsp file of the login page and also defines getter() and setter() methods to these variables. It also defines execture() and validate() methods. The execute() method return “SUCCESS” that informs struts to load the result “SUCCESS” with .jsp file “welcome.jsp” else “login.jsp” file.

Now let’s define struts.xml configuration file into Listing 5:

Listing 5: struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
	<constant name="struts.custom.i18n.resources" value="global" />
	<package name="user" namespace="/user" extends="struts-default">
		<default-action-ref name="login"/>
		<action name="login">
			<result>pages/login.jsp</result>
		</action>
		<action name="validateUser" class="com.test.actionManager.LoginAction">
			<result name="SUCCESS">pages/welcome.jsp</result>
			<result name="input">pages/login.jsp</result>
		</action>
	</package>
</struts>

Listing 5 defines tag that defines namespace “user” with its two actions “login”, “invalidUser”. The action “login” is loaded automatically using tag <default-action-ref> tag. The action “login” loads “login.jsp” page and “validateUser” loads “welcome.jsp” page.

Now let’s define login.jsp file into Listing 6 as:

Listing 6: login.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>

<style type="text/css">
.errors {
	background-color:#F6CEE3;
	border:1px solid #F5A9E1;
	width:200px;
	font-family: san-sarif;
	margin-bottom:8px;
}
.errors li{ 
	list-style: none; 
}
</style>

</head>

<body>
<h1>Action Error Management Example</h1>

<s:if test="hasActionErrors()">
   <div class="errors">
      <s:actionerror/>
   </div>
</s:if>

<s:form action="validateUser">
	<s:textfield key="global.username" name="username"/>
	<s:password key="global.password" name="password"/>
	<s:submit key="global.submit" name="submit"/>
</s:form>

</body>
</html>

Listing 6 defines the login form with the variable names “username” and “password” that matches “LoginAction.java” class member variables. It picks “username” and “password” values from properties file using “global.username” and “global.password” variables with “global.submit” variable to “submit” button. The login form hits to “validateUser” action class file and perform the appropriate action and loads the relevant jsp file. It also test the error message using method “hasActionError()” into the tag <s: if test = “”/> and it is displayed into the “error” class div using tag <s:actionerror/>

Let’s define the welcome.jsp page into Listing 7 as:

Listing 7: welcome.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>

<style type="text/css">
.welcome {
	background-color:#DDFFDD;
	border:1px solid #009900;
	width:200px;
}
.welcome li{ 
	list-style: none; 
}
</style>

</head>
<body>
<h1>Struts Login Manager Application Welcome Page</h1>

<s:if test="hasActionMessages()">
   <div class="welcome">
      <s:actionmessage/>
   </div>
</s:if>

<h4>

<s:property value="getText('welcome.hello')" /> : 
<s:property value="username"/>

</h4>

</body>
</html>

Listing 7 defines the welcome.jsp page that welcome the user with its name and the welcome message using the tag <s:if test = “hasActionMessage()”> and displays the message into the “welcome” class div using tag <s:actionmessage/>. To get access the username into the welcome page, property tag <s:property value = “username”/> is used and to get welcome message property tag <s:property value = “getText(‘welcome.hello’)”/> is used.

Let’s define web.xml file into Listing 8 as:

Listing 8: web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Struts Login Manager</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>

Listing 8 defines the web.xml configuration file that defines the struts filter dispatcher class mapping to all the hitting urls. All the url should hit and transferred to struts.xml file to perform the relevant action using class name “org.apache.struts2.dispatcher.FilterDispatcher”.

Now, lets deploy this application and visit this url: http://localhost:8080/Struts2Example/user/ that loads page as displayed into Figure 1:

Login.jsp page

Figure 1: Login.jsp page

Figure 1 displays the login page that accepts the username “umesh” and password “umesh” to be logged in. if the username and password is invalid, it will displayed as shown in Figure 2:

Login.jsp with invalid login information error

Figure 2: Login.jsp with invalid login information error

Figure 2 shows the invalid login information error message, but if the user inputs the valid information, it will be redirected to welcome.jsp page as displayed into Figure 3:

welcome.jsp

Figure 3: welcome.jsp

Figure 3 displays the welcome.jsp page with the welcome message and username entered by the user.

Required Tools and Applications:

To run this application, we are required to have following tools and applications:

  • Eclipse IDE (Eclipse Juno)
  • JDK Run time environment (JDK 7)
  • Web or Application Server (Tomcat Apache)
  • Set of Struts 2 jar files as shown in figure 3
Set of Struts 2 Jar Files

Figure 4: Set of Struts 2 Jar Files

Figure 4 shows the struts 2 jar files to run struts 2 web application.

Conclusion:

In this article, we learn about the need of ActionError and ActionMessage class and also learn about way of using these classes into the struts web applications. We also learn about the use of properties file into your web applications.



Working in Software Development domain from 7 years now and is well equipped with programming languages like HTML, CSS, Java, PHP, .NET etc.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login