Struts 2 数据验证的两种方式——手动验证和xml验证
2018-09-20 本文已影响0人
周大侠_2409
1. 手动验证——通过编写action进行验证
- 建立表单 login_1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<body>
<form action="dataAction.action" method="post">
name:<input name="name" type="text"><br>
age:<input name="age" type="text"><br>
<input type="submit" value="登陆">
</form>
</body>
</html>
- 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>
<!--
数据验证,手动验证
-->
<package name="checke" namespace="/" extends="struts-default">
<action name="dataAction" class="actions.dataAction" method="execute">
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
- src下建立actions包,在里面编写data.Action.java
- addFieldError()方法执行后到
<result name="input">/index.jsp</result>
,跳转到index对应的页面。 - 若action中有多个方法
validate()
验证对所有方法都有效 - 若想只对某方法进行验证,如
Login()
修改validate()→validatelogin()
- addFieldError()方法执行后到
package actions;
import java.util.regex.Pattern;
import com.opensymphony.xwork2.ActionSupport;
public class dataAction extends ActionSupport {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public void validate(){
if (name == null || name.equals("")){
addFieldError("name","姓名不能为空:"+name);
}
if (age == null || name.equals("")){
addFieldError("age","年龄不能为空:"+age);
}else if(!isInteger(age)){
addFieldError("age","年龄输入不合理:"+age);
}else if(Integer.parseInt(age)>100 && Integer.parseInt(age)<1){
addFieldError("age","请输入年龄在0-100之间:"+age);
}
System.out.println("name:"+name+";age:"+age);
}
//判断字符串是否为整数
public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
public String execute(){
return "success";
}
}
- 编写index.jsp页面
- 在jsp页面中使用struts标签时需要加入如下语句:
<%@ taglib uri="/struts-tags" prefix="s" %>
<s:fielderror>标签:
如:addFieldError("name","姓名不能为空")执行后,
这段语句将获取<s:fielderror> <s:param>name</s:param> </s:fielderror>
name
对应的值姓名不能为空
- 在jsp页面中使用struts标签时需要加入如下语句:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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 'index.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:param>name</s:param>
</s:fielderror>
<s:fielderror>
<s:param>age</s:param>
</s:fielderror>
</body>
</html>
- 编写success.jsp页面:
-
<s:property>
标签:name:<s:property value="name"/><br> age:<s:property value="age"/>
- 通过
<s:property>
标签返回name
和age
的值。
-
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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 'success.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>
name:<s:property value="name"/><br>
age:<s:property value="age"/>
</body>
</html>
2. XML验证
- struts提供了一套标准的表单验证机制,通过配置文件可以调用系统的16个表单验证器进行表单验证
- 约束信息
xwork-validator-1.0.3.dtd - 16个表单验证器在哪里?
xwork-core-2.3.24.1.jar
→com.opensymphony.xwork2.validator.validators
→default.xml
- 约束信息
- 新建一个登陆界面login_2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<body>
<form action="dataAction1.action" method="post">
name:<input name="name" type="text"><br>
age:<input name="age" type="text"><br>
<input type="submit" value="登陆">
</form>
</body>
</html>
- 编写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>
<!--
数据验证,xml验证
-->
<package name="checke" namespace="/" extends="struts-default">
<action name="dataAction1" class="actions.dataAction1" method="execute">
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
- 在src/actions下编写java类
package actions;
import com.opensymphony.xwork2.ActionSupport;
public class dataAction1 extends ActionSupport {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String execute(){
return "success";
}
}
- 在src/actions下建立xml格式文件。
- 命名规则:
- 数据验证用户是整个Action对象:
ActionName-validation.xml
- 只是对Action对象中的特定方法进行验证:
ActionName-AliasName-validation.xml
ActionName
为Action类名
AliasName
为Action的name属性的名称。
例如:有struts文件如下:
- 数据验证用户是整个Action对象:
- 命名规则:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="checke" namespace="/" extends="struts-default">
<action name="ActionloginUser" class="actions.dataAction" method="User">
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
</action>
<action name="Actionloginadmin" class="actions.dataAction" method="admin">
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
- 想要验证整个Action对象,xml为:
dataAction-validation.xml
- 只是验证action里面的admin方法,xml为:
dataAction-Actionloginadmin-validation.xml
- 编写dataAction1-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="name">
<field-validator type="requiredstring">
<message>
姓名不能为空
</message>
</field-validator>
</field>
<field name="age">
<field-validator type="requiredstring">
<message>
年龄不能为空
</message>
</field-validator>
</field>
</validators>
- 编写结果界面,
sucess.jsp
index.jsp
参考手动验证的编写。