简体   繁体   中英

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 .

After prepare method sets the error message I want to process this message in the real initialize method.

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.

struts.xml : no intercepter in action block.

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

If you don't want to have your (non-standard) workflow interrupted, don't set an error in the prepare() method. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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