在Struts中使用异常
1 自定义用户异常
public class MyException extends Exception{
private String code;
public MyException(){
super();
}
public MyException(String code){
this.code=code;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
2 在相应的业务方法中抛出异常
public boolean isExistMember(String memberCode)throws MyException{
List list=(List)baseDAO.getByProperty("TMember","memberCode",memberCode);
if(list.size()>0){
throw new MyException("Error_001");
}else{
return false;
}
}
3 在Action中作出相应的页面跳转
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AddMemberForm addMemberForm = (AddMemberForm) form;// TODO Auto-generated method stub
try{
if(!memberManager.isExistMember(addMemberForm.getMemberCode())){
memberManager.addMember(addMemberForm);
}
}catch(MyException ee){
request.setAttribute("code",ee.getCode());
return mapping.findForward("error");
}catch(Exception e){
e.printStackTrace();
}
return mapping.findForward("success");
}
*其中"error","success"要先在对应的Action中配置好,内容如下
name="addMemberForm"
path="/addMember"
scope="request">
4 首先定义好Struts中常用的标签文件,把这些标签统一放到一个jsp文件中方便在其它页面使用标签时可以直接引用
taglibs.jsp内容如下:<!---->
5 再创建一个引用properties文件的jsp文件
i18n.jsp内容如下:
<fmt:setbundle basename="com.demo.resource.member" var="addMember"></fmt:setbundle>
其中member.properties文件内容如下:
####==============Error====================
Error_001=此会员已存在!
####==============Success====================
Succ_001=增加成功!
6 在jsp页面使用时:
如在error.jsp内容如下:
<!---->
<!---->
<!---->
<!---->
<html:html lang="true">
<html:base>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
error.jsp
<fmt:message bundle="${addMember}"><!----></fmt:message>
</html:html>
<html:html lang="true">
<fmt:message bundle="${addMember}"><!----></fmt:message>
</html:html>
分享到:
- 2007-08-28 14:00
- 浏览 1343
- 评论(1)
- 论坛回复 / 浏览 (1 / 1845)
- 查看更多
相关推荐
Struts2异常处理机制
项目中出现的异常通常要用一个友好的异常页面来显示,通过对struts2.xml的配置能拦截全局异常,只要出现异常就会转向异常页面。
NULL 博文链接:https://yuxisanren.iteye.com/blog/1707508
配合博文:http://blog.csdn.net/u011638419/article/details/41343683
NULL 博文链接:https://wlh269.iteye.com/blog/321746
在我们做struts2文件下载的时候,经常会遇到这种问题:点“打开/保存”一切正常,但当我们点击“取消”时,却报一堆的异常,非常让人头疼,现在总结了一些问题的解决方案,有实例。
struts2 的异常收集
struts2的异常处理,解决异步请求时,异常以json格式发送到客户端。以及dao单元测试的配置。
NULL 博文链接:https://wjt276.iteye.com/blog/690323
struts常见异常及处理
在struts2中使用result里type="stream"的结果类型时,可以实现文件的下载管理,使用时也是比较顺畅,但是当在“下载提示窗口”中点击“取消按钮”时,总是报出“java.lang.IllegalStateException”异常,异常内容...
struts2一些异常和如何搭struts2项目 非常详细我在开发中总结的 ,也可能有点乱,
本文档主要介绍了struts2的异常处理机制,希望对你有所帮助。
异常错误概要 异常错误 说明 StrutsException A generic runtime exception that optionally contains Location information 概览 程序包 类 使用 树 已过时 索引 帮助 上一个程序包 下一个程序包 框架...
Struts2调用EJB异常项目包,出现的问题以及解决方案参见EJBStruts.zip中的说明.txt 以及博客 http://blog.csdn.net/icecream0/article/details/7167853
Struts2 异常处理的四种获取属性方法,大家可以借鉴学习
在我们做struts2文件下载的时候,经常会遇到这种问题:点“打开/保存”一切正常,但当我们点击“取消”时,却报一堆的异常(其中包括...附件中使用struts2-sunspoter-stream-1.0.jar插件完美的解决了此类问题。
Struts2的异常处理实例 博文链接:https://zmx.iteye.com/blog/463996
Struts简介 什么是Struts Struts基本运作流程 ActionMapping类 Action类 ActionForm类 ActionError与ActionMessage 协同开发 模块化程序 Struts异常处理 Struts国际化支持 PlugIn接口 等等
从网上搜索的一段关于struts1.x自定义异常处理的方法。