Struts2 - 运用基本JavaBean和简单的Validation(从零开始学习Strust2_03)

开发环境:

Eclipse IDE for Java EE Developers(下载地址

struts-2.3.1.2(下载地址

apache-tomcat-6.0.35(下载地址


结果图:



web.xml跟HelloWorld相似,将welcome-list的内容改为了register.jsp

<?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_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>register.jsp</welcome-file>
	</welcome-file-list>
</web-app>

然后实现一个用户注册信息的JavaBean

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();
	}
}

在Action的实现类RegisterAction.java中使用这个JavaBean,并实现Validation函数。

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;
	}

	public void validate() {

		if (userRegInfoBean.getAccount().length() == 0) {

			addFieldError("userRegInfoBean.account", "Account is required.");

		}

		if (userRegInfoBean.getEmail().length() == 0) {

			addFieldError("userRegInfoBean.email", "Email is required.");

		}

		if (userRegInfoBean.getRealName().length() == 0) {

			addFieldError("userRegInfoBean.realName", "Real Name is required.");

		}

		if (!userRegInfoBean.getPassword().equals(
				userRegInfoBean.getPwdValidate())) {
			addFieldError("userRegInfoBean.pwdValidate","Password input is inconsistent");
		}

	}

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

struts.xml中添加了一个<result name="input">/register.jsp</result>,当验证出现不合法的情况,就会转到这个result

<?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>
	</package>
</struts>

因为使用了JavaBean,所以在Jsp中使用XXX.xxx的方式

<%@ 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 name="userRegInfoBean.account" label="Account"></s:textfield>
		<s:password name="userRegInfoBean.password" label="Password"></s:password>
		<s:password name="userRegInfoBean.pwdValidate"
			label="Input Password Again"></s:password>
		<s:textfield name="userRegInfoBean.realName" label="Real Name"></s:textfield>
		<s:textfield name="userRegInfoBean.email" label="E-Mail"></s:textfield>
		<s:textfield name="userRegInfoBean.msn" label="MSN"></s:textfield>
		<s:textfield name="userRegInfoBean.qq" label="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>







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值