struts配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- 定义数据源 -->
<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property
property="driverClassName"
value="com.mysql.jdbc.Driver"/>
<set-property
property="url"
value="jdbc:mysql://localhost:3306/test" />
<set-property value="root" property="username"/>
<set-property value="root123" property="password"/>
</data-source>
</data-sources>
<form-beans>
<form-bean name="loginForm" type="com.hsia.form.LoginForm"></form-bean>
</form-beans>
<global-exceptions/> <!--定义全局异常-->
<global-forwards/> <!--定义全局转发-->
<action-mappings>
<action
name="loginForm"
attribute="lgForm"
path="/login"
scope="request"
input="/index.jsp"
type="com.hsia.action.LoginAction">
<forward name="success" path="/jsp/login.jsp"></forward>
<forward name="error" path="/jsp/error.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="com.validator.struts.ApplicationResource"></message-resources>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validator.xml"/>
</plug-in>
</struts-config>
validator.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="lgForm">
<field property="name" depends="required,minlength">
<arg0 key="name"/>
<arg1 key="${var:minlength}" resource="false" />
<var>
<var-name>minlength</var-name>
<var-value>4</var-value>
</var>
</field>
<field
property="password"
depends="required,minlength,maxlength">
<arg0 key="password"/>
<arg1 key="${var:maxlength}" resource="false" />
<arg2 key="${var:minlength}" resource="false" />
<var>
<var-name>maxlength</var-name>
<var-value>6</var-value>
</var>
<var>
<var-name>minlength</var-name>
<var-value>1</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
validator-rules.xml文件是在strut1.2.9-lib中找到的。对应于每一个版本的struts都有该文件,一般不去修改其中的代码
资源文件applicationResource.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.
name=name
password=password
login=login
# Errors
errors.header=<h3><font color"="red">Validation "u9519"u8BEF"uFF01"uFF01"uFF01</font>
jsp文件:
Java代码
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@taglib prefix="bean" uri="/WEB-INF/struts-bean.tld"%>
- <%@taglib prefix="html" uri="/WEB-INF/struts-html.tld" %>
- <%@taglib prefix="logic" uri="/WEB-INF/struts-logic.tld" %>
-
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Insert title here</title>
-
- <script type="text/javascript">
-
- function sm()
- {
- alert("郁闷!!");
- }
-
- </script>
-
- </head>
-
- <!-- 下面的标签用于输出出错信息 -->
- <html:errors/>
- <body>
-
-
- <html:form action="login.do" method="post">
- <table border="0">
-
- <tr>
-
- <td>name:</td>
-
- <td><html:text property="name"></html:text></td>
-
- </tr>
-
- <tr>
-
- <td><bean:message key="password"/>:</td>
-
- <td><html:text property="password"></html:text></td>
-
- </tr>
-
- <tr>
-
- <td colspan="2" align="center"><input type="button" value="郁闷" onclick="sm();"/></td>
- <td colspan="2" align="center"><html:submit><bean:message key="login"/></html:submit></td>
- </tr>
-
- </table>
-
- </html:form>
- </body>
- </html>