小编典典

Struts2异常处理用法?

jsp

我在Action下面的类中getTspNameIdMap抛出ReqMgmtException异常(自定义异常)。

public String findTspNameIdMap(){

        SlsReqMgmtCommonRemote slsReqMgmtCommonRemote = null;
        tspNameIdMap = new HashMap<String, String>();

        try{
            slsReqMgmtCommonRemote = getSlsReqMgmtCommonRemote();
            tspNameIdMap = slsReqMgmtCommonRemote.getTspNameIdMap(gmaThresholdParameters.getId().getCircleId());

        }
        catch(ReqMgmtException rEx){
            addActionError(rEx.getError());
            result = "error";
            return ERROR;
        }
        catch (Exception e){    
            addActionError("Error in processing your request. Contact Administrator");
            e.printStackTrace();
            System.out.println("[ConfigureTspThresholdAction: findTspNameIdMap Function]:In catch Inside Constructor!!");
            result = "error";
            return ERROR;
        }
        return SUCCESS;
    }

我知道Struts2中也有异常处理,但是目前我没有使用它。我应该使用Struts2异常处理吗?它的用途是什么?


阅读 328

收藏
2020-06-08

共1个答案

小编典典

您应该在Struts2中使用异常处理机制,这就是exception拦截器所提供的。同样,您应该像问题一样处理action方法中的异常。如果它可以很好地处理所有异常,则异常处理程序可以处理它。同样,在一些没有throws Exception签名的方法中,您只能捕获异常,而不能返回ERROR结果。因此,重新抛出异常并由拦截器进行处理是解决方法。

2020-06-08