简体   繁体   English

Struts2从另一个动作改变动作

[英]Struts2 change action from another action

I was wondering if it could be possible to change some fields for a struts2 action from another action. 我想知道是否有可能从另一个动作中更改一个struts2动作的某些字段。 For example, I have Class1 Action with field string1, and I want to change it from another action, Class2. 例如,我有Class1 Action with field string1,我想从另一个动作Class2更改它。

public class Class1 extends ActionSupport{

    private String string1="old String";

}

public class Class2 extends ActionSupport{

    public String execute(){
        Class1 class1=new Class1();
        class1.setString1("new String");
    }

}

In struts1, one could take all ActionForms from session. 在struts1中,可以从会话中获取所有ActionForms。 In struts2 they are no longer available on session, from the moment that there are no more action forms. 在struts2中,从没有更多操作表单的那一刻起,它们就不再在会话中可用。 Thanks! 谢谢!

Struts2 action instances are created when the request matches action mapping for the action. 当请求与操作的操作映射匹配时,将创建Struts2操作实例。 New instance is created each time. 每次都会创建新实例。

They live until the request processing ends and after that are inaccessible. 它们一直存在,直到请求处理结束,之后无法访问。

Hence you'd have to somehow map a request to two actions at the same time, which is AFAIK impossible or do some other trickery which would go against Struts2 model. 因此,你必须以某种方式将请求同时映射到两个动作,这是AFAIK不可能的,或者做一些与Struts2模型相反的诡计。

If you need to pass information from one action to another, you have some options, ie: 如果您需要将信息从一个动作传递到另一个动作,则有一些选择,即:

  • put it in the session 把它放在会议中
  • put it in the database 把它放在数据库中
  • send it back to client and resend to server with next request. 将其发送回客户端并使用下一个请求重新发送到服务器。

The ScopedModelDriven interceptor and interface allows a reasonable facsimile of session-scoped action forms (more or less identical at the model level). ScopedModelDriven拦截器和接口允许合理地传真会话范围的动作形式(在模型级别或多或少相同)。

It really depends on your usecase, though--if you're trying to implement session-scoped action forms, this is the way to do it. 这实际上取决于你的用例 - 如果你正在尝试实现会话范围的动作表单,这就是这样做的方法。

If your goal is more of a "set a value on an arbitrary action", ScopedModelDriven might not be the best approach. 如果您的目标更多是“为任意操作设置值”, ScopedModelDriven可能不是最好的方法。 For example, I needed to set values on arbitrary actions (not a "form wizard" type thing) and used an interceptor and annotations so I could pull data from several sources and set it on various non-contiguous actions. 例如,我需要在任意操作(不是“表单向导”类型的东西)上设置值,并使用拦截器和注释,这样我就可以从多个源中提取数据并将其设置为各种非连续的操作。

In essence it was the same technique, but included other non-web scopes and allowed a bit more leeway in terms of what was being set when, and on what. 本质上它是相同的技术,但包括其他非Web范围,并允许在设置什么时,以及什么方面更多的余地。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM