迁移到 Struts2 的错误处理

Migration error handling to Struts2

要显示错误,我们需要包含错误消息的消息键。 我创建了一个名为 ApplicationError.properties 的新资源包,其中包含此 属性:

app.error=Error: {0}

我尝试将此 class 从 struts1 更改为 struts2,但我遇到错误 handling.I 未在 [=22= 中找到等效项的问题]

   public class MyAction extends Action{

    public ActionForward execute(ActionMapping actionMapping,
                    ActionForm actionForm,
                    HttpServletRequest request,
                    HttpServletResponse response)
                 throws Exception{

    ActionErrors actionErrors = new ActionErrors();
    ActionMessage message = new ActionMessage("app.error","Not Found Exception");


    try{
    //some code

    }catch(NotFoundException e){
        actionErrors.add(ActionErrors.GLOBAL_MESSAGE, message);
        saveErrors(request, actionErrors);
        return getErrorActionForward(actionMapping);

    }
    return getActionForward(actionMapping);
}

你能帮我解决这个问题吗?

检查 ActionSupport class 您的操作 class 以延长。然后使用它的方法来添加错误。

此 class 还实现了 TextProvider,因此您可以从资源中获取本地化消息。

/** Add an Action-level error message to this Action. */
void  addActionError(String anErrorMessage)

/** Add an Action-level message to this Action. */
void  addActionMessage(String aMessage)