【例】用户登录添加数据校验
第一步:创建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>
效果: