• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

半翻译半整理的一点struts的东东,很浅显的,欢迎指正(四)

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 15次 | 进入软件测试论坛讨论

领测软件测试网 二、    出错管理(Managing Errors)
Struts框架有两个主要的类来管理出错,一个是org.apache.struts.action.ActionError类,它对错误信息进行包装。另一个是org.apache.struts.action.ActionErrors类,它是ActionError实例的容器。这两个类经常要在ActionForm及Action类中使用。其具体的使用如下:
ActionErrors errors = new ActionErrors();
errors.add("propertyname", new ActionError("key");
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("key");
关于"propertyname"和ActionErrors.GLOBAL_ERROR,对前者用在ActionForm中,这里是对应表现层(JSP)中的属性值。而对后者则用在Action中,它对应struts-config.xml的<global-forwards />中描述的信息。例子:
ActionForm类:
public class LoginForm extends ActionForm {
…………………
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if ( (username == null ) || (username.length() == 0) ) {
            errors.add("username",new ActionError("errors.username.required"));
        }
        if ( (password == null ) || (password.length() == 0) ) {
            errors.add("password",new ActionError("errors.password.required"));
        }
        return errors;
    }
…………………
}

Action类:
public class LoginAction extends Action {
……………………
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request, HttpServletResponse  response) throws IOException, ServletException {
        String user = null;
        // Default target to success
        String target = "success";
        // Use the LoginForm to get the request parameters
        String username = ((LoginForm)form).getUsername();
        String password = ((LoginForm)form).getPassword();
        user = getUser(username, password);
        // Set the target to failure
        if ( user == null ) {
            target = "login";
            ActionErrors errors = new ActionErrors();
            errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("errors.login.unknown",username));
            // Report any errors we have discovered back to the
            // original form
            if (!errors.empty()) {
                saveErrors(request, errors);
            }
        }
        else {
            HttpSession session = request.getSession();
            session.setAttribute("USER", user);
        }
        // Forward to the appropriate View
        return (mapping.findForward(target));
    }
}
在表现层中表现错误只须要写上<html:error />标签即可。

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网