Topic: 求助:为什么没有去调用 ActionForm 中 Validate 的方法? |
Print this page |
1.求助:为什么没有去调用 ActionForm 中 Validate 的方法? | Copy to clipboard |
Posted by: ntshenwh Posted on: 2005-12-04 15:00 大家好,我遇到个奇怪的问题,请大家帮忙解释一下: 1)------------------ struts-config.xml ----------------------------------- <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"> <struts-config> <form-beans> <form-bean name="userForm" type="mypackage.UserForm"/> </form-beans> <global-forwards> <forward name="userCreated" path="/viewUser.jsp"/> <forward name="userCreatedError" path="/createUserError.jsp"/> </global-forwards> <action-mappings> <action path="/createUser" type="mypackage.UserAction" name="userForm" scope="request" validate="true" input="/createUser.jsp"> </action> </action-mappings> <message-resources parameter="ApplicationResources"/> </struts-config> 2)-------------------- UserAction.java ------------------------------ package mypackage; import org.apache.struts.action.*; import javax.servlet.http.*; public class UserAction extends Action { public ActionForward perform(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) { // get data from actionform UserForm f = (UserForm)form; // call actionform's validate ActionErrors errors = f.validate(mapping,request) ; if ( errors != null) { if ( errors.size()>0) { System.out.println("Exist error, will navigate to error page"); request.setAttribute("errors",errors); return (mapping.findForward("userCreatedError")); } } try { UserBean bean = new UserBean(); bean.addUser(f.getUser()); } catch(Exception e) { e.printStackTrace(); } request.setAttribute("User",f.getUser()); return (mapping.findForward("userCreated")); } } 3)---------------------------- UserForm.java------------------------ package mypackage; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.*; public class UserForm extends ActionForm { private User user = new User(); public void setUser(User u) { this.user = u; } public User getUser() { return this.user; } public void setUserName(String username) { user.setUserName(username); } public String getUserName() { return user.getUserName(); } // reset form public void reset(ActionMapping mapping,HttpServletRequest request) { this.user = new User(); } // validate public ActionErrors Validate(ActionMapping mapping,HttpServletRequest request) { ActionErrors errors = new ActionErrors(); System.out.println("The password's legnth is " + this.user.getPassword().length()); if (this.user.getUserName()==null) { errors.add("UserName",new ActionError("User name should not be empty")); } if (this.user.getPassword().length()<3 ) { System.out.println("Find a error"); errors.add("Password",new ActionError("Password's length should be larger than 3")); } return errors; } } 4)--------------------- createUser.jsp --------------------------- <%@ page contentType="text/html;charset=gb2312" %> <%@ taglib uri="/struts-logic" prefix="logic" %> <%@ taglib uri="/struts-bean" prefix="bean" %> <%@ taglib uri="/struts-html" prefix="html" %> <html:html locale="true"> <head> <html:base/> <title> <bean:message key="index.title"/> </title> </head> <body> <h2>´´½¨Ò»¸öÓû§£º</h2> <html:errors/> <html:form action="createUser.do" method="get"> UserName;<html:text property="user.userName"/><br/> Password:<html:password property="user.password"/><br/> Age:<html:text property="user.age"/><br/> <html:submit property="submit"/> </html:form> </body> </html:html> 5)------------------------------------ createUserError.jsp---------------------- <%@ page contentType="text/html;charset=gb2312" import="mypackage.User,org.apache.struts.action.*" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %> <%@ taglib uri="/struts-logic" prefix="logic" %> <%@ taglib uri="/struts-bean" prefix="bean" %> <%@ taglib uri="/struts-html" prefix="html" %> <html:html locale="true"> <head> <html:base/> <title> <bean:message key="index.title"/> </title> </head> <body> <h2>The Errors is :</h2> <c:forEach var="err" items="${errors}"> <c:out value="${err.toString()}"/><br> </c:forEach> </body> </html:html> (注意下划线部分) 问题是, 我在 createUser.jsp 中, password 只输入2个字符(也就是不应该验证通过) 提交后,并没有按照我所想的那样 转到 createdUserError.jsp ? |
2.Re:求助:为什么没有去调用 ActionForm 中 Validate 的方法? [Re: ntshenwh] | Copy to clipboard |
Posted by: ntshenwh Posted on: 2005-12-05 12:22 麻烦哪位高手帮我看看,是不是我不应该这么写?谢谢 |
3.Re:求助:为什么没有去调用 ActionForm 中 Validate 的方法? [Re: ntshenwh] | Copy to clipboard |
Posted by: bigfoot007 Posted on: 2005-12-06 16:49 这个结果是肯定的,当你调用Validate()时,如果返回的errors不为空则,直接返回配置文件中的Input所指定的jsp文件,而不再继续执行action中的perform()函数。 你仔细研究一下struts的工作原理,就明白了。 如果你想在页面中显示你的错误提示信息,可以将提示错误的代码写入你的createUser.jsp 中,也就是将你的createUserError.jsp中的代码合并到createUser.jsp中。 |
4.Re:求助:为什么没有去调用 ActionForm 中 Validate 的方法? [Re: bigfoot007] | Copy to clipboard |
Posted by: ntshenwh Posted on: 2005-12-06 18:07 Thanks bigfoot007 for your kindlly helps good luck |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |