struts_20_对Action中所有方法、某一个方法进行输入校验(基于XML配置方式实现输入校验)
第01步:导包

第02步:配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- 第00步:启动Struts框架 -->
<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> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第03步:编写bean
package cn.itcast.bean; /**
* 第01步:
* ******编写bean
* 下一步:package cn.itcast.action.PersonAction;
*/
public class Person {
private String name;
private String mobile; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
第04步:编写action
package cn.itcast.action; import cn.itcast.bean.Person; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 第02步:
* 编写action
*/
public class PersonAction extends ActionSupport{
private Person person; /**第03步:编写action方法update()、save(),下一步:struts.xml**/
/**3.1**/
public String update(){
System.out.println("执行update!");
ActionContext.getContext().put("message", "更新成功");
return "message";
}
/**3.2**/
public String save(){
System.out.println("执行save!");
ActionContext.getContext().put("message", "保存成功");
return "message";
}
/**3.3**/
public String other(){
System.out.println("执行other!");
ActionContext.getContext().put("message", "other");
return "message";
} /**set()、get()方法*/
public Person getPerson() {
return person;
} public void setPerson(Person person) {
this.person = person;
} }
第05步:编写校验器xml配置文件:
名字如果是:PersonAction-validation.xml:对所有方法进行验证(类名+"-validation.xml")
名字如果是:PersonAction-manage_*-validation.xml:对manage_*所指定的方法进行校验,manage_*是struts配置的
名字如果是:PersonAction-manage_save-validation.xml:对save方法进行校验
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.3//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">
<validators>
<!-- field指定action中需要校验的属性 -->
<field name="person.name">
<!--
field-validator指定校验器,requiredstring是系统提供的校验器(校验不能为空),
可以在com.opensymphony.xwork2.validator.validations下的default.xml中找到所有校验器,也可以扩展加入自己的校验器
-->
<field-validator type="requiredstring">
<!-- 反射注入方式:实现去掉字符串前后的空字符-->
<param name="trim">true</param>
<!-- 提示的错误信息 -->
<message>用户名不能为空!</message>
</field-validator>
</field>
<field name="person.mobile">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>手机号不能为空!</message>
</field-validator>
<!-- regex:正则表达式校验器,CDATA:xml的格式化,表明里面类容为字符串,不是xml元素 -->
<field-validator type="regex">
<param name="expression"><![CDATA[^1[358]\d{9}$]]></param>
<message>手机号格式不正确!</message>
</field-validator>
</field>
</validators>
第06步:配置strut.xml文件
<?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>
<package name="person" namespace="/person" extends="struts-default">
<action name="manage_*" class="cn.itcast.action.PersonAction" method="{1}">
<!-- 指定input视图 -->
<result name="input">/index.jsp</result>
<result name="message">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>
第07步:编写界面
index.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>输入校验</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head> <body>
save方法校验
<!-- s:fielderror显示失败信息 -->
<s:fielderror/>
<form action="person/manage_save.action" method="post">
用户名:<input type="text" name="person.name"/>不能为空<br/>
手机号:<input type="text" name="person.mobile"/>不能为空,并且要符合手机号的格式1,3/5/8,后面是9个数字<br/>
<input type="submit" value="提 交"/></form> update方法校验
<s:fielderror/>
<form action="person/manage_update.action" method="post">
用户名:<input type="text" name="person.name"/>不能为空<br/>
手机号:<input type="text" name="person.mobile"/>不能为空,并且要符合手机号的格式1,3/5/8,后面是9个数字<br/>
<input type="submit" value="提 交"/></form> 所有方法校验
<s:fielderror/>
<form action="person/manage_other.action" method="post">
用户名:<input type="text" name="person.name"/>不能为空<br/>
手机号:<input type="text" name="person.mobile"/>不能为空,并且要符合手机号的格式1,3/5/8,后面是9个数字<br/>
<input type="submit" value="提 交"/></form>
</body>
</html>
message.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>结果</title>
</head> <body>
${message }
</body>
</html>
注意、需求:
对所有方法进行校验
1、基于XML配置方式实现action方法的校验 需求:
用户名:不能为空
手机号:不能为空,并且要符合手机号的格式1,3/5/8,后面是9个数字 注意:
需继承ActionSupport类,
需要在struts.xml中指定input视图,
校验文件放在action类同一包下,
文件格式为ActionClassName-validation.xml(类名+"-validation.xml")
名字如果是:PersonAction-validation.xml:对所有方法进行验证
名字如果是:PersonAction-manage_*-validation.xml:对manage_*所指定的方法进行校验,manage_*是struts配置的
名字如果是:PersonAction-manage_save-validation.xml:对save方法进行校验
struts_20_对Action中所有方法、某一个方法进行输入校验(基于XML配置方式实现输入校验)的更多相关文章
- struts2视频学习笔记 22-23(基于XML配置方式实现对action的所有方法及部分方法进行校验)
课时22 基于XML配置方式实现对action的所有方法进行校验 使用基于XML配置方式实现输入校验时,Action也需要继承ActionSupport,并且提供校验文件,校验文件和action类 ...
- 转载 - Struts2基于XML配置方式实现对action的所有方法进行输入校验
出处:http://www.cnblogs.com/Laupaul/archive/2012/03/15/2398360.html http://www.blogjava.net/focusJ/arc ...
- Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析
Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML ...
- Struts2基于XML配置方式实现对Action方法进行校验
JavaWeb框架(2) 使用XML对Action方法进行校验方式有两种,一种是对Action的所有方法进行校验,另一种是对Action指定方法进行校验. 对Action的所有方法进行校验: 步骤: ...
- 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的全部方法进行输入校验
在struts2中,我们能够实现对action的全部方法进行校验或者对action的指定方法进行校验. 对于输入校验struts2提供了两种实现方法: 1. 採用手工编写代码实现. 2. 基于XML配 ...
- action中redirectAction到另一个命名空间中的action该如何配置
action中redirectAction到另一个命名空间中的action该如何配置,请注意namespace这儿必须是/global,而不是global,要不然找不到此action的
- 实例化Bean的方法(基于xml配置)-http://blog.csdn.net/shymi1991/article/details/48153293
实例化Bean的方法(基于xml配置) 标签: spring framework 2015-09-01 13:43 918人阅读 评论(0) 收藏 举报 分类: Spring FrameWork(7 ...
- struts2对action中的方法进行输入校验---xml配置方式(3)
上面两篇文章已经介绍了通过编码java代码的方式实现action方法校验,这里我们介绍第二种方式:xml配置文件 首先我们来看一个样例: ValidateAction.java: package co ...
- Java面试 - 在Java中, 既然构造方法是一个方法,那么为什么不使用void 定义呢?
Java程序编译器是根据代码结构来进行编译处理的,执行的时候也是根据代码结构来处理的. 如果在构造方法上使用void,那么此结构就会与普通方法的结构相同,这样编译器会认为此方法是一个 普通方法,而普通 ...
随机推荐
- JS之call/apply/bind
测试代码: var a = 1; var obj = { a = 2; } function test(a){ alert(a); alert(this.a); } 1.test(3); 结果:3,1 ...
- [转]android自动弹出软键盘(输入键盘)
转自:http://www.devdiv.com/home.php?mod=space&uid=65729&do=blog&id=11847 很多应用中对于一个界面比如进入搜索 ...
- linux命令之tee
功能说明:读取标准输入的数据,并将其内容输出成文件.语 法:tee [-ai][--help][--version][文件...]补充说明:tee指令会从标准输入设备读取数据,将其内容输出到标准输出设 ...
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
- Insert BLOB && CLOB from PL/SQL and JDBC
For PL/SQL 1)Create Directory Where BLOB resides. create or replace directory temp as '/oradata2'; - ...
- nohup和&的区别
nohup和&的区别http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4241330&fromuid=21288388 ...
- Android各个版本 版本号对应关系表
Platform Version API Level VERSION_CODE Notes Android 5.0 21 LOLLIPOP Platform Highlights Android 4. ...
- 【Netty学习】 ChannelHandler 改动及影响
channelHandler 在Netty 4.x版本有了相当大的改动 http://netty.io/wiki/new-and-noteworthy.html 官网的更新改进说明. 以下节选官网 ...
- 发起post请求
string postUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo"; //string req ...
- 高级选择器querySelector和querySelectorAll
Javascript新提供的querySelector和querySelectorAll方法,是仿照CSS选择器功能编写的 querySelector 功能:该方法返回满足条件的单个元素.按照深度优先 ...