Struts 2-XMLを使用した簡単なValidation(ゼロからStrust 2_05を学ぶ)


開発環境:
Eclipse IDE for Java EE Developers(ダウンロードアドレス)
struts-2.3.1.2(ダウンロードアドレス)
apache-tomcat-6.0.35(ダウンロードアドレス)
Validationは、Validation関数による再アクションに加え、XMLファイルによる実装も可能です.
XMLによるValidationの実装には2つの要件があります
一つはXMLファイルの名前がRegisterAction-validation.xml形式で、RegisterActionは対応するxxxAction.javaの名前です.
1つは、対応するActionの同じパッケージにXMLファイルを配置することです.
web.xmlは変わっていません
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>struts2_20120313_01</display-name>
	<display-name>struts2_20120312_01</display-name>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</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>

struts.xmlは関数Validationと同じです
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<constant name="struts.devMode" value="true" />
	<package name="default" extends="struts-default">
		<action name="RegisterAction" class="com.zeph.struts2.action.RegisterAction">
			<result name="success">/regSuccess.jsp</result>
			<result name="input">/register.jsp</result>
		</action>
		<action name="registerInput" class="com.zeph.struts2.action.RegisterAction"
			method="input">
			<result name="input">/register.jsp</result>
		</action>
	</package>
</struts>

RegisterAction.java
package com.zeph.struts2.action;

import com.opensymphony.xwork2.ActionSupport;
import com.zeph.struts2.bean.UserRegInfo;

public class RegisterAction extends ActionSupport {

	private static final long serialVersionUID = 7711471040010683683L;

	private UserRegInfo userRegInfoBean;

	public UserRegInfo getUserRegInfoBean() {
		return userRegInfoBean;
	}

	public void setUserRegInfoBean(UserRegInfo userRegInfoBean) {
		this.userRegInfoBean = userRegInfoBean;
	}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
}

UserRegInfo.java
package com.zeph.struts2.bean;

public class UserRegInfo {
	private String account;
	private String password;
	private String pwdValidate;
	private String realName;
	private String email;
	private String msn;
	private String qq;

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPwdValidate() {
		return pwdValidate;
	}

	public void setPwdValidate(String pwdValidate) {
		this.pwdValidate = pwdValidate;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getMsn() {
		return msn;
	}

	public void setMsn(String msn) {
		this.msn = msn;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	@Override
	public String toString() {
		return "Account:" + getAccount() + "Real Name:" + getRealName()
				+ "E-Mail:" + getEmail() + "MSN:" + getMsn() + "QQ:" + getQq();
	}
}

Propertiesファイルを使用してkeyを実装するため、jspは次のようになります.
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>A action link to Register page to make sure properties
	can be used</title>
</head>
<body>
	<s:url action="registerInput" var="registerInputLink" />
	<p>
		<a href="${registerInputLink}">Please register</a>
	</p>
</body>
</html>

register.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Register</title>
</head>
<body>
	<s:form action="RegisterAction">
		<s:textfield key="userRegInfoBean.account"></s:textfield>
		<s:password key="userRegInfoBean.password"></s:password>
		<s:password key="userRegInfoBean.pwdValidate"></s:password>
		<s:textfield key="userRegInfoBean.realName"></s:textfield>
		<s:textfield key="userRegInfoBean.email"></s:textfield>
		<s:textfield key="userRegInfoBean.msn"></s:textfield>
		<s:textfield key="userRegInfoBean.qq"></s:textfield>
		<s:submit></s:submit>
	</s:form>
</body>
</html>

regSuccess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Register Success</title>
</head>
<body>
	account:
	<s:property value="userRegInfoBean.account" />
	<br> real name:
	<s:property value="userRegInfoBean.realName" />
	<br> e-mail:
	<s:property value="userRegInfoBean.email" />
	<br> MSN:
	<s:property value="userRegInfoBean.msn" />
	<br> QQ:
	<s:property value="userRegInfoBean.qq" />
</body>
</html>

RegisterAction.properties
#Created by JInto - www.guh-software.de
#Mon Mar 12 23:45:24 CST 2012
userRegInfoBean.account=Account
userRegInfoBean.email=E-Mail
userRegInfoBean.msn=MSN
userRegInfoBean.password=Password
userRegInfoBean.pwdValidate=Input Password Again
userRegInfoBean.qq=QQ
userRegInfoBean.realName=Real Name

最後にRegisterAction-validation.xmlですが、ここでは簡単なValidationメソッドについてのみ説明します.
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
	<validator type="requiredstring">
		<param name="fieldname">userRegInfoBean.account</param>
		<message>Account required.Not empty.</message>
	</validator>
	<validator type="requiredstring">
		<param name="fieldname">userRegInfoBean.realName</param>
		<message>Real Name required.Not empty.</message>
	</validator>
	<validator type="email">
		<param name="fieldname">userRegInfoBean.email</param>
		<message>Email address not valid.</message>
	</validator>
	<validator type="fieldexpression">
		<param name="fieldname">userRegInfoBean.pwdValidate</param>
		<param name="expression"><![CDATA[userRegInfoBean.pwdValidate == userRegInfoBean.password]]></param>
		<message>Password input is inconsistent</message>
	</validator>
</validators>