简体   繁体   English

Struts2的准备方法无法处理

[英]Struts2 prepare method can not be handled

When I use implements Preparable and override prepare() method and if I get any problem in preparation I set an action error with com.opensymphony.xwork2.ActionSupport addActionError . 当我使用implements Preparable和重写prepare()方法时,如果在准备过程中遇到任何问题,我会使用com.opensymphony.xwork2.ActionSupport addActionError设置操作错误。

After prepare method sets the error message I want to process this message in the real initialize method. 在prepare方法设置了错误消息之后,我想在实际的initialize方法中处理此消息。

public class TestClass implements Preparable {

    public void prepare() {
        // ...
        if (error) {
            addActionError("error");
        }
    }

    public String initializeAndDo() {
        String target = ERROR;
        // ...
        return target;
    }

}

When I debug it, it never reaches initiliazeAndDo method and returns INPUT automatically. 当我调试它时,它永远不会到达initiliazeAndDo方法并自动返回INPUT。

struts.xml : no intercepter in action block. struts.xml:操作块中没有拦截器。

 <action name="action_name" method="initializAndDo" class="TestClass">
    <result name="input">/pages/input.jsp</result>
    <result name="error">/pages/error.jsp</result>
 </action>

Is there any idea? 有什么主意吗

Sure; 当然; this is normal behavior. 这是正常现象。

If there are errors, the "workflow" interceptor will see that, and forward to the "input" result. 如果有错误, "workflow"拦截器将看到该错误,并转发到"input"结果。

If you don't want to have your (non-standard) workflow interrupted, don't set an error in the prepare() method. 如果您不想中断(非标准)工作流程,请不要在prepare()方法中设置错误。 One alternative would be to set a flag in prepare() that could be checked in initializeAndDo method--this would keep "workflow" from short-circuiting the requst. 一种替代方法是在prepare()中设置一个可以在initializeAndDo方法中检查的标志-这样可以防止"workflow"使请求短路。

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

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