in Struts 2 interceptors are procedures that run
before or after an action is invoked, there are some
defined we can use directly or create new and
implement these two schemes that have served us
to better understand the famous interceptor of Struts2.
But who better than an example to better understand this concept
.
we will develop a program that spends a
word of a page to another and the time of the change request page
of us display a message that runs automatically
by an interceptor. 1 .- Primiero
we create our home page, as always
page
index.jsp \u0026lt;% @ page contentType = "text / html"%>
\u0026lt;% @ page pageEncoding = "UTF -8 "%>
\u0026lt;% @ taglib uri =" / struts-tags "prefix =" s "%>
\u0026lt;! DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.01 Transitional / / EN"
"http://www.w3.org/TR/html4/loose.dtd">
\u0026lt;html>
\u0026lt;head> ;
\u0026lt;meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
\u0026lt;title> Struts2 \u0026lt;/ title>
\u0026lt;/ head>
\u0026lt;body>
\u0026lt;h1> pass the following word \u0026lt;/ h1>
\u0026lt;s:form action="proceso">
name="palabra" \u0026lt;s:textfield label="palabra"/>
\u0026lt;s:submit/>
\u0026lt;/ s: form>
\u0026lt;/ body>
\u0026lt;/ html> ;
2 .- create the following classes import
com.opensymphony.xwork2.ActionSupport;
public class Parameter extends
ActionSupport {private String word;
/ / @ Override public String execute
()
{System.out.println ("entered axecute");
return SUCCESS;}
getPalabra public String ()
{return word;
} public void
setPalabra (String word) {
this.palabra = word;
}} --------------------
-----------------------
this class is the interceptor, as
key things we can say that implements Interceptor
interface and method invoke () is which defines the processes
us before or after the action.
com.opensymphony.xwork2.ActionInvocation import, import
com.opensymphony.xwork2.interceptor.Interceptor;
PruebaInterceptor
public class implements Interceptor {public String intercept
(ActionInvocation next) throws Exception {
Next.invoke String s = ();
System.out.println ("intercept");
return s;}
public void init () {}
public void destroy () {}
}
if we put the System.out.println ("intercept"); before
next.invoke String s = (); will show the message before
System.out.println ("entered axecute") found in parameter class
3 .- create the page response, which is associated to the action
parameter.
\u0026lt;% @ page contentType = "text / html"%>
\u0026lt;% @ page pageEncoding = "UTF-8"%>
\u0026lt;% @ taglib uri = "/ struts-tags" prefix = "s"%>
\u0026lt;html>
\u0026lt;body>
\u0026lt;s:property value="palabra"/>
parameter
\u0026lt;/ body>
\u0026lt;/ html>
4 .- now we would be the most important thing strust.xml
our archive, which has our
configuration.
\u0026lt;!
DOCTYPE struts PUBLIC "- / / Apache Software Foundation / / DTD Struts Configuration 2.0 / / EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="tiempo_servidor" class="PruebaInterceptor"/>
</interceptors>
<action name="proceso" class="parametro">
<interceptor-ref name="tiempo_servidor"/>
<interceptor-ref name="defaultStack" /> \u0026lt;result
name="success"> / resultado.jsp \u0026lt;/ result>
\u0026lt;/ action>
\u0026lt;/ package>
\u0026lt;/ struts>
of this code we can say that it is important this
interceptor-ref name = "defaultStack" in our action, since
that if the parameter is not word does not come to our website now
respuesta.jsp
when running our code, we
appreciate that at a glance we can see that
the parameter is passed from one page to another, but if we
our console can see that shows what next.
axecute entered
intercept try to make good an example as simple to understand and start
knowledge of this framework, I think
we recommend you read a lot and try to understand how it works
to avoid being stuck in
configuration problems, notably in the file
struts.xml