使用“Jakarta Struts for Dummies”一书学习 Struts 1.1,同时阅读ActionForm
它说
validate method: Is called after the `ActionServlet` populates the
form with the request properties and before the form is passed to a particular
`Action` class for processing
实际上,我们提交带有属性(比如用户名或密码)的表单,这些都在请求中填充。但这里是说ActionServlet
填充表格?
我认为这里的形式手段ActionForm
或其子类仪式,如果我错了,请纠正我。
这是表格:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- JSTL tag libs --%>
<%@ taglib prefix="fmt" uri="/WEB-INF/fmt.tld" %>
<%-- Struts provided Taglibs --%>
<%@ taglib prefix="html" uri="/WEB-INF/struts-html-el.tld" %>
<html:html locale="true"/>
<head>
<fmt:setBundle basename="ApplicationResources" />
<title><fmt:message key="login.title"/></title>
</head>
<body>
<html:errors property="login"/>
<html:form action="login.do" focus="userName">
<table align="center">
<tr align="center">
<td><H1><fmt:message key="login.message"/></H1></td>
</tr>
<tr align="center">
<td>
<table align="center">
<tr>
<td align="right">
<fmt:message key="login.username"/>
</td>
<td align="left">
<html:text property="userName"
size="15"
maxlength="15" />
<html:errors property="userName" />
</td>
</tr>
<tr>
<td align="right">
<fmt:message key="login.password"/>
</td>
<td align="left">
<html:password property="password"
size="15"
maxlength="15"
redisplay="false"/>
<html:errors property="password" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<html:submit>
<fmt:message key="login.button.signon"/>
</html:submit>
</td>
</tr>
</table>
</td>
</tr>
</table>
</html:form>
</body>
</html>
这是LoginForm
:
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* @author kiran
*
*/
public class LoginForm extends ActionForm
{
private String userName;
private String password;
int count=0;
/**
* Resets data fields to initial values on loginform
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request)
{
password = "";
userName = "";
count++;
System.out.println("reset method is called"+count+ " time/s");
}
/**
* Performs validation of data on loginform
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
{
System.out.println("validate method is called"+count+" time/s");
System.out.println(getUserName());
ActionErrors errors = new ActionErrors();
if((userName == null) || (userName.length() < 1))
errors.add("userName", new ActionError("error.username.required"));
if((password == null) || (password.length() < 1))
errors.add("password", new ActionError("error.password.required"));
return errors;
}
/**
* @return String
*/
public String getPassword() {
return password;
}
/**
* @return String
*/
public String getUserName() {
return userName;
}
/**
* @param string
*/
public void setPassword(String string) {
password = string;
}
/**
* @param string
*/
public void setUserName(String string) {
userName = string;
}
}