struts2中处理自定义异常的方法小结
在struts2的架构中,应该充分利用ssh架构的优势,去实现自定义异常,小结如下。
先写自定义异常类:
?
public class ProjectException extends Exception {
??? public ProjectException(){
??? ?super();
??? }
???
??? private String message;
??? public ProjectException(String message){
??? ?this.message = message;
??? }
???
?public String getMessage() {
??return message;
?}
然后在struts.xml中,定义如下:
<global-results>
<result name="error">/error.jsp</result>
</global-results>
??<global-exception-mappings>?
???????????? <exception-mapping result="error" exception="java.lang.Exception"/>?
???????????? <exception-mapping result="error" exception="com.exception.ProjectException"/>?
??????? </global-exception-mappings>
然后error.jsp中
? <s:fielderror></s:fielderror>
?? <s:property value="exception.message"/>