日期:2014-05-20 浏览次数:20929 次
public class ValidateForm extends ActionForm { private User user = new User(); public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if(user.getName() == null || "".equals(user.getName().trim())) { System.out.println("name must be not null!"); errors.add("name", new ActionMessage("name", "name must be not null!")); } if(errors.get().hasNext()) { System.out.println("here is ValidateForm and has errors"); return errors; } else { System.out.println("here is ValidateForm and no errors"); return null; } } } Action的execute方法只有一句mapping.findForward("success");
<struts-config> <form-beans> <form-bean name="validateForm" type="com.struts.form.ValidateForm"> </form-bean> </form-beans> <action-mappings> <!-- 带有验证功能的ACTION --> <action path="/validateAction" name="validateForm" type="com.struts.action.ValidateAction" input="/ValidateAction/errors.jsp" validate="true"> <forward name="success" path="/WEB-INF/user/success.jsp"></forward> </action> </action-mappings> <message-resources parameter="MessageResources" /> </struts-config>
name must be not null! here is ValidateForm and has errors //上面这两句是System.out.println输出的 2011-4-27 21:32:02 org.apache.struts.util.PropertyMessageResources loadLocale 警告: Resource MessageResources_zh_CN.properties Not Found. 2011-4-27 21:32:02 org.apache.struts.util.PropertyMessageResources loadLocale 警告: Resource MessageResources_zh.properties Not Found. 2011-4-27 21:32:02 org.apache.struts.util.PropertyMessageResources loadLocale 警告: Resource MessageResources.properties Not Found.