Migration error handling to Struts2

可紊 提交于 2021-02-04 21:09:25

问题


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}

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);
}

Can you help me with this?


回答1:


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

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)


来源:https://stackoverflow.com/questions/42114569/migration-error-handling-to-struts2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!