struts_19_对Action中所有方法、某一个方法进行输入校验(手工编写代码实现输入校验)
对所有方法进行校验
1、通过手工编写代码的形式实现
需求:
用户名:不能为空
手机号:不能为空,并且要符合手机号的格式1,3/5/8,后面是9个数字
第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
*/
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 java.util.regex.Pattern; import cn.itcast.bean.Person; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 第02步:
* 编写action
*/
public class PersonAction extends ActionSupport{//需要继承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";
} /**第05步:PersonAction继承ActionSupport,并编写校验方法;下一步:编写界面**/
/**5.1**/
public void validateSave() {//validate+Save:会对save()方法校验
System.out.println("对save()方法进行校验");
if(this.person.getName()==null || "".equals(this.person.getName().trim())){
this.addFieldError("username", "用户名不能为空");
}
if(this.person.getMobile()==null || "".equals(this.person.getMobile().trim())){
this.addFieldError("mobile", "手机号不能为空");
}else{
//调用校验Pattern类方法:不能为空,首个数字:1(^1),第二个数字:3、5、8([358],以任意9个数字:(\\d{9}),结尾:($))1,3/5/8,后面是9个数字
if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.person.getMobile()).matches()){
this.addFieldError("mobile", "手机号格式不正确");
}
}
}
/**5.2**/
public void validateUpdate() {//validate+Update:会对update()方法校验
System.out.println("对update()方法进行校验");
if(this.person.getName()==null || "".equals(this.person.getName().trim())){
this.addFieldError("username", "用户名不能为空");
}
if(this.person.getMobile()==null || "".equals(this.person.getMobile().trim())){
this.addFieldError("mobile", "手机号不能为空");
}else{
if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.person.getMobile()).matches()){
this.addFieldError("mobile", "手机号格式不正确");
}
}
}
/**5.3**/
public void validate() {//validate:会对所有action方法校验
System.out.println("对所有action方法进行校验");
if(this.person.getName()==null || "".equals(this.person.getName().trim())){
this.addFieldError("username", "用户名不能为空");
}
if(this.person.getMobile()==null || "".equals(this.person.getMobile().trim())){
this.addFieldError("mobile", "手机号不能为空");
}else{
if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.person.getMobile()).matches()){
this.addFieldError("mobile", "手机号格式不正确");
}
}
} /**set()、get()方法*/
public Person getPerson() {
return person;
} public void setPerson(Person person) {
this.person = person;
} }
第05步:配置struts.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>
第06步:编写界面
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、浏览器提交请求
2、Struts2调用默认类型转换器,转换表单提交的值,将值付给action的属性:person
3、如果在转换类型的过程中出现异常,系统将异常保存到ActionContext。conversionError拦截器将异常信息封装到fieldErrors里。不管类型转换时否出现异常,都会进入第4步
4、通过反射技术,先调用action中的validateXxx()方法校验单个action方法(Xxx为action方法名,首字母大写)。如果出错,系统将错误信息保存到fieldErrors里
5、然后调用action中的方法validate()方法校验所有action方法。如果出错,系统将错误信息保存到fieldErrors里
6、如果出错(即fieldErrors存在信息,即存放的size大于0),请求自动转发到input视图(input视图会显示错误信息,和原提交界面信息)。如果没有错,系统执行action方法
注意:
注意:
1、如果校验方法没错,任然转发到input视图,那么可能是转换器,转换时出错。因为转换错误信息会保存到fieldErrors里,校验错误信息也会保存到fieldErrors里面。比如添加一个Date(日期)类型,那么就会转换类型出错,转到input视图。
2、需要在struts中指定input出错时显示的视图
struts_19_对Action中所有方法、某一个方法进行输入校验(手工编写代码实现输入校验)的更多相关文章
- action中redirectAction到另一个命名空间中的action该如何配置
action中redirectAction到另一个命名空间中的action该如何配置,请注意namespace这儿必须是/global,而不是global,要不然找不到此action的
- struts2视频学习笔记 19-20(手工编写代码实现所有方法和指定方法校验)
课时19 对Action中所有方法进行输入校验 1.手工编写代码实现对action中所有方法输入校验 通过重写validate() 方法实现, validate()方法会校验action中所有与exe ...
- Java面试 - 在Java中, 既然构造方法是一个方法,那么为什么不使用void 定义呢?
Java程序编译器是根据代码结构来进行编译处理的,执行的时候也是根据代码结构来处理的. 如果在构造方法上使用void,那么此结构就会与普通方法的结构相同,这样编译器会认为此方法是一个 普通方法,而普通 ...
- Action中获取servletAPI对象的方法
1.ServletActionContext:可以从中获取当前Action对象需要的一切ServletAPI的相关对象: 常用的方法: 1.获取HttpServletRequest:ServletAc ...
- SpringBoot项目中如何异步执行一个方法
1. SpringBoot上加上开启异步方法注解:@EnableAsync 2. 在需要异步执行的方法上,加上异步方法注解 @Async 3. 测试 5. 测试结果为,访问127.0.0.1:8888 ...
- 11SpringMvc_一个Action中,写多个类似的业务控制方法
我们要实现这么一个功能: 编写两个表单,提交到同一个Action中的不同的处理方法中.比如注册和登录,都提交到UserAction这个控制类中.但是这两个提交由userAction这个控制类不同的方法 ...
- struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...
- 理解Struts2的Action中的setter方法是怎么工作的
接触过webwork和Struts2的同行都应该知道, 提交表单的时候,只要Action中的属性有setter 方法,这些表单数据就可以正确赋值到Action中属性里:另外对于Spring配置文件中声 ...
- struts2的占位符*在action中的配置方法
转自:https://blog.csdn.net/u012546338/article/details/68946633 在配置<action> 时,可以在 name,class,meth ...
随机推荐
- Mongo中的数据类型
一.null null用于表示空值或者不存在的字段 {"X" : null} 二.布尔型 布尔类型有两个值true和false {"x" : true} 三.数 ...
- 弄清const与指针、引用之间的关系
const和 define在常量定义上的差别 在C++中,我们可以使用const 或者 宏define来定义常量.但是C++鼓励使用const定义常量,而不是宏define.原因有很多. 1.defi ...
- android监听屏幕打开关闭广播无响应的情况
android在屏幕打开和关闭的时候会发出广播,但是如果receiver配置在AndroidManifest.xml中时,receiver是接受不到任何广播的. <receiver androi ...
- asp.net mvc页面javascript代码中如何使用razor
我们需要用<text>将javascript代码包含起来,强制让razor编译器回到内容模式, 或者将javascript代码放在函数中,让razor编译器可以识别,请看下面两个例子: & ...
- Java学习-026-类名或方法名应用之二 -- 统计分析基础
前文讲述了类名或方法的应用之一调试源码,具体请参阅:Java学习-025-类名或方法名应用之一 -- 调试源码 此文主要讲述类名或方法应用之二统计分析,通过在各个方法中插桩(调用桩方法),获取方法的调 ...
- Selenium2学习-033-WebUI自动化实战实例-031-页面快照截图应用之二 -- 区域截图
我在之前的文章中曾给出浏览器显示区域截图的方法,具体请参阅 .或许,有些小主已经想到了,每次都获取整个显示区域的截图存储,那么经过一段时间后,所使用的图片服务器的容量将会受到极大的挑战,尤其是在产品需 ...
- Selenium2学习-031-WebUI自动化实战实例-029-JavaScript 在 Selenium 自动化中的应用实例之四(获取元素位置和大小)
通过 JS 或 JQuery 获取到元素后,通过 offsetLeft.offsetTop.offsetWidth.offsetHeight 即可获得元素的位置和大小,非常的简单,直接上源码了,敬请参 ...
- imx6 kernel clock
前段时间查看了uboot的时钟,kernel的也稍微了解了下,记录于此,以后再来补充完善. board-mx6q_sabresd.c MACHINE_START(MX6Q_SABRESD, " ...
- angularJs:双向数据绑定
示例1 <!DOCTYPE html> <html ng-app> <head> <meta charset="UTF-8" /> ...
- 智能硬件+App移动新生态【10.24北京站】
活动概况 时间:2015年10月24日13:30-16:30 地点:Wepac空间(海淀区北四环西路68号左岸工社6层) 主办:APICloud.机智云.智石科技.华为云 网址:www.apiclou ...