一、需求
所谓数据校验,即当用户输入的信息不符合要求时,程序会直接返回,提示用户再次输入正确的信息。数据校验有客户端校验和服务端校验,而XWork校验框架就是服务器检验实现方法之一。
在struts2项目中,当存在大量Action时,需要多次重写validate方法,这使得代码非常繁琐。通过使用XWork校验框架来进行数据校验,可减少代码量。
二、XWork校验框架实现
通过简单的struts2项目来说明XWork校验框架的实现。
1、创建struts2项目
在Eclipe中新建一个Dynamic Web Project,添加struts2所需要的JAR包,编写web.xml配置文件。
web.xml配置文件代码如下:
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts2-4</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app></span>
2、编写Action类
<span style="font-size:14px;">package com.struts.action;
import com.opensymphony.xwork2.ActionSupport;
public class ShowAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String name;
private String password;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String execute() throws Exception {
return SUCCESS;
}
}</span><span style="font-size:12px;">
</span>
3、编写struts.xml配置文件
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf-8"/>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="show" class="com.struts.action.ShowAction">
<result>/success.jsp</result>
<strong><span style="color:#ff0000;"> <!-- 检验出错返回 input -->
<result name="input">/InputError.jsp</result></span></strong>
</action>
</package>
</struts></span>
4、 在与Action类包下创建验证文件ShowAction-validation.xml,采用字段校验方式,并编辑代码如下:
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="age">
<field-validator type="conversion" short-circuit="true">
<message>年龄必须为整数!</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int" short-circuit="true">
<param name="min">18</param>
<param name="max">100</param>
<message>年龄必须在${min}到${max}之间!</message>
</field-validator>
</field>
<field name="age">
<field-validator type="required" short-circuit="true">
<message>年龄不能为空!</message>
</field-validator>
</field>
<field name="name">
<field-validator type="requiredstring" short-circuit="true">
<message>姓名不能为空!</message>
</field-validator>
</field>
<field name="password">
<field-validator type="stringlength" short-circuit="true">
<param name="maxLength">15</param>
<param name="minLength">6</param>
<message>密码长度必须为${minLength}到${maxLength}之间!</message>
</field-validator>
</field>
</validators> </span>
在使用验证文件时,要严格遵守其命名规则。检验文件的命名规则为:actionName-validation.xml,其中actionName是指需要校验的Action的类名,且该文件总与Action类的class文件位于相同路径下。Struts2框架提供了两种方式来配置校验文件,分别是字段校验方式和非字段校验方式。上述代码是使用了字段检验方式。
5、编写JSP页面
index.jsp代码如下:
<span style="font-size:14px;"><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=UTF-8">
<title>首页</title>
</head>
<body>
<center>
<s:form action="show" method="post" theme="simple">
用户名:<s:textfield name="name"/><br/>
年龄:<s:textfield name="age"/><br/>
密码:<s:textfield name="password"/><br/>
<s:submit value="提交"/>
</s:form>
<br/><br/><br/><br/>
<h3><s:a action="hello">Hello</s:a></h3>
</center>
</body>
</html></span>
success.jsp代码如下:
<span style="font-size:14px;"><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=UTF-8">
<title>展示</title>
</head>
<body>
<center>
Name:<s:property value="name"/><br>
Age:<s:property value="age"/><br>
Password:<s:property value="password"/><br>
</center>
</body>
</html></span>
InputError.jsp代码如下:
<span style="font-size:14px;"><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=UTF-8">
<title>错误!</title>
</head>
<body>
<center>
<h3><s:fielderror/></h3>
<s:a href="index.jsp">返回首页</s:a>
</center>
</body>
</html></span>
这样就实现了XWork校验框架。