struts - error 提示问题! (有请 kui(kui) 及各位高手 进来解答)
这是我的 actionform :
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.io.Serializable;
public class OrderCarForm extends org.apache.struts.validator.ValidatorForm
implements Serializable {
private String errormessage;
private String password;
private String username;
public OrderCarForm()
{
}
public String getErrormessage()
{
return errormessage;
}
public void setErrormessage(String errormessage) {
this.errormessage = errormessage;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest)
{
/**@todo: finish this method, this is just the skeleton.*/
ActionErrors errors = new ActionErrors();
OrderCarForm form = new OrderCarForm();
try
{
if (username.equals("")) {
errors.add("username", new ActionError("username.errors"));
//errors.add(errors.GLOBAL_MESSAGE, new ActionMessage("username.errors"));
form.setErrormessage("用户名称不可以为空!");
//errors.add("error",new ActionMessage("用户名不可以为空!")) ;
}
if (password.equals(""))
errors.add("password", new ActionError("password.error"));
}
catch(Exception e)
{
System.out.println(e.getMessage() );
errors = null;
}
return errors;
}
public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest)
{
}
}
这是我的 action ;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class RegistrationAction extends Action
{
private Log log;
public RegistrationAction()
{
log = LogFactory.getFactory().getInstance(getClass().getName());
}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception
{
HttpSession session = request.getSession();
java.util.Locale locale = getLocale(request);
org.apache.struts.util.MessageResources messages = getResources(request);
OrderCarForm info = (OrderCarForm)form;
String action = request.getParameter("action");
if(isCancelled(request))
{
if(log.isInfoEnabled())
log.info(" " + mapping.getAttribute() + " - Registration transaction was cancelled");
removeFormBean(mapping, request);
return mapping.findForward("success");
} else
{
ActionErrors errors = new ActionErrors();
System.out.println("in action errorsadd !!");
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("error.login.failed"));
saveErrors(request,errors);
return (new ActionForward(mapping.getInput()));
}
}
这是 struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="orderCarForm" type="shopbag.OrderCarForm" />
</form-beans>
<global-forwards>
<forward name="home" path="/login.jsp" />
</global-forwards>
<action-mappings>
<action name="orderCarForm" type="shopbag.loginAction" input="/login.jsp" scope="session" path="/loginAction" />
</action-mappings>
<!-- Registration Action -->
<action path="/loginAction"
type="shopbag.RegistrationAction"
name="OrderCarForm"
scope="request"
validate="true"
input="/registration.jsp">
<forward name="success" path="/calendarRG.htm"/>
</action>
<!-- ========== Message Resources Definitions =========================== -->
<message-resources
parameter="shopbag.ApplicationResources"/>
<!-- ========== Plug Ins Configuration ================================== -->
<!--
Add multiple validator resource files by setting the pathnames property
with a comma delimitted list of resource files to load.
-->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
这是 jsp :
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html>
<head>
<title>
login.jsp
</title>
<LINK href="css/style1.css" rel="stylesheet" type="text/css">
<LINK href="css/aa.css" rel="stylesheet" type="text/css">
<LINK href="css/bb.css" rel="stylesheet" type="text/css">
<LINK href="css/cc.css" rel="stylesheet" type="text/css">
</head>
<html:errors/>
<body text="#000000" background="images/bg.gif">
<html:form action="loginAction" >
<table width="550" border="0" cellspacing="0" cellpadding="0" align="center" height="80%">
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0" bordercolor="#88B3D9" style="border-collapse: collapse" height="100">
<tr align="center">
<td valign="middle">
<table width="300" border="1" cellspacing="0" cellpadding="0" bordercolor="#88B3D9" style="border-collapse: collapse" height="100" align="center">
<tr bgcolor="#B3D9FF">
<td class="head" height="25" colspan="2">
<div align="center"><bean:message key="login.title"/></div>
</td>
</tr>
<tr>
<td align="center" class="fr" height="30">
<bean:message key="login.name"/>
</td>
<td align="center" class="button" height="30">
<html:text property="username" maxlength="10"/>
</td>
</tr>
<tr>
<td align="center" class="fr" height="30">
<bean:message key="login.pass"/>
</td>
<td align="center" class="button" height="30">
<html:password property="password" maxlength="10"/>
</td>
<tr>
<td height="35" align="center" colspan="2">
<html:submit property="submit" onclick="bCancel=false;">
<bean:message key="button.login"/>
</html:submit>
<html:reset property="reset">
<bean:message key="button.reset"/>
</html:reset>
</td>
</tr>
</table>
</td>
</tr>
</table>
</html:form>
<html:javascript formName="OrderCarForm"/>
</body>
</html:html>
我想知道 为什么利用 ActionErrors validate 的方法可以实现对数据的检验 但是没有提示具体的错误信息!!!