有没有一种方法来重定向到另一个动作类,而无需使用上的struts.xml [英] Is there a way to redirect to another action class without using on struts.xml

查看:137
本文介绍了有没有一种方法来重定向到另一个动作类,而无需使用上的struts.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Struts应用程序中创建许多类。我没有检查是否已登录状态中的任何类。相反,我已经延长一个基本操作类。

I have many classes created in my Struts application. I did not check whether logged in condition in any of the classes. Instead I have extended a base action class.

现在我想在我的基地行动创造了pre-处理程序检查登录并重定向如果他们没有登录。我想是这样的。

Now I want to create a pre-handler in my base action to check logged in and redirect if they are not logged in. I want something like this.

public BaseAction(){
  if(isLoggedIn){
    //go to child which was called
   }
   else {   //redirect to login page
  }

   }

另一种方法是通过调用所有动作类此方法,并定义像

Another way is to check isLoggedIn() by calling this method in all action classes and defining a global result like

<result name="not-logged-in" type="redirectAction">Login.action</result>

请帮我找到这个更好的办法。

Please help me to find better way for this.

推荐答案

看起来像要在基本操作类的构造函数来检查,但你错了。构造函数使用的对象工厂实例的操作实例。在这个阶段的几件事情是提供给您。你的情况,这是错的。另一种方法是,如果你移动逻辑进入方法说的execute()并调用 super.execute()之前的任何方法调用的工作,但如果你忘了把超级呼叫的行动,那么你可能最终动作code运行未经过身份验证。以prevent它执行任何动作之前,您应该运行code,并能够访问操作实例或行动方面更Struts2的。我猜你从来没有读过这本书的Struts 2在行动,所以我会给你一些我自己的想法。这是关于建立 AuthenticationInterceptor 和实现 UserAware :注射用户登录到实现这个接口的动作的动作。该拦截器看起来像

Looks like you want to check in the constructor of the base action class, but you are mistaken. The constructor is used by the object factory to instantiate your action instance. In this stage a few things is available to you. In your case it's wrong. Another approach is if you move the logic into the method say execute() and call super.execute() before any method call would work, but if you forget to put the super call in the action then you may end up the action code running not authenticated. To prevent it you should run the code before any action is executed and be able to access the action instance or action context to be more Struts2. I guess you've never read the book "Struts 2 in Action" so I will give you some my own thoughts. It's about creating AuthenticationInterceptor and the action that implements UserAware that injects the user logged in into the action that implement this interface. The interceptor is looks like

public class AuthenticationInterceptor implements Interceptor {

public void destroy() {
}

public void init() {
}

public String intercept(ActionInvocation actionInvocation) throws Exception {
    Map session = actionInvocation.getInvocationContext().getSession();
    User user = (User) session.get(Struts2MyConstants.USER);

    if (user == null) {
        return Action.LOGIN; //login required result
    }               
    else {              
        Action action = (Action)actionInvocation.getAction();

        if (action instanceof UserAware) {
            User freshUser = myService.getUser(user.getId());
            ((UserAware)action).setUser(freshUser);
        }

        System.out.println("Logged in: interceptor");
        return actionInvocation.invoke();
    }
}

UserAware 的样子

public interface UserAware {

    public void setUser( User user );

}

和做出会引用任何行动安全的默认堆栈

and make a secure default stack that will reference any action

<interceptors>
  <interceptor name="authenticationInterceptor" class="org.yourapp.struts.interceptor.AuthenticationInterceptor"/>
  <interceptor-stack name="secureStack">
    <interceptor-ref name="authenticationInterceptor"/>
    <interceptor-ref name="defaultStack"/>
  </interceptor-stack>
</interceptors>
<default-interceptor-ref name="secureStack"/> 

如果你让你的基地采取行动落实 UserAware ,然后将其记录在将不仅可从会话,但在行动太多,如果你的定义吸气的用户对象用户或使其受到保护。你必须让用户对象不变,这样不会危及安全功能。

If you make your base action to implement UserAware then the user object that is logged in will be available not only from session but in action too if you define getter for the user or make it protected. You have to make the User object immutable so that not compromise the security feature.

这篇关于有没有一种方法来重定向到另一个动作类,而无需使用上的struts.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆