第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配置方式实现输入校验)的更多相关文章

  1. struts2视频学习笔记 22-23(基于XML配置方式实现对action的所有方法及部分方法进行校验)

    课时22 基于XML配置方式实现对action的所有方法进行校验   使用基于XML配置方式实现输入校验时,Action也需要继承ActionSupport,并且提供校验文件,校验文件和action类 ...

  2. 转载 - Struts2基于XML配置方式实现对action的所有方法进行输入校验

    出处:http://www.cnblogs.com/Laupaul/archive/2012/03/15/2398360.html http://www.blogjava.net/focusJ/arc ...

  3. Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析

    Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML ...

  4. Struts2基于XML配置方式实现对Action方法进行校验

    JavaWeb框架(2)  使用XML对Action方法进行校验方式有两种,一种是对Action的所有方法进行校验,另一种是对Action指定方法进行校验. 对Action的所有方法进行校验: 步骤: ...

  5. 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的全部方法进行输入校验

    在struts2中,我们能够实现对action的全部方法进行校验或者对action的指定方法进行校验. 对于输入校验struts2提供了两种实现方法: 1. 採用手工编写代码实现. 2. 基于XML配 ...

  6. action中redirectAction到另一个命名空间中的action该如何配置

    action中redirectAction到另一个命名空间中的action该如何配置,请注意namespace这儿必须是/global,而不是global,要不然找不到此action的

  7. 实例化Bean的方法(基于xml配置)-http://blog.csdn.net/shymi1991/article/details/48153293

    实例化Bean的方法(基于xml配置) 标签: spring framework 2015-09-01 13:43 918人阅读 评论(0) 收藏 举报  分类: Spring FrameWork(7 ...

  8. struts2对action中的方法进行输入校验---xml配置方式(3)

    上面两篇文章已经介绍了通过编码java代码的方式实现action方法校验,这里我们介绍第二种方式:xml配置文件 首先我们来看一个样例: ValidateAction.java: package co ...

  9. Java面试 - 在Java中, 既然构造方法是一个方法,那么为什么不使用void 定义呢?

    Java程序编译器是根据代码结构来进行编译处理的,执行的时候也是根据代码结构来处理的. 如果在构造方法上使用void,那么此结构就会与普通方法的结构相同,这样编译器会认为此方法是一个 普通方法,而普通 ...

随机推荐

  1. Bluetooth HCI介绍

    目录 1. HCI功能 2. HCI Packet 1. HCI Command 2. HCI Event 3. HCI Data 3. HCI传输层 HCI, 主机控制接口(Host Control ...

  2. linux OSlab4 添加自定义系统调用

    http://blog.csdn.net/ly01kongjian/article/details/8947285 http://www.cnblogs.com/hoys/archive/2011/0 ...

  3. 获取设备唯一标识 uuid(采用第三方库SSKeychain)

    SSKeyChain 下载链接: http://pan.baidu.com/s/1booV3VD 密码: ivdi /** *  获取设备唯一标识 uuid */ +(NSString*) uuid ...

  4. rabbitMq 转自 http://gaoyangang.iteye.com/blog/1566600

    rabbitMq  转自 http://gaoyangang.iteye.com/blog/1566600

  5. Requirements Gathering

    Requirements gathering is an essential part of any project and project management. Understanding ful ...

  6. JS事件分析

    1.注册事件 1.1 使用HTML元素的事件属性 <div id='myDiv' style="width:100px;height:100px;background-color:re ...

  7. asp.net 把数据导出为excel

    本篇介绍c#中如何使用DataTable导出Excel,至于其他的导出方法,这里不作介绍! 1.首页从数据库读取数据,得到DataTable: DataTable dt = HelperExecute ...

  8. zero1--hibernate注解02

  9. 一个例子深入理解ClassLoader

    文件类加载器,该加载器重载了loadClass方法,逻辑是只读取文件来加载类,不委托给父类加载器进行加载 package com.ydd.study.hello.classloader; import ...

  10. Jboss 安全和优化

    一.        Jboss后台启动:添加后台修改命令:vi run.shwhile true; do   if [ "x$LAUNCH_JBOSS_IN_BACKGROUND" ...