1 编写JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>注册信息(简单验证)</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
.text{
height: 20px;
width: 120px;
}
</style>
</head>
<body>
<html:messages id="errorUser" property="errorUser"></html:messages>
<html:form action="simpleValidation">
<table width="100%">
<tr>
<td align="right" width="45%">用户名:</td>
<td width="55%"><html:text property="user" styleClass="text"></html:text><font color="red"><html:errors property="errorUser"></html:errors></font></td>
</tr>
<tr>
<td align="right" width="45%">登陆密码:</td>
<td width="55%"><html:text property="password" styleClass="text"></html:text><font color="red"><html:errors property="errorPassword"></html:errors></font></td>
</tr>
<tr>
<td align="right" width="45%">重复登陆密码:</td>
<td width="55%"><html:text property="password1" styleClass="text"></html:text><font color="red"><html:errors property="errorPassword1"></html:errors></font></td>
</tr>
<tr>
<td align="right" width="45%">电子邮件:</td>
<td width="55%"><html:text property="email" styleClass="text"></html:text><font color="red"><html:errors property="errorEmail"></html:errors></font></td>
</tr>
<tr>
<td align="right"><br/>${requestScope.success}</td>
<td align="left"><br/><html:submit value="提交"></html:submit></td>
</tr>
</table>
</html:form>
</body>
</html>
2 编写Form
package com.du.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class SimpleValidationForm extends ActionForm{
/**
*
*/
private static final long serialVersionUID = 1L;
private String user;
private String password;
private String password1;
private String email;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors ae = new ActionErrors();
if(this.user == null || "".equals(this.user)){
ActionMessage am = new ActionMessage("error.user.blank");
ae.add("errorUser", am);
ActionMessage am1 = new ActionMessage("error.password.blank");
ae.add("errorUser", am1);
}
return ae;
}
}
3 编写Action
package com.du.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class SimpleAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("success", "提交成功");
return mapping.findForward("simple");
}
}
4 编写struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="simpleValidationForm" type="com.du.struts.SimpleValidationForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/simpleValidation" name="simpleValidationForm" type="com.du.struts.SimpleAction" input="/index.jsp">
<forward name="simple" path="/index.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="com.du.struts.ApplicationResources" />
</struts-config>
5 编写ApplicationResource.property文件
error.user.blank = User can't be null.
error.password.blank = Password can't be null.
error.password1.confirmation = Password doesn't match confirmation.
error.email.blank = Email can't be null.
error.email.invalid =It is not a valid email address.