迁移到Struts2的错误处理

迁移到Struts2的错误处理

本文介绍了迁移到Struts2的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

To display errors we need message keys which contains the error messages.I create a new resource bundle named ApplicationError.properties which contain this property:

app.error=Error: {0}

我尝试将此类从struts1更改为struts2,但是我在错误处理方面遇到了问题.我在struts2中找不到等效项

I try to change this class from struts1 to struts2, but I have a problem with error handling.I have not found the equivalent in struts2

   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 类.然后使用其方法添加错误.

Checkout ActionSupport class that your action class to extend. Then use its methods to add errors.

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

This class also implements a TextProvider, so you can get localized messages from the resources.

/** 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)

这篇关于迁移到Struts2的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:52