ntshenwh
发贴: 40
积分: 0
|
于 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 ?
勿在浮砂筑高台
|