使用@InterceptorRef的 Struts2 自定义拦截器示例

使用@InterceptorRef的 Struts2 自定义拦截器示例

原文: https://howtodoinjava.com/struts2/struts-2-custom-interceptor-with-interceptorref-example/

在以前的帖子中,我们了解了 helloworld 应用设置 Struts2 应用的结果路径。 现在,在这篇文章中,我将举一个使用注解的自定义或用户定义的拦截器配置示例。

拦截器是一个类,其每次访问已配置的服务器资源时都会调用其预定义方法。 这可以在资源访问之前或之后访问。 请注意,此处的资源访问也意味着 HTTP 请求处理。

Steps summary:
1) Create custom interceptor class which implements com.opensymphony.xwork2.interceptor.Interceptor
2) Implement overridden methods
3) Give definition in struts.xml file
4) Apply on any Action class

重要:请注意,Struts2 随附了许多现成的拦截器实现,因此请确保您确实需要创建自己的拦截器,或者有什么适合您的。

1)& 2)创建自定义拦截器类并实现覆盖的方法

以下是自定义拦截器的示例代码,以及所有替代方法的实现。 请随时以适当的方法添加应用逻辑。

package com.howtodoinjava.struts2.example.web;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class DemoCustomInterceptor implements Interceptor
{
	private static final long serialVersionUID = 1L;

	@Override
	public void destroy() {
		System.out.println("DemoCustomInterceptor destroy() is called...");
	}

	@Override
	public void init() {
		System.out.println("DemoCustomInterceptor init() is called...");
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception 
	{
		System.out.println("DemoCustomInterceptor intercept() is called...");
		System.out.println(invocation.getAction().getClass().getName());
		return invocation.invoke();
	}
}

3)在struts.xml文件中给出定义

这是非常重要的一步,因为在这里您可以在 struts 上下文和运行时中注册拦截器类。

<?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>
	<package name="default" namespace="/" extends="struts-default">
		<interceptors>	
			<interceptor name="demoCustomInterceptor" class="com.howtodoinjava.struts2.example.web.DemoCustomInterceptor" />
			<interceptor-stack name="customStack">
	     		<interceptor-ref name="demoCustomInterceptor"/>
				<interceptor-ref name="defaultStack" />
        	</interceptor-stack>
	    </interceptors>	    
	</package>

	<constant name="struts.convention.result.path" value="/WEB-INF/jsp/" />
	<constant name="struts.devMode" value="true" />
	<constant name="struts.action.extension" value="action," />
	<constant name="struts.custom.i18n.resources" value="test" />
	<constant name="struts.convention.default.parent.package" value="default"/>

</struts>

4)应用任何动作类

我在一个这样的TestAction类中应用了上面的拦截器。

package com.howtodoinjava.struts2.example.web;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

@InterceptorRef(value="customStack")
@Results({
	   @Result(name="success", location="success.jsp"),
	   @Result(name="input", location="index.jsp")
})
public class TestAction extends ActionSupport 
{
	private static final long serialVersionUID = 1L;

	private String name;
    private Date nowDate;

    @Override
    public void validate(){
        if (name==null || name.length()==0)
            addActionError(getText("error.enter.message"));
    }
    @Actions({
        @Action("/"),
        @Action("/test")
    })
    @Override
    public String execute() throws Exception {
        nowDate = new Date();
        return ActionSupport.SUCCESS;
    }
    public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getNowDate() {
        return nowDate;
    }
}

请注意,我已经使用@InterceptorRef(value="customStack")定义应用了拦截器。 它可以帮助您一次性应用多个拦截器。 如果仅只需要特定的拦截器,请使用其名称,如下所示:@InterceptorRef(value="demoCustomInterceptor")

测试应用

当我运行上面的应用时。 我观察到服务器在控制台中的日志如下:

INFO: Loading global messages from [test]

DemoCustomInterceptor init() is called...

//some logs
INFO: Server startup in 1146 ms

DemoCustomInterceptor intercept() is called...
com.howtodoinjava.struts2.example.web.TestAction

请点击下面的链接下载该项目的源代码。

祝您学习愉快!