有没有办法在不使用 struts.xml 的情况下重定向到另一个 Action 类 [英] Is there a way to redirect to another Action class without using on struts.xml

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

问题描述

我在 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.

现在我想在我的基本操作中创建一个预处理程序来检查登录情况并在他们未登录时重定向.

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

我想要这样的东西:

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

另一种方法是检查isLoggedIn(),通过在所有动作类中调用此方法并定义一个全局结果如

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(),但是如果您忘记将超级调用操作,那么您可能最终会运行未经过身份验证的操作代码.为了防止它,您应该在执行任何操作之前运行代码,并且能够访问更多 Struts2 的操作实例或操作上下文.我猜你从来没有读过Struts 2 in行动,所以我会给你一些我自己的想法.这是关于创建 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,那么如果您为用户定义 getter 或使其受到保护,登录的用户对象不仅可以从会话中使用,而且可以在操作中使用.您必须使 User 对象不可变,以免损害安全功能.

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 的情况下重定向到另一个 Action 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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