Struts2 IllegalArgumentException异常处理?

时间:2014-01-31 04:38:41

标签: java jsp exception-handling struts2 illegalargumentexception

我有一个无状态会话Bean:

public CmcMapServerInfo getMapServerDetails() throws IllegalArgumentException, PersistenceException{

        Query query;
        List<CmcMapServerInfo> resultList;
        CmcMapServerInfo mapServer = null;

        try{
            query = em.createNamedQuery("CmcMapServerInfo.getMapServerr");
            resultList = query.getResultList();
            mapServer =  resultList.get(0); // as only single row is there
        }catch(IllegalArgumentException e){
            System.out.println(" ILLEGAL CATCHED------------------------------------------------------------");
            throw e;
        }catch(PersistenceException e){
            System.out.println(" PERSISTENCE CATCHED------------------------------------------------------------");
            throw e;
        }
        return mapServer;
    }

此处CmcMapServerInfo.getMapServerr是非法参数,因此会在此处捕获IllegalArgumentException并将其重新发送到调用此函数的Action class

行动类:

public String getMapServerDetails(){

        try{
            mapServer = getSlsConfigureMapServerRemote().getMapServerDetails();
        }catch(NamingException e){
            System.out.println("NAMING EXCEPTION INSIDE :---------------");
            addActionError("There was an Error displaying the Required Page. Please Contact the system Adminstrator");
            return ERROR;
        }catch(IllegalArgumentException e){
            System.out.println("ILLEGAl ARG INSIDE :---------------");
            addActionError("There was an Error getting the Map Server Details. Please Contact the system Adminstrator");
            return ERROR;
        }catch(PersistenceException e){
            System.out.println("PERSISTENCE INSIDE :---------------");
            addActionError("There was an Error getting the Map Server Details. Please Contact the system Adminstrator");
            return ERROR;
        }
        return SUCCESS;
    }

问题在于Action class内部没有捕获bean的重新抛出异常,因此struts2异常Interceptor处理它。

我已在 struts.xml 中配置了struts2异常处理:

<package name="cdot.oss.cmsat.conf.mapserver.struts" namespace="/"
        extends="struts-default">

        <interceptors>
            <interceptor-stack name="appDefaultStack">
                <interceptor-ref name="defaultStack">
                    <param name="exception.logEnabled">true</param>
                    <param name="exception.logLevel">ERROR</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="appDefaultStack" />

        <global-results>
            <result name="exception">mapServer/pages/Exception.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception"
                result="exception" />
        </global-exception-mappings>


        <action name="getMapServerDetails"
            class="cdot.oss.cmsat.conf.struts.ConfigureMapServerAction" method="getMapServerDetails">

            <result name="success">mapServer/pages/ConfigureMapServerInput.jsp
            </result>
            <result name="input">mapServer/pages/ConfigureMapServerInput.jsp
            </result>
            <result name="error">mapServer/pages/Error.jsp</result>


        </action>

由于异常未在Action类中捕获,global exception mapping的{​​{1}}会处理它并显示Exception

为什么Exception.jsp中没有捕获异常,即使它被Bean捕获并在那里重新抛出?

0 个答案:

没有答案