尽管Struts框架提供了有效的异常处理机制,但不能保证处理所有的错误,这时Struts框架会把错误抛给Web容器,在默认情况下Web容器会向用户浏览器直接返回原始信息。
正在装载数据……
如果想避免直接让用户看到这些原始信息,可以在web.xml中配置
元素,以下代码演示了如何避免用户看到HTTP 404、HTTP 500错误和Exception异常。
web.xml:
404
/exception/error404.jsp
500
/exception/error500.jsp
java.lang.Exception
/exception/default.jsp
当WEB容器捕获到exception-type或error-code指定的错误时将跳到由location指定的页面。
? 问题:当form bean 为动态bean时,在action中无法对form bean数据进行验证,因为formbean没有具体实现类。action中无法引用
? ActionError/ActionErrors/ActionMessage/ActionMessages:
有时候你需要向用户提供相关处理信息,包括表单验证时发现错误等。
1. 相关类介绍:
ActionMessage:用于保存一个与资源束对应的提示信息。主要构造函数如:
ActionMessage(String message);
ActionMessage(String message,paramater)。
ActionMessages:用于保存多个ActionMessage。并在html:errors 和html:messages中起作用。
主要构造函数:
ActionMessages().
主要方法是add(String property,ActionMessage message)
ActionMessages 有一个HashMap类型messages保存多个ActionMessage对象,每个ActionMessage对象都有唯一的一个property 标识。这个property可以是自定义的任意字符串,也可以由 org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE指定
html:messages/html:errors使用property属性访问某个资源
ActionErrors:用于保存一个与资源束对应的错误信息。用法跟ActionMessages差不多。
ActionError不赞成使用。
2. 版本:
struts1.1中用ActionErrors报告错误,用ActionMessages提供信息。
在struts1.2中使用ActionMessages提供信息和错误,不赞成使用ActionError
struts1.3中已经没有ActionError类了。
3. AtionErrors和ActionMessages的区别
1. ActionErrors是ActionMessages的一个子类,功能几乎相同,不同点在于标签和的使用上的区别。
html: errors指定了footer和header属性。默认值为errors.header和errors.footer,需要时可以自己指定。如果资源属性文件配置了 errors.header和errors.footer,则任何时候使用html:errors时开头和结尾都是这两个属性对应的资源信息。
而html:message默认情况下没有errors.header和errors.footer值,当然可以自己指定。
2. html:errors可以根据property属性指定显示一个错误信息。html:messages有一个必添项id。html:messages不能直接显示信息,它将选出的信息放入一个用id标识的Iterator对象里,然后在用ben:write或JSTL c:out标签显示每个信息.例如:
3. 具体的一个例子:
接受输入页面input.jsp:
phoneNumber :
struts-config.xml:
attribute="inputForm"
input="/errormessage/input.jsp"
name="inputForm"
path="/errormessage/input"
scope="request"
type="com.yourcompany.struts.action.errormessage.InputAction"
validate="false">
InputAction.java:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
cn.rolia.struts.form.errorexception.InputForm inputForm = (cn.rolia.struts.form.errorexception.InputForm) form;// TODO Auto-generated method stub
String phoneNumber = inputForm.getPhoneNumber();
if(phoneNumber.length()<4){
ActionErrors messages = new ActionErrors();
messages.add(org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.errormessage.input"));
this.saveErrors(request, messages);
return mapping.getInputForward();
}
return mapping.findForward("success");
}
解说:用户输入手机号码,页面跳转到InputAction控制层进行处理,若输入数据小于4,则创建一个ActionMessage类存储相关错误信息。然后再创建ActionErrors类将此ActionMessage放入ActionErrors。再调用Action的saveErrors方法将此 ActionErrors保存的request范围里,然后返回input.jsp页面要求重新输入并用html:errors提示错误信息。
4. Action包含saveErrors()方法和saveMessages()方法。
如果创建的ActionErrors则应该调用saveErrors(),若创建的是ActionMessages则应该调用saveMessages()方法。
saveErrors ()接收ActionMessages而不是ActionErrors;同时将其保存在request中并用一个由 org.apache.struts.Globals.ERROR_KEY指定的常量” org.apache.struts.Globals.ERROR_KEY”标识这个ActionMessages,便于html:errors查找。 saveMessages()方法接收ActionMessages同时将其保存在request中并用一个由 org.apache.struts.Globals.MESSAGE_KEY指定的常量” org.apache.struts.Globals.MESSAGE_KEY”标识这个ActionMessages,进而让html: messages从常量Globals.ERROR_KEY中遍历获取信息。可以将其属性message设置为true,那么它将从常量 Globals.MESSAGE_KEY中遍历获取信息。
5. 默认情况下html:messages从
如果你想将信息保存在session里而不是request,struts1.2提供了
struts1.1 没有的saveMessages(HttpSession session, ActionMessages messages)方法和saveErrors(javax.servlet.http.HttpSession session, ActionMessages errors)方法。
InputAction.java:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
cn.rolia.struts.form.errorexception.InputForm inputForm = (cn.rolia.struts.form.errorexception.InputForm) form;// TODO Auto-generated method stub
String phoneNumber = inputForm.getPhoneNumber();
if(phoneNumber.length()<4){
ActionErrors messages = new ActionErrors();
messages.add(org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.errormessage.input"));
this.saveErrors(request.getSession(true), messages);
return mapping.getInputForward();
}
return mapping.findForward("success");
}
Tags: java 笔记 Eclipse java基础 JSP编程 struts
原创文章如转载,请注明:转载自:飞扬部落编程仓库 : http://www.busfly.net/csdn/
本文链接地址:http://www.busfly.net/csdn/post/404.html
如果你喜欢本文,请顶一下,支持我,你的支持是我继续发好文章的最大动力。谢谢。
好东西需要分享,快把本文发给你的朋友吧~!~