zoukankan      html  css  js  c++  java
  • JAVAEE错误处理

    错误分为前端错误,逻辑错误。错误信息显示的标签<html:errors property=""/>如果用的不是默认的资源文件则需要绑定使用bundle<html:errors property="" bundle=""/>

    property的内容是errors属性标志bundle是其他资源文件的关键字key,在struts.xml里找

    前段错误:比如验证文本框里的内容是否为空,长度,等等。一般在form的validate里写,举例:

    public ActionErrors validate(ActionMapping mapping,  HttpServletRequest request) {
        ActionErrors errors = new ActionErrors(); //专门容纳ActinError,当内部有ActionError是就认为发生了前段错误
        if (accout.length() == 0) {
         ActionMessage msg = new ActionMessage("内容不能为空"); //第一个参数,消息内容key,第二个参数取代0的值
         errors.add("accout",msg); //第一个参数对这个error加一个属性标记
        }  

       return errors;
    }

    ActionMessage(String key)

    ActionMessage(String key,Object value)
    ActionMessage(String key,Object value0,Object value1)
    ActionMessage(String key,Object value0,Object value1,Object value2)
    ActionMessage(String key,Object value0,Object value1,Object value2,Object value3)

    key是在资源文件中配置的key值,必须在配置文件中进行相关配置.

    逻辑错误:输入的内容不符合要求,一般在action里实现,注意别忘保存错误(errors)使用this.saveErrors(request, errors);举例:

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
      LoginForm loginForm = (LoginForm) form  
      String account = loginForm.getAccout();
      
      ActionMessages errors = new ActionMessages();


      if (account.equals("abcdefg")) {
         ActionMessage msg = new ActionMessage("error.login", account);
         errors.add("login", msg);
         this.saveErrors(request, errors);
         return mapping.getInputForward();
      }
      return null;
     }

    其中不提倡使用saveErrors(HttpServletRequest,ActionErrors),改用saveErrors(HttpServletRequest,ActionMessages) 。因此定义errors时用ActionMessages。其中ActionErrors是的ActionErrors父类。

  • 相关阅读:
    Linux 守护进程一
    Linux 改进捕捉信号机制(sigaction,sigqueue)
    Linux 发送信号
    Linux 信号捕捉
    Heartbeat+DRBD+MFS高可用
    centos7 MFS drbd keepalived
    RabbitMQ-官方指南-RabbitMQ配置
    CentOS 7 单用户模式+救援模式
    CentOS6.8 x64+Nginx1.3.8/Apache-httpd 2.4.3+PHP5.4.8(php-fpm)+MySQL5.5.28+CoreSeek4.1源码编译安装
    nginx定制header返回信息模块ngx_headers_more
  • 原文地址:https://www.cnblogs.com/wyhong/p/2387911.html
Copyright © 2011-2022 走看看