开发者

Struts2 : Interceptor is being only one time instead of two times

开发者 https://www.devze.com 2023-04-04 22:02 出处:网络
I am working on Struts2 Interceptors . I have read that Struts2 Interceptors are just like Filters , which execute before the Action class is executed and one more time after processing the result ( P

I am working on Struts2 Interceptors . I have read that Struts2 Interceptors are just like Filters , which execute before the Action class is executed and one more time after processing the result ( Please correct me if i am wrong ) , that is two times

But when i ran the below code , the interceptors are executed only once . Please correct me if i made any mistake . Please see my code below :

This is My Struts.xml file

<struts>
<constant name="struts.devMode" value="true" />
    <package name="test" extends="struts-default">
<interceptors>
 <interceptor name="loginkiran" class="vaannila.MyLoginInterCeptor" />
</interceptors>
        <action name="HelloWorld" class="vaannila.HelloWorld" method="kiran">
            <interceptor-ref name="loginkiran" />
            <result name="SUCCESS">/success.jsp</result>
        </action>
    </package>
</struts>

This is my Action class

public class HelloWorld
{
    public HelloWorld() {
    }
    public String kiran() {
        System.out.println("iNSIDE THE aCTION CLASS");
        return "SUCCESS";
    }
}

This is my Interceptor class

public class MyLoginInterCeptor implements Interceptor {

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        System.out.println("Destroying Interceptor");

    }

    @Override
    public void init() {

    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {

        HttpS开发者_Python百科ervletRequest request = (HttpServletRequest) ActionContext
                .getContext().get(ServletActionContext.HTTP_REQUEST);

        System.out.println("iNSIDE THE iNTERCEPTOR");

        return invocation.invoke();

    }

}

This is my JSP File :

<html>
<body>
<%
System.out.println("iNSIde THE jsp");
%>
</body>
</html>

The Output for the above code is this :

iNSIDE THE iNTERCEPTOR
iNSIDE THE aCTION CLASS
iNSIde THE jsp


Interceptors are not executed twice (nor are filters): interceptors (and filters) wrap the action (or servlet/etc.)

public String intercept(ActionInvocation invocation) throws Exception {
    System.out.println("Before action invocation...");
    return invocation.invoke();
    System.out.println("After action invocation...");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号