码迷,mamicode.com
首页 > 其他好文 > 详细

拦截器-注解

时间:2014-05-29 03:49:22      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:des   style   c   class   blog   code   

首先定义我们自己的Interceptor

bubuko.com,布布扣
package com.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.common.utils.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.ValueStack;
import com.web.action.Constants;

@ParentPackage(value="struts-default")
public class LoginInterceptor extends AbstractInterceptor
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static final String USER_SESSION_KEY="wallet.session.user";
    
    public void init()
    {}
    
    @SuppressWarnings("unchecked")
    @Override
    public String intercept(ActionInvocation invocation) throws Exception
    { 
        ActionContext actionContext = invocation.getInvocationContext();
        HttpServletRequest request = (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);

        String uri = request.getRequestURI();
        if(uri.indexOf("index.action")>-1)
        {
            return Constants.INPUT; 
        }
        else if(uri.indexOf("login.action")>-1)
        {
            ValueStack vt = actionContext.getValueStack();
            java.util.Map<String, Object> mp = vt.getContext();
            
            java.util.Map<String, Object> paraMap = (java.util.Map<String,Object>)mp.get(ActionContext.PARAMETERS);
            String[] name = (String[])paraMap.get("user.name");
         
            //String name = (String)vt.findValue("user.name");
            if(name!=null && name.length>0 && !StringUtil.hasContent(name[0]))
            {
                vt.set("fielderror", "必须输入用户名!");
                return Constants.INPUT;
            }
            else return invocation.invoke();
        }
        else
            return invocation.invoke();
    }

    public void destroy()
    {}
}
bubuko.com,布布扣

将其注册到struts中,修改struts2.xml文件增加如下内容

<package name="struts-interceptor" extends="struts-default">
        <interceptors>
            <interceptor name="loginInterceptor" class="com.web.interceptor.LoginInterceptor"/>
        </interceptors>
    </package>
这样我们自己定义的Interceptor已经设置完成,如果想使用则可以用annotation指定。如

bubuko.com,布布扣
import com.opensymphony.xwork2.ActionInvocation; 
import com.opensymphony.xwork2.interceptor.Interceptor;

@InterceptorRefs({ @InterceptorRef(
"loginInterceptor"), @InterceptorRef("defaultStack") }) @ParentPackage(value="struts-interceptor") @Namespace(value="/application") @Result(name="input",location="/application/login.jsp") public class LoginAction extends BaseAction { private static final long serialVersionUID = 1L; }
bubuko.com,布布扣

我们在LoginAction里使用了刚刚自定义的Interceptor。

官网教程提供的解决方案:

引用


If you get errors like "Unable to find interceptor class referenced by ref-name XYZ". This means that the package where Convention is placing your actions, does not extend the package where the interceptor is defined. To fix this problem either 1)Use @ParentPackage annotation(or struts.convention.default.parent.package) passing the name of the package that defines the interceptor, or 2) Create a package in XML that extends the package that defines the interceptor, and use @ParentPackage(or struts.convention.default.parent.package) to point to it.

拦截器-注解,布布扣,bubuko.com

拦截器-注解

标签:des   style   c   class   blog   code   

原文地址:http://www.cnblogs.com/sandea/p/3754062.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!