struts2数据校验(重写validate方法)

【例】用户登录添加数据校验

第一步:创建loginAction

package com.dwx.action;
import java.util.regex.Pattern;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class loginAction extends ActionSupport {
	private String username;
	private String password;
	@Override
	public String execute() throws Exception {
		if(username.equals("admin")){
			if(password.equals("123456")){				
				ActionContext.getContext().getSession().put("username",username);
				return SUCCESS;
			}else{
				this.addFieldError("password", "密码错误!");
				return INPUT;
			}
		}else{
			this.addFieldError("username", "用户名错误!");
			return INPUT;
		}
	}
	@Override
	public void validate() {
		if(username==null||"".equals(username.trim())){
			this.addFieldError("username", "用户名不能为空!");
		}
		if(!Pattern.compile("^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$").matcher(username.trim()).matches()){
			this.addFieldError("username", "用户名长度必须在5-20之间,且以字母开头,可带数字、_和.的字符串!");
		}
		if(password==null||"".equals(password.trim())){
			this.addFieldError("password", "密码不能为空!");
		}
		if(password.length()<6||password.length()>20){
			this.addFieldError("username", "密码长度必须在6-20之间!");
		}
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

第二步:配置struts.xml

<?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.custom.i18n.resources" value="message"/>
	<constant name="struts.i18n.encoding" value="GBK"/>
	<package name="user" extends="struts-default" namespace="/">
		<action name="login" class="com.dwx.action.loginAction">
			<result>/index.jsp</result>
			<result name="input">/login.jsp</result>
		</action>
	</package>
</struts>

第三步:创建login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用户登录</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<LINK href="css/admin.css" type="text/css" rel="stylesheet">
</head>
<body>
	<TABLE height="100%" cellSpacing=0 cellPadding=0 width="100%"
		bgColor=#002779 border=0>
		<TR>
			<TD align=middle>
				<TABLE cellSpacing=0 cellPadding=0 width=468 border=0>
					<TR>
						<TD><IMG height=23 src="images/login_1.jpg" width=468></TD>
					</TR>
					<TR>
						<TD><IMG height=147 src="images/login_2.jpg" width=468></TD>
					</TR>
				</TABLE>
				<TABLE cellSpacing=0 cellPadding=0 width=468 bgColor=#ffffff
					border=0>
					<TR>
						<TD width=16><IMG height=122 src="images/login_3.jpg"
							width=16></TD>
						<TD align=middle>
							<TABLE cellSpacing=0 cellPadding=0 width=230 border=0>
								<FORM action="login" method="post">
									<TR height=5>
										<TD width=5></TD>
										<TD width=56></TD>
										<TD></TD>
									</TR>
									<TR height=36>
										<TD></TD>
										<TD>用户名</TD>
										<TD><INPUT
											style="BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #000000 1px solid"
											maxLength=20 size=20 name="username"></TD>
									</TR>
									<TR height=36>
										<TD> </TD>
										<TD>密 码</TD>
										<TD><INPUT
											style="BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #000000 1px solid"
											type=password maxLength=20 size=20 name="password"></TD>
									</TR>
									<TR height=5>
										<TD colSpan=3></TD>
									</TR>
									<TR>
										<TD> </TD>
										<TD> </TD>
										<TD><INPUT type=image height=18 width=70
											src="images/bt_login.gif"></TD>
									</TR>
								</FORM>
							</TABLE>
						</TD>
						<TD width=16><IMG height=122 src="images/login_4.jpg"
							width=16></TD>
					</TR>
				</TABLE>
				<TABLE cellSpacing=0 cellPadding=0 width=468 border=0>
					<TR>
						<TD><IMG height=16 src="images/login_5.jpg" width=468></TD>
					</TR>
				</TABLE>
				<table cellpadding="0" cellspacing="0" border="0">
					<tr>
						<td style="font-size: 12px;color: red;"><s:fielderror /></td>
					</tr>
				</table>
			</TD>
		</TR>
	</TABLE>
</body>
</html>

效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云淡风轻58

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值