struts2对action中的方法进行输入校验(2)
struts2输入校验流程:
1.类型转换器对请求參数运行类型转换,并把转换后的值赋给aciton中的属性
2.假设在运行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext,
conversionError拦截器将异常信息加入到fieldErrors里,无论类型转换是否出现异常,都会进入第三步
3.系统通过反射技术先调用action的validateXXX方法
4.再调用aciton中的validate方法
5.经过上述的4步。假设系统中的fieldErrors存在错误信息,系统自己主动将请求转发至名称为input的视图
假设系统中的fieldErrors没有不论什么错误信息,系统将运行aciton中的处理方法。
也就是说转发至input视图有两个原因:1.类型转换异常
2.输入校验不合法
例如以下:
InvidateAction.java:
package com.itheima.action;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class InvidateAction extends ActionSupport{
private String username;
private String tel;
private Date birthday;
private String msg;
public void setUsername(String username) {
this.username = username;
}
public void setTel(String tel) {
this.tel = tel;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getMsg() {
return msg;
}
public void validate() {
}
public String execute1() {
msg = "execute1";
return "success";
}
public String execute2() {
msg = "execute2";
return "success";
}
}
person.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<s:fielderror/>
<form action="${pageContext.request.contextPath }/invidateAction_execute1.action" method="post">
用户名:<input type="text" name="username"><br>
手机号:<input type="text" name="tel"><br>
生日:<input type="text" name="birthday"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
struts2.xml
<action name="invidateAction_*" class="com.itheima.action.InvidateAction" method="{1}">
<result name="success">/success.jsp</result>
<result name="input">/person.jsp</result>
</action>
我在jsp中输入信息例如以下:
则会提示:
======================================================================================================
解决以上问题能够通过创建Action的类型转换器:
类型转换器教程參见:http://blog.csdn.net/m631521383/article/details/40680723
InvidateAction.java:
package com.itheima.action;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class InvidateAction extends ActionSupport{
private String username;
private String tel;
private Date birthday;
private String msg;
public void setUsername(String username) {
this.username = username;
}
public void setTel(String tel) {
this.tel = tel;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getMsg() {
return msg;
}
public void validate() {
}
public String execute1() {
msg = "execute1";
return "success";
}
public String execute2() {
msg = "execute2";
return "success";
}
}
DateTypeConverter.java:
package com.itheima.converter; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter; public class DateTypeConverter extends DefaultTypeConverter{ @Override
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmdd");
if(toType == Date.class) {
String[] strs = (String[])value;
Date date = null;
try {
date = dateFormat.parse(strs[0]);
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return date;
} else if(toType == String.class) {
return dateFormat.format((Date)value);
} return null;
} }
InvidateAction-conversion.properties:
birthday=com.itheima.converter.DateTypeConverter
项目树:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbTYzMTUyMTM4Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
struts2对action中的方法进行输入校验(2)的更多相关文章
- struts2对action中的方法进行输入校验---xml配置方式(3)
上面两篇文章已经介绍了通过编码java代码的方式实现action方法校验,这里我们介绍第二种方式:xml配置文件 首先我们来看一个样例: ValidateAction.java: package co ...
- Struts2 对Action中所有方法进行输入校验、单个方法进行校验
index.jsp: <body> <s:fielderror /> <form action="${pageContext.request.contextPa ...
- 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的全部方法进行输入校验
在struts2中,我们能够实现对action的全部方法进行校验或者对action的指定方法进行校验. 对于输入校验struts2提供了两种实现方法: 1. 採用手工编写代码实现. 2. 基于XML配 ...
- 转载 - Struts2基于XML配置方式实现对action的所有方法进行输入校验
出处:http://www.cnblogs.com/Laupaul/archive/2012/03/15/2398360.html http://www.blogjava.net/focusJ/arc ...
- 第三章Struts2 Action中动态方法调用、通配符的使用
01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...
- struts_19_对Action中所有方法、某一个方法进行输入校验(手工编写代码实现输入校验)
对所有方法进行校验1.通过手工编写代码的形式实现 需求:用户名:不能为空手机号:不能为空,并且要符合手机号的格式1,3/5/8,后面是9个数字 第01步:导包 第02步:配置web.xml <? ...
- Struts2 Action中动态方法调用、通配符的使用
一.Struts2执行过程图: 二.struts2配置文件的加载顺序 struts-default.xml---struts-plugin.xml---struts.xml 具体步骤: 三.Actio ...
- Struts2 Action中的方法命名不要以get开头
偶然发现,在调用一个action中的某个方法时,会自动调用另一个无关的方法,找了好久,最后发现是方法命名的问题,方法命名以get开头,action会自动调用!所以,以后再写action中的方法时尽量不 ...
- 使用validateXxx()方法进行输入校验 --Struts2框架
1.本例是在使用validate()方法进行输入校验 --Struts2框架的基础上接着做的,上一篇使用validate()方法进行输入校验时会对当前Action中的所有方法有效,由于Struts2框 ...
随机推荐
- javax.servlet.http.HttpServletRequest;
错误提示是没有引入javax.servlet.http.HttpServletRequest所在的包,编译错误. 这么添加: 项目-->右键-->properties-->java ...
- php连接oracle及简单操作
使你的php支持oracle,按照以下步骤即可: 1.安装php环境,找一下appserv或者xampp,一键安装,很方便 2.把php的ext目录下的php_oci8.dll拷到system32目录 ...
- 用lisp来让计算机学会写作
大部分的代码.思路参考了<Ansi Common Lisp>P138~P141. 问题:给一篇英文文本,如何让计算机依据此文本而生成随机但可读的文本.如: |Venture| The Na ...
- [ACM] n划分数m部分,它要求每一个部分,并采取了最大的产品(间隔DP)
A - 爱管闲事 春希很爱管闲事,他每天都会抽出时间帮助一些同学,因为春希很死板,出于公平性,春希不会先帮助后来找他的同学. 如今有n个同学须要他的帮助,尽管他非常想一天之类帮助全部人,但毕竟精力有限 ...
- Ubuntu环境下SSH的安装及使用
Ubuntu环境下SSH的安装及使用 SSH是指Secure Shell,是一种安全的传输协议,Ubuntu客户端可以通过SSH访问远程服务器 .SSH的简介和工作机制可参看上篇文章SSH简介及工作机 ...
- 基于visual Studio2013解决面试题之0604O(1)时间复杂度删除链表节点
题目
- cocos2d-x 精灵移动
在HelloWorldScene.h中声明 class HelloWorld : public cocos2d::CCLayer { public : ...... CCPoin ...
- Effective C++_笔记_条款07_为多态基类声明virtual析构函数
(整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 这个规则只适用于polymorphic(带多态性质的)base ...
- Linear Regression(线性回归)(二)—正规方程(normal equations)
(整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 在上篇博客中,我们提出了线性回归的概念,给出了一种使代价函数最小的 ...
- Android中 Bitmap Drawable Paint的获取、转换以及使用
比如Drawable中有一系列连续的图片,img_0.png, img_1.png, img_2.png ... 如果要动态获取这些图片,通过"R.drawable.img_x"的 ...