开发框架之struts出错处理小探
Struts中提供方便的出错处理机能,利用ActionForm的validate方法就可以进行业务相关的错误处理。
①首先,我们会在ActionForm中validate方法中将报错信息存储在ActionErrors对象中,在这里只有ActionErrors才会被request捕获
| public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) { // TODO Auto-generated method stub ActionErrors errors = new ActionErrors(); if(username == null || "".equals(username)) { ActionError error = new ActionError("Invalid.username", "null"); //出现错误,将记录错误的key errors.add("usernameInvalid", error); } return errors; } |
Invalid.username=invalid user name "{0}"
这里的Invalid.username就对应到ActionError error = new ActionError("Invalid.username", "null");中的Invalid.username。
{0}将被null替换。
②JSP代码:
| < H2>< bean:message key="welcome.h2"/>< /H2>< html:errors/> |
| < action-mappings >
< action path="/LoginAction" input="/welcome.jsp" name="Login" type="com.home.struts.action.LoginAction" scope="request" validate="true" > < forward name="success" path="/blank.jsp"/> < forward name="fail" path="/welcome.jsp"/> < /action> < message-resources parameter="com.home.struts.ApplicationResources" /> < /struts-config> |
- 本文关键词:

