- 浏览: 111117 次
- 性别:
- 来自: 上海
文章分类
最新评论
一 配置异常(在struts-config.xml文件中定义),定制异常有两种:
1 全局异常(Global Exceptions)
<global-exceptions>
<exception key="invalideUser"
path="/Login.jsp" type="com.future.struts.MyException" />
</global-exceptions>
2 局部异常(Local Exception)
<action-mappings>
<action attribute="loginForm" name="loginForm"
path="/loginAction(出错转向的路径)" scope="request"
input="/login.jsp"//异常转向的页面,同exception中的path,优先级path高于input
/>
<exception key="invalideUser(异常信息的key)" path="/Error.jsp"
type="cn.itcast.ItcastException(异常类全名)" />
</action-mappings>
path:出现异常后跳转的页面
key:异常信息的键,对应的值在资源文件当中
type:所要处理的异常类
二 在相应的action中的execute方法中抛出异常
三 在异常处理页面(path所指的页面)使用html:errors标签打印异常信息
1 建立资源文件ApplicationResources.properties
内容:invaliduser=it is an invalid user(key/value)
2 配置struts-config.xml文件
<message-resources parameter="cn.itcast.ApplicationResources" key="invalideuser"/>
3 使用html:errors标记打印信息
<html:errors />
--------------------------------------------
1 编程式异常
* 截获异常
* 创建相应的异常消息
* 传递异常消息
* 转向相应的页面处理异常
2 声明式异常(自动处理的异常)
* 在struts-config.xml文件中配置<exeception/>标签
* 理解局部和全局exception
* 注意局部<exception/>标签需要配置到<forward/>标签的前面
<exeception/>标签中的属性说明:
* key:指异常信息对应的国际化消息文本,这个key值需要在国际化资源文件中定义
* type:处理那种异常
* path:定义一但出现异常,需要转向那个页面,如果不定义path,
默认情况下将使用<action>标签中input属性对应的页面
* scope:可以取值request和session,默认为request
* handler:导常的处理类,struts默认采用org.apache.struts.action.ExceptionHandler,
如果做个性化的异常处理可以继承此类复写相应的方法
-------------------------------------------------------------------------
个性异常类定义
一 方法:
1 定义MessageResources.propertices资源文件
2 在struts-config中配置<exception/>
<exception key="error.exception" type="com.bjsxt.struts.ErrorCodeException"
handler="com.bjsxt.struts.ErrorCodeException" />
3 编写异常类ErrorCodeException继承RuntimeException
public class ErrorCodeException extends RuntimeException {
private String errorCode;//这是key对应的值
private Object[] args;//这是参数集合
public ErrorCodeException(String errorCode){
this(errorCode,null);
}
public ErrorCodeException(String errorCode,Object args0){
this(errorCode,new Object[]{args0});
}
public ErrorCodeException(String errorCode,Object[] args){
this.errorCode=errorCode;
this.args=args;
}
public String getErrorCode() {
return errorCode;
}
public Object[] getArgs() {
return args;
}
}
4 编写ErrorCodeExceptionHandler类继承ExceptionHandler,
复写public ActionForward execute(Exception ex, ExceptionConfig ae,
ActionMapping mapping, ActionForm formInstance,
HttpServletRequest request, HttpServletResponse response)
throws ServletException{}方法:
public ActionForward execute(Exception ex, ExceptionConfig ae,
ActionMapping mapping, ActionForm formInstance,
HttpServletRequest request, HttpServletResponse response)
throws ServletException {
//添加判断
------------------------------------------------------------
if(!(ex instanceof ErrorCodeException)){
return super.execute(ex, ae, mapping, formInstance, request, response);
}
------------------------------------------------------------
ActionForward forward;
ActionMessage error;
String property;
// Build the forward from the exception mapping if it exists
// or from the form input
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
} else {
forward = mapping.getInputForward();
}
// Figure out the error
if (ex instanceof ModuleException) {
error = ((ModuleException) ex).getActionMessage();
property = ((ModuleException) ex).getProperty();
} else {
//改修这个地方
//----------------------------------------------
ErrorCodeException ece=(ErrorCodeException)ex;
String errorCode=ece.getErrorCode();
Object[] args=ece.getArgs();
error = new ActionMessage(errorCode, args);
property = error.getKey();
//------------------------------------------------
//error = new ActionMessage(ae.getKey(), ex.getMessage());
//property = error.getKey();
}
this.logException(ex);
// Store the exception
request.setAttribute(Globals.EXCEPTION_KEY, ex);
this.storeException(request, property, error, forward, ae.getScope());
if (!response.isCommitted()) {
return forward;
}
return null;
}
}
5 页面直接抛出异常
public void login(String username,String password){
if(!"admin".equals(username)){
throw new ErrorCodeException("user.not.found",username,age);
}
}
---------------------------------------------------------------------
二 方法:
1 定义MessageResources.propertices资源文件
内容:error.exception={0}
2 在struts-config中配置<exception/>
<exception key="error.exception" type="com.bjsxt.struts.ErrorCodeException"/>
3 编写异常类ErrorCodeException继承RuntimeException
public class ErrorCodeException extends RuntimeException {
public ErrorCodeException(String msg){
super(msg);
}
}
4 页面直接抛出异常
public void login(String username,String password){
if(!"admin".equals(username)){
throw new ErrorCodeException("名称"+usrname+"错误!");
}
}
--------------------------------------------------------------------------
1 ApplicationResources.properties文件
num2Meg=is not a double
2 struts-config.xml
<message-resources parameter="ApplicationResources" />
3 ActionMessages errs=new ActionMessages();
errs.add("num1Error",new ActionMessage("num2Meg"));
//errs.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("num2Meg"));
this.saveErrors(request,errs);
页面
<html:errors property="num1Error"/>
//<html:errors/>//全局不能指定property
errs.header="<script>alert("
errs.footer=")</script>"
发表评论
-
[导入]为Struts 2.0做好准备
2009-10-29 19:00 0摘要: Struts 2.0系列之一 Struts ... -
[导入]常用的Struts 2.0的标志(Tag)介绍
2009-10-29 19:00 269摘要: 在上一篇文章《为Struts 2.0做好准备 ... -
[导入]Struts 2.0的Action讲解
2009-10-29 19:00 589摘要: 有Struts 1.x经验的朋友都知道Act ... -
[导入]在Struts 2.0中国际化(i18n)您的应用程序
2009-10-29 19:00 302摘要: Struts 2.0系列之四 国际化是商业系 ... -
[导入]转换器(Converter)——Struts 2.0中的魔术师
2009-10-29 19:00 431摘要: 在我已往的Struts 1.x项目经验中,有 ... -
[导入]在Struts 2.0中实现表单数据校验(Validation)
2009-10-29 19:00 275摘要: All Input Is Evil! ... -
[导入]Struts 2的基石——拦截器(Interceptor)
2009-10-29 19:00 340摘要: Struts 2.0系列之七 Interce ... -
[导入]在Struts 2中实现文件上传
2009-10-29 19:00 247摘要: Struts 2.0系列之九 Struts ... -
[导入]在Struts 2中实现CRUD
2009-10-29 19:00 331摘要: Struts 2系列之十 CRUD是Crea ... -
[导入]Struts 2中的OGNL
2009-10-29 19:00 512摘要: Struts 2系列之十一 OGNL是一种功 ... -
[导入]Strus 2的新表单标志的使用
2009-10-29 19:00 472摘要: Struts 2系列之十二 Struts 2 ... -
[导入]Struts 2与AJAX(第一部分)
2009-10-29 19:00 441摘要: Struts 2系列之十三 在当今——Web ... -
[导入]Struts 2与AJAX(第二部分)
2009-10-29 19:00 549摘要: Struts 2系列之十四 在上一篇文章《S ... -
[导入]Struts 2与AJAX(第三部分)
2009-10-29 19:00 536摘要: 在上两部分的《Struts 2与AJAX》中 ... -
struts2 页面转向错误No result defined for action and result Invalidation
2009-11-20 16:57 752Messages: No result defined for ... -
struts1.3-基础及原理
2009-11-29 22:59 563网站struts.apache.org 实践 ... -
struts1.3-DispatchAction
2009-11-30 08:10 375DynaActionForm 配态form 1 配置动态for ... -
struts1.3-Converter
2009-11-30 08:10 614Converter添加java.util.Date ... -
struts1.3-plugIn
2009-11-30 08:12 337可以加入ValidatorPlugIn动态验证框架插件 org ... -
struts1.3-ForwardAction
2009-11-30 08:12 666ForwardAction是统一访问形式 配置struts-c ...
相关推荐
3.6 异常处理:exception-mapping元素 47 3.7 通配符映射 48 3.8 动态方法调用 51 3.9 对动作类进行测试 51 3.10 小结 51 第4章 OGNL 52 4.1 Value Stack栈 52 4.2 读取Object Stack里的对象的属性 53 4.3 ...
3.6 异常处理:exception-mapping元素 47 3.7 通配符映射 48 3.8 动态方法调用 51 3.9 对动作类进行测试 51 3.10 小结 51 第4章 OGNL 52 4.1 Value Stack栈 52 4.2 读取Object Stack里的对象的属性 53 4.3 读取...
1.3 JavaWeb开发的基石-开发工具包JrDK 1.3.1 下载JDK 1.3.2 JDK的安装 1.3.3 配置和测试JDK 1.4 JavaWeb应用的Web服务器Tomcat 1.4.1 Web应用服务工作原理 1.4.2 web应用服务器汇总 1.4.3 获取Tomcat 1.4.4 熟悉...
{6}Exception}{99}{chapter.6} {6.1}\ttfamily try-catch}{99}{section.6.1} {6.2}\ttfamily finally}{100}{section.6.2} {6.3}\ttfamily throws}{101}{section.6.3} {7}IO}{103}{chapter.7} {7.1}Java的文件...
1.3 修订历史 8 1.4 参考资料 8 2 常用功能处理方式 9 2.1 如何执行条件查询 9 2.1.1 查询示例 9 2.1.2 查询限制 9 2.1.2.1 Like 9 2.1.2.2 In 10 2.1.2.3 Between 10 2.1.2.4 Equal 10 2.1.2.5 NotEqual 10 2.1.2.6...
第1章 Java Web应用开发简介 1 1.1 Java EE应用概述 1 1.2 Java EE概念 1 1.2.1 Java EE多层模型 1 1.2.2 Java EE体系结构 2 1.3 Java EE的核心API与组件 4 1.4 Web服务器和应用服务器 13 1.5 小结 16 第2章 建立...
5、语句float f=1.3;编译能否通过? 8 6、short s1 = 1; s1 = s1 + 1;有什么错? 8 7、Java 有没有goto? 8 8、int 和Integer 有什么区别? 9 9、&和&&的区别? 9 10、简述逻辑操作(&,|,^)与条件操作(&&,||)的区别? 9...
5、语句float f=1.3;编译能否通过? 8 6、short s1 = 1; s1 = s1 + 1;有什么错? 8 7、Java 有没有goto? 8 8、int 和Integer 有什么区别? 9 9、&和&&的区别? 9 10、简述逻辑操作(&,|,^)与条件操作(&&,||)的区别? 9...
1.3. 系统的开发目标 管理员通过前台页面进入后台管理模块后,可对注册的博客用户进行维护,包括对注册用户的添加、查找、修改和删除。 管理员进入登录后,可对帐户进行管理,包括添加管理员帐户、修改管理员帐户...