Java自学笔记之struts2异常处理
来源:互联网 发布:hp m1136驱动下载 mac 编辑:程序博客网 时间:2024/06/05 11:52
1、struts2中异常处理不是在action类中try…catch,而是把异常抛出去,让struts框架自己处理,程序员需要做的是,当action类中出现异常需要在struts.xml配置文件中进行配置,出现这个异常跳转到哪个物理视图。
2、以前写获取异常是这么写的:
public String update() { Article article = new Article(); article.setContent(content); article.setTitle(title); article.setId(id); try { articleService.update(article); return SUCCESS; } catch (SQLException e) { e.printStackTrace(); return ERROR; } catch (InvalidInputException e) { e.printStackTrace(); System.out.println("输入非法"); return ERROR; }}
3、struts里这么写,只需要把Exception抛出去
public String regist() throws Exception { System.out.println(username); System.out.println(password); System.out.println("你已经成功注册了"); if (username.equals("1")) { throw new SQLException(); } return SUCCESS; }
捕获异常的任务则交给struts.xml配置文件
<package name="lee" extends="struts-default"> <global-results> <result name="sql">/WEB-INF/content/exception.jsp</result> <result name="root">/WEB-INF/content/exception.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result="sql" exception="java.sql.SQLException"></exception-mapping> <exception-mapping result="root" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings> <action name="*" class="com.action.LoginAction" method="{1}"> <exception-mapping result="root" exception="com.exception.MyException"></exception-mapping> <result name="success">/WEB-INF/content/success.jsp</result> <result name="error">/WEB-INF/content/error.jsp</result> </action> </package>
注意:在编写xml文件的时候可能会报错,
这个错误是顺序错误,只需要把struts.xml按照提示的顺序写就OK
这种处理方式是不是比较简单明了。
阅读全文
0 0
- Java自学笔记之struts2异常处理
- java自学笔记之异常处理
- 16.如何自学Struts2之Struts2异常处理[视频]
- Python自学笔记之四、异常处理
- Java自学总结之六异常处理
- Java自学笔记之struts2入门
- Java自学笔记之struts2 文件上传
- Struts2.x学习笔记之 异常处理
- java自学笔记之多态 内部类 异常处理
- 自学Java系列 笔记2 异常处理1
- 自学primer之异常处理
- 自学java总结----异常处理
- struts2自学笔记之二
- Struts2之异常处理
- Struts2之异常处理
- Struts2之异常处理
- Struts2之异常处理
- Struts2之异常处理
- 周末总结--2017.11.12
- 习题6.1
- Open CV 学习开发笔记之ROI(Region Of Interest)感兴趣区域(三)
- 首次发布
- Leetcode题解-3Sum&3Sum Closest
- Java自学笔记之struts2异常处理
- JAVA冒泡
- 积极加班,负责任的工作
- ASCII,Unicode,UTF-8
- SpringBoot—入门
- PriorityBlockingQueue 简单实用例子
- 多线程
- JAVA并行
- 6.7