Struts2 - 使用XML实现简单Validation(从零开始学习Strust2_05)

开发环境:

Eclipse IDE for Java EE Developers(下载地址

struts-2.3.1.2(下载地址

apache-tomcat-6.0.35(下载地址


Validation除了可以再Action中通过Validation函数实现,还可以用过XML文件来实现。

使用XML实现Validation有两点要求

一个是XML文件的名字是RegisterAction-validation.xml形式,RegisterAction是对应的xxxAction.java的名字。

一个是将XML文件放置在对应Action的同一个包中。


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>








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值