1.重写validate()方法的数据校验
(1)新建web project.将基本的struts2环境配置好包括web.xml,struts.xml以及要导入的jar包。
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>interceptor</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
(2)新建登录页面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>My JSP 'login.jsp' starting page</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">
-->
</head>
<body>
<s:fielderror></s:fielderror>
<s:form name="form1" action="login.action" method="post">
<s:textfield name="username" label="用户名"/>
<s:textfield name="password" label="密码"/>
<s:submit value="提交"/>
</s:form>
</body>
</html>
表单标签用于提交数据,<s:fielderror>标签显示Action的addFiledError方法封装的错误信息。
(3)src目录下新建com.tyf.action包,创建LoginAction类
package com.tyf.action;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String username;
private String password;
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;
}
public String execute() throws Exception{
return SUCCESS;
}
public void validate(){
if(getUsername()==null||"".equals(getUsername().trim()))
this.addFieldError("usernameMsg", "用户名不能为空!");
else {//使用正则表达式
Pattern p=Pattern.compile("\\w{6,20}");
Matcher m=p.matcher(getUsername().trim());
if(!m.matches()){
this.addFieldError("passwordMsg", "用户名有数字,下划线,字母构成,长度为6-20");
}
}
if(this.getPassword().trim().length()==0)
this.addFieldError("passwordMsg", "密码不能为空");
else{
int s=getPassword().trim().length();
if(s<6||s>20)
this.addFieldError("passwordMsg", "密码长度在6-20之间!");
}
}
}
(4)配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="validate" extends="struts-default" namespace="/">
<action name="login" class="com.tyf.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
(5)success.jsp
欢迎:<s:property value="username"/>,登录成功
(6)部署运行就可以啦