用这个标签老不成功,求解释!
- Java code
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.xml
- XML code
<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>
验证失败的JSP页面就一句
<html:errors/>
标签库也引进来了
后台输出
- Java code
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.
顺便抱怨下struts1的文档
------解决方案--------------------
------解决方案--------------------
是你自己应该写的。
算了,这样子先看到效果:
将
errors.add("name", new ActionMessage("name", "name must be not null!"));
改成
errors.add("name", new ActionMessage("name must be not null!", false));
之后,LZ再慢慢弄明白MessageResources.properties是怎么回事吧
------解决方案--------------------