要在struts中用validate框架,必须导入相应的jar包。我已经在附件中上传了一个struts的一个空白项目,所有的jar包可以在附件中的struts-blank-1.3.10.war可以把他中lib包中的所有jar包拷贝到自己的项目中。
validate框架是验证表单中添写的信息是否合法,比如用户名不能为空、email的格式是否正确等。类似于JavaScript。
项目的大致轮廓在附件的图片中
下面我将最基本的validate框架实现代码公布如下:
struts-config.xml代码如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <!-- 接收表单数据的ActionForm(FormBean) --> <form-beans> <form-bean name="loginForm" type="com.struts.forms.LoginForm"></form-bean> </form-beans> <!-- 用于让index.jsp文件跳转到LoginUI.do --> <global-forwards> <forward name="loginui" path="/LoginUI.do"></forward> </global-forwards> <action-mappings> <!-- 此Action用于跳转到登录页面 --> <action path="/LoginUI" type="com.struts.actions.LoginUI"> <forward name="success" path="/WEB-INF/user/userLogin.jsp"></forward> </action> <!-- 此Action用于接收并判断用户的信息 --> <action path="/Login" name="loginForm" input="/WEB-INF/user/userLogin.jsp" scope="request" type="com.struts.actions.Login"> <forward name="success" path="/WEB-INF/user/main.jsp"></forward> </action> </action-mappings> <!-- 用于指定国际化的资源文件的所在位置,注意不要加.properties --> <message-resources parameter="com.struts.properties.Application"> </message-resources> <!-- 把validate框架所用到的两个xml文件加载到项目中 --> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/org/apache/struts/validator/validator-rules.xml, /WEB-INF/validation.xml"/> </plug-in> </struts-config>
web.xml文件内容如下:
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
validate.xml文件内容如下(validate-rules.xml文件用系统默认的就可以):
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd"> <form-validation> <formset> <!-- An example form --> <form name="loginForm"> <field property="username" depends="required"> <arg key="loginForm.username"/> </field> <field property="password" depends="required,minlength,maxlength"> <arg key="loginForm.password" position="0"/> <!-- ${var:minlength}的作用是引用下面定义的变量 --> <arg key="${var:minlength}" name="minlength" resource="false" position="1"/> <arg key="${var:maxlength}" name="maxlength" resource="false" position="1"/> <var> <var-name>minlength</var-name> <var-value>4</var-value> </var> <var> <var-name>maxlength</var-name> <var-value>7</var-value> </var> </field> </form> </formset> </form-validation>
userLogin.jsp文件如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <% 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>My JSP 'userLogin.jsp' starting page</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> <html:form action="/Login" method="POST"> <bean:message key="loginForm.username" /><html:text property="username"></html:text><html:errors property="username" /><br/> <bean:message key="loginForm.password" /><html:password property="password"></html:password><html:errors property="password" /><br/> <html:submit><bean:message key="loginForm.submit" /></html:submit> <html:reset><bean:message key="loginForm.reset" /></html:reset> </html:form> </body> </html>
index.jsp文件内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <logic:forward name="loginui"/>
Login.java
package com.struts.actions; 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 Login extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return null;//mapping.findForward("success") ; } }
LoginUI.java
package com.struts.actions; 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 LoginUI extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("success") ; } }
LoginForm.java
package com.struts.forms; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMapping; import org.apache.struts.validator.ValidatorForm; public class LoginForm extends /*ActionForm*/ValidatorForm { /** * */ private static final long serialVersionUID = 1L; private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { return super.validate(mapping, request); } }
Application_en.properties
errors.required=(en){0} is required. errors.minlength=(en){0} can not be less than {1} characters. errors.maxlength=(en){0} can not be greater than {1} characters. loginForm.username=username loginForm.password=password loginForm.submit=submit loginForm.reset=reset
Application.properties
errors.required={0} is required. errors.minlength={0} can not be less than {1} characters. errors.maxlength={0} can not be greater than {1} characters. loginForm.username=username loginForm.password=password loginForm.submit=submit loginForm.reset=reset
Application_zh_CN.properties
errors.required={0} \u662F\u5FC5\u586B\u9879. errors.minlength={0} \u4E0D\u80FD\u5C0F\u4E8E {1} \u5B57\u7B26. errors.maxlength={0} \u4E0D\u80FD\u5927\u4E8E {1} \u5B57\u7B26. loginForm.username=\u7528\u6237\u540D loginForm.password=\u5BC6\u7801 loginForm.submit=\u63D0\u4EA4 loginForm.reset=\u91CD\u7F6E
完毕,自己去试试吧。
如果有什么问题可以随时跟我联系,我们可以交流下。