validator验证小例子

<?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>
<form-beans>
<form-bean name="userForm" type="com.UserForm"/>
</form-beans>
<action-mappings>
<action path="/register" name="userForm" attribute="userForm" scope="request" input="/input.jsp" type="action.DealAction">
<forward name="success" path="/main.jsp"/>
<forward name="error" path="/input.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.ApplicationResources"/>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validations.xml"/>
</plug-in>
</struts-config>
 input.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<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">
	-->

	</head>

	<body>
		<table align="center">
			<tr>
				<td>
					<table>
						<tr>
							<td>
								<logic:messagesPresent>
									<fieldset class="errorMessage">
										<legend>
										<bean:message key="errors.header" />
										</legend>
										<ul>
												<html:messages id="error">
													<li>
														<bean:write name="error" />
													</li>
												</html:messages>
											</ul>
									</fieldset>
								</logic:messagesPresent>
							</td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td>
					<form action="register.do" method="post">
						<fieldset>
							<legend>
								登陆界面
							</legend>
							<table>
								<tr>
									<td>
										姓名:
									</td>
									<td>
										<input type="text" name="name" id="name">
									</td>
								</tr>
								<tr>
								<td>邮箱:</td>
								<td><input type="text" name="email" id="email"/></td>
								</tr>
								<tr><td>生日:</td>
								<td><input type="text" name="birthday" id="birthday"/></td>
								</tr>
								<tr><td colspan="2"><input type="submit" value="保存"/></td></tr>
							</table>
						</fieldset>
					</form>
				</td>
			</tr>
		</table>
	</body>
</html>
 配置文件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>
<form-beans>
<form-bean name="userForm" type="com.UserForm"/>
</form-beans>
<action-mappings>
<action path="/register" name="userForm" attribute="userForm" scope="request" input="/input.jsp" type="action.DealAction">
<forward name="success" path="/main.jsp"/>
<forward name="error" path="/input.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.ApplicationResources"/>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validations.xml"/>
</plug-in>
</struts-config>
 配置文件validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">

<form-validation>
	<formset>
		<form name="userForm">
			<field property="name" depends="required">
				<arg key="userForm.name" />
			</field>
			<field property="email" depends="email">
				<arg key="userForm.email" />
			</field>
			<field property="birthday" depends="date">
				<arg key="userForm.birthday" />
				<var>
					<var-name>datePatternStrict</var-name>
					<var-value>yyyy-MM-dd</var-value>
				</var>
			</field>
		</form>
	</formset>
</form-validation>

 配置文件validator-rules.xml为struts包中的元文件目录为:struts-1.3.8\src\core\src\main\resources\org\apache\struts\validator
ApplicationResource.properties
errors.date     = {0}\u4E0D\u662F\u65E5\u671F
errors.email    = {0}\u4E0D\u662F\u5408\u6CD5\u7684\u90AE\u4EF6\u5730\u5740
errors.header   = \u9519\u8BEF\u4FE1\u606F\u63D0\u793A
errors.required = {0}\u4E3A\u7A7A

userForm.birthday = \u51FA\u751F\u65E5\u671F
userForm.email    = email
userForm.name     = \u7528\u6237\u540D
 UserForm.java
package com;

import org.apache.struts.validator.ValidatorForm;

public class UserForm extends ValidatorForm {
    /**
	 * 
	 */
	
	private static final long serialVersionUID = 1L;
	private String  name;
    private String  email;
    private String  birthday;
    
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	
    
}
 
DealAction.java
package action;

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;

import com.UserForm;

public class DealAction extends Action {
  @Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
		HttpServletRequest request, HttpServletResponse response)
		throws Exception {
	UserForm userform=(UserForm)form;
	return  mapping.findForward("success");
}
}
 

你可能感兴趣的:(apache,bean,jsp,xml,struts)