Interceptors in struts 2

Interceptors:

Interceptor is an object which is called at the pre processing and post processing of a request. These are same as the filters in the servlet.

Use of interceptors in struts 2.

  1. Interceptors are used to perform some pre-processing task before the action is invoked.
  2. Interceptors are used to perform some post-processing task after the action is invoked.
  3. Interceptors are used to perform operations like validation, internationalization etc
  4. Interceptors can also be used to catch exceptions.

Commonly used default interceptors of struts 2.

  1. alias: It allows same parameters to have different alias name across requests.
  2. checkbox: It is used to manage the check boxes by adding the false value for unchecked checkboxes.
  3. conversionError: It is used to add the conversion errors into the action’s field errors.
  4. execAndWait: It is used to send the user to an intermediate waiting page while action is executed.
  5. i18n: It is used to track the selected locale during a session. It provides the internationalization and localization facilities.
  6. debugging: It provides debugging support to the developer.
  7. exception: It provides the automatic exception handling facility by mapping the exception from an action to the result.
  8. fileUpload: It provides the facility to upload files.
  9. logger: It outputs the executed action name.
  10. validation: It provides the validation support.

Struts 2 logger interceptor example:

test.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
	<head>
		<title>Struts 2 logger interceptor example</title>
	</head>
	<body>
		<h3>This is a logger interceptor example.</h3>
 
		<s:form action="test">
			<s:submit value="Hello" align="center"/>
		</s:form>
 
	</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/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>
 
	<welcome-file-list>
	  <welcome-file>test.jsp</welcome-file>
	</welcome-file-list>
 
</web-app>

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
	<package name="user" extends="struts-default">
		<action name="test" 
		          class="com.w3spoint.action.Test">
			<interceptor-ref name="logger"/>
			<result name="success">/welcome.jsp</result>
		</action>
	</package>
 
</struts>

Test.java

/**
 * This class is used as an action class.
 * @author w3spoint
 */
public class Test {
	//business logic
	public String execute(){
		return "success";	
	}
}

welcome.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
	<head>
		<title>Struts 2 logger interceptor example</title>
	</head>
	<body>
		<h3>This is a logger interceptor example.</h3>	 
	</body>
</html>

Output:

struts 8 first   Click on Hello button. struts 8 final   Download this example.   Next Topic: Struts 2 execAndWait interceptor with example. Previous Topic: Value Stack in struts 2 with example.

 

Please follow and like us:
Content Protection by DMCA.com