例如action里面爆出个nullpointerException 异常或者别的异常,404 或者 500 什么的。 我就想获得这个异常的信息 ,并且显示到指定的JSP页面上。请问我应该如何获得异常信息,jsp页面怎么获得异常并显示出来呢

解决方案 »

  1.   

    可以在web.xml中配置:
    <error-page> 
            <error-code>500</error-code> 
            <location>/error.jsp</location> 
    </error-page>然后在error.jsp中输出详细异常信息:
    <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true"%> 
    An exception was thrown: <b> <%=exception.getClass()%>:<%=exception.getMessage()%>
      

  2.   

    可以在action里面catch到你要的一些错误信息,然后直接把这个异常信息带到你要显示的jsp页面上
      

  3.   

    可以将 Exception放入到一个 静态对象中(如Map,可以只放Excepion类型),界面中看能否取到,然后显示自己想要显示的内容。
      

  4.   

    谢谢1楼,是我说脑子没转过来。 3楼说的是正解, 可是我只能获得action层的异常,而且是通过 throw new Exception(e);这个方式获得的异常。
    这个获取异常 An exception was thrown: <b> <s:property value="exception.message"/>
    但是server层和dao层都或许不到异常,求解释 
      

  5.   

    <!-- 定义全局Result -->
    <global-results>
    <!-- 当发生异常时,转入错误页面 -->
    <result name="all">/error3.jsp</result>

    </global-results>

    <global-exception-mappings>
    <exception-mapping result="all" exception="java.lang.Exception">
    </exception-mapping>
    </global-exception-mappings>这是Strust2.xml配置信息
      

  6.   

    好了,捕获了。 Caused by: org.postgresql.util.PSQLException: 错误: 在字段 "hj_date" 中空值违反了非空约束  
    但我想获取:后面的的信息 ,用什么获得?