简体   繁体   English

我如何从另一个人那里获得行动课的价值

[英]How can I get a value of an action class from another

I have two action classes 我有两个动作课

public class TokenAction extends Action {
    private ActionForward getToken(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        String token = generateToken();
        response.setContentType("text/plain");
        response.getWriter().print(token);

        return null;
    }

and

public class ActionTwo extends Action{
    private ActionForward doSomething(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response)  {
        token = ???
        return actionMapping.findForward("page");
    }

}

How can I get in ActionTwo from TokenAction? 如何从TokenAction进入ActionTwo?

Basically, in Java there are multiple ways to pass objects across contexts, the most popular being likely: 基本上,在Java中,有多种方法可以跨上下文传递对象,最流行的可能是:

  • through static variables (not recommended) 通过静态变量(不推荐)
  • through shared parameter maps 通过共享参数图
  • through a controller object 通过控制器对象

In your case, since the parameters passed to the methods seem to have a lifetime that encompasses both calls (although it's hard to say without the specifics), you should probably store them in the parameters ( HttpServletResponse isn't very useful for that purpose, but the ActionMapping and ActionForm likely are). 在您的情况下,由于传递给方法的参数似乎有一个包含两个调用的生命周期(尽管很难说没有细节),因此您可能应该将它们存储在参数中( HttpServletResponse对于此目的不是很有用,但ActionMappingActionForm可能是)。 If your Action s are created in the context of some controller, you could use the controller to pass the parameter instead. 如果您的Action是在某个控制器的上下文中创建的,则可以使用控制器来传递参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从另一个班级获得价值 - How can i get a value to class from another class 如何将我从 class 获得的值传递给另一个 class? - How can I pass the value I get from a class to another class? 我如何在不扩展的情况下将价值从一门课转移到另一门课? - How can I get value from one class to another without extends it? 我如何从Java集获取返回值 <String> 在java的另一个类中 - how i can get the return value from java set<String> in another class in java 如何获取从一个类到另一个Action Listener类的变量信息? - How do I get information of the variables from one class to another Action Listener class? 如何通过从另一个类中执行操作来更改一个类中的Graphics的颜色? - How can I change the color of my Graphics in one class by preforming an action from another class? 如何使用JButton从另一个类打开JFrame。 目前,我使用链接到动作监听器的按钮 - how can i open a JFrame from another class with a JButton . For the moment i use a button linked to action listener 我怎样才能得到从另一个类返回的整数? - How can I get the ints returned from another class? 如何从Java中的另一个类获取变量? - How can I get a variable from another class in Java? 如何获取一个类的值并将其传递给另一个类? - How to get and pass a value from a class to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM