【问题标题】:How to execute an action before loading a page in Struts2?如何在 Struts2 中加载页面之前执行操作?
【发布时间】:2012-02-04 08:33:13
【问题描述】:

我有一个使用 Struts2 作为控制器层的 Java Webapp 项目。在做任何事情(比如打开网页)之前,我需要检查用户的权限。我开发了一个授权系统,需要在 struts 操作中执行。那么,如何在加载页面之前执行此操作?我应该使用拦截器吗?

【问题讨论】:

  • 我不明白你的问题。如果必须在 Struts 动作中执行,则在 Struts 动作中执行。有什么问题?
  • 看,问题是我想在加载特定页面的任何内容之前执行该操作。这可能是这样的场景: 1-用户进入 www.app.com/admin.jsp 2-应该在操作中检查权限 3-如果授权,则加载页面,否则会显示错误。
  • @SJ.Jafari: 如果你想实现一个授权系统,可以选择 spring-security/JAAS 等授权框架。

标签: java struts2 interceptor


【解决方案1】:

您未能理解像 Struts 这样的 MVC 框架的全部意义。在 MVC 应用程序中,您应该从不有任何链接或表单指向 JSP。

您应该始终指向映射到 Struts servlet/filter 的 URL。然后 servlet 根据 URL 调用适当的动作,动作返回结果(要转到的 JSP),Struts 转发到这个 JSP。

如果你直接指向一个 JSP,那么你就完全绕过了 Struts。阅读http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html

【讨论】:

  • 我想你明白了:D,我实际上了解 MVC 框架的全部意义。问题是,除了使用 struts 作为表单的控制器之外,我没有想过将它们用于链接。无论如何,谢谢,我得到了答案。
  • 如果您不使用 struts2-conventions-plugin,我强烈建议您查看它。您将应用一个拦截器,它将强制某个文件夹中的所有 jsp 都使用它。然后你就不需要任何 xml(也许有点定义你的拦截器堆栈)。要点是您只需创建链接而无需担心动作映射,其余的将由约定完成。
【解决方案2】:

当然,您应该实现自己的拦截器并将其放在其他拦截器的顶部(不一定在顶部)。如果你把它放在最上面,它会在你的页面执行之前工作,并且可以改变你的返回值。

例如:

在你的 struts.xml 中

<interceptor name="security"
    class="com.solekia.common.SecurityInterceptor">
</interceptor>

<interceptor-stack name="defaultStack">
    <interceptor-ref name="security"/>
    <interceptor-ref name="exception" />
...
</interceptor-stack>

<global-results>
    <result name="login" type="redirectAction">Login</result>
    <result name="adminlogin" type="redirectAction">AdminLogin</result>
    <result name="error">/error.jsp</result>
</global-results>

你的拦截器:

public class SecurityInterceptor extends AbstractInterceptor
{
    public static final String CURRENT_USER = "current_user";
    public static final String LOGIN = "login";
    public static final String ADMINLOGIN = "adminlogin";

    public String intercept(ActionInvocation invocation) throws Exception {

        Object user = invocation.getInvocationContext().getSession().get(CURRENT_USER); 
        Object action = invocation.getAction();

        if(action instanceof Login)
        {
            return invocation.invoke();
        }
        else if(action instanceof Register)
        {
            return invocation.invoke();
        }
        else if(action instanceof Pro)
        {
            return invocation.invoke();
        }

        if( user == null )
        {
            invocation.getInvocationContext().getSession().clear();
            return LOGIN;
        }
        else
        {
            if(user instanceof User)
            {
                if(action instanceof AdminLogin)
                    return ADMINLOGIN;
            }

            if(user instanceof Admin)
            {
                if(action instanceof ClientPortal)
                    return LOGIN;
            }           

            synchronized(user)
            {           
                return invokeLocal(invocation) ;
            }
        }
    }

    protected String invokeLocal(ActionInvocation invocation) throws Exception
    {
        return invocation.invoke();     
    }   
}

希望对你有帮助

【讨论】:

  • 感谢您的回答,但我在直接指向链接时犯了一个根本性错误。
猜你喜欢
  • 2018-02-07
  • 2010-10-27
  • 1970-01-01
  • 2020-03-01
  • 1970-01-01
  • 2013-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多