Struts2之自定义类型转换器
Struts2自定义类型转换器分为局部类型转换器和全局类型转换器
(1)局部类型转换器
如果页面传来一个参数reg.action?birthday=2010-11-12到后台action,然后属性用date类型是可以接收到的,但是如果传的是20101112这样类型的字符串,用date类型是获取不到,并且会出现错误的,struts2提供了一种类型转换器供我们使用。
以下为局部类型转换器的开发步骤
a.首先要写一个类来继承DefaultTypeConverter
b.然后覆盖convertValue这个方法,在里面进行数据转型
c.在action类所在的包下放置ActionClassName-conversion.properties文件,ActionClassName是类名,后面的-conversion.properties是固定的写法,
如:HelloWorldAction-conversion.properties
d.Properties文件里面的内容为:属性名称=类型转换器的全类名(既包名.类名)
如:birthday=com.ljq.type.converter.DateTypeConverter
(2)全局类型转换器
如果业务需求所有的日期都要转换,则可以使用全局类型转换器,只要在src根目录下面放置xwork-conversion.properties文件,并且properties文件中的内容为:
待转换的类型=类型转换器的全类名
如:java.util.Date = com.type.Converter.DateTypeConverter 即可

代码
Action类
package com.ljq.action;
import java.util.Date;
public class HelloWorldAction {
// 日期
private Date birthday;
// 枚举
private Gender gender;
public String execute() {
return "success";
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
System.out.println("birthday="+birthday);
this.birthday = birthday;
}
// 自定义枚举
public enum Gender {
MAN,WOMEN
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
System.out.println("gender="+gender);
this.gender = gender;
}
}
日期类型转换器
package com.ljq.type.converter; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter; /**
* 日期自定义类型转换器
*
* @author jiqinlin
*
*/
public class DateTypeConverter extends DefaultTypeConverter { @SuppressWarnings("unchecked")
@Override
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
try {
if (toType == Date.class) { // 当字符串向Date类型转换时
String[] params = (String[]) value;
return sdf.parseObject(params[0]);
} else if (toType == String.class) { // 当Date转换成字符串时
Date date=(Date)value;
return sdf.format(date);
}
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return null;
}
}
枚举类型转换器
package com.ljq.type.converter; import java.util.Map; import com.ljq.action.HelloWorldAction.Gender;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter; /**
* 枚举自定义类型转换器
*
* @author jiqinlin
*
*/
public class GenderTypeConverter extends DefaultTypeConverter{ @Override
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
if(toType==Gender.class){ //当字符串向Gender类型转换时
String[] params=(String[])value;
return Gender.valueOf(params[0]);
}else if (toType==String.class) { //当Gender转换成字符串时
Gender gender=(Gender)value;
return gender.toString();
}
return null;
}
}
配置类型转换器
测试路径
日期
http://localhost:8083/struts2/control/employee/list_execute.do?birthday=20110315 23:34:55
枚举
http://localhost:8083/struts2/control/employee/list_execute.do?gender=WOMEN 局部类型转换器: HelloWorldAction-conversion.properties
birthday=com.ljq.type.converter.DateTypeConverter
gender=com.ljq.type.converter.GenderTypeConverter 全局类型转换器: xwork-conversion.properties
java.util.Date=com.ljq.type.converter.DateTypeConverter
在页面打印日期和枚举的值
birthday=${birthday }
gender=${gender }
本文转自:http://www.cnblogs.com/linjiqin/archive/2011/03/16/1986565.html
Struts2之自定义类型转换器的更多相关文章
- struts2基础---->自定义类型转换器
这一章,我们开始struts2中自定义类型转换器的学习. 自定义类型转换器
- sruts2 自定义类型转换器
1.1.1 Struts2中自定义类型转换器:(了解) 类型转换的过程是双向的过程: JSP---->Action参数提交:String---Date. Action---->JSP ...
- Struts2框架的自定义类型转换器
前言:对于java的基本数据类型及一些系统类(如Date类.集合类),Struts2提供了内置类型转换功能,但是也有一定的限制.所以就演示出自定义类型转换器 一.应用于局部类型转换器 eg.用户登录出 ...
- [原创]java WEB学习笔记67:Struts2 学习之路-- 类型转换概述, 类型转换错误修改,如何自定义类型转换器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- struts2自定义类型转换器
首先,何为struts2的类型转换器? 类型转换器的作用是将请求中的字符串或字符串数组参数与action中的对象进行相互转换. 一.大部分时候,使用struts2提供的类型转换器以及OGNL类型转换机 ...
- Struts2初学 struts2自定义类型转换器
一.问题的引出 Struts2的类型转换是基于OGNL表达式的,由于请求的参数都是字符串,而JAVA 本身属于强类型的的语言,这样就需要把请求参数字符串转换成其他类型. Struts ...
- [JavaWeb基础] 013.Struts2 自定义类型转换器
很多时候,我们在做web开发的时候经常要用到时间的转换器,Struts2给我们提供了一种类型转换器的接口.下面我们讲讲怎么实现吧. 1.首先我们要定义一个类型转换的类,继承自com.babybus.s ...
- 自定义类型转换器converter
作用:目前将日期转换成string,将string转换成我想要的类型 0509课件里讲 一.数据类型转换在web应用程序中,数据存在两个方向上的转换:1.当提交表单时 表单数据以字符串的形式提交 ...
- Struts(二十):自定义类型转换器
如何自定义类型转换器: 1)为什么需要自定义类型转化器?strtuts2不能自动完成字符串到所有的类型: 2) 如何定义类型转化器? 步骤一:创建自定义类型转化器的类,并继承org.apache.st ...
随机推荐
- C++ traits
[本文链接] http://www.cnblogs.com/hellogiser/p/cplusplus-traits.html [分析] 什么是traits?其实它并不是一个新的概念,上个世纪90年 ...
- 让Delphi的DataSnap发挥最大效率
让Delphi的DataSnap发挥最大效率 让Delphi的DataSnap发挥最大效率 一个DataSnap的应用程序由两个层组成: DataSnap服务器,它有一个带有一个或者更多DataSet ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 处理html5离线应用程序存储的一些问题。
manifest方法引入appcache文件,缓存页面,是html5的新特性,通过加载一次,下次自动读取缓存,加载速度快,离线也能加载.缺点就是,被加载的页面会被强制缓存所有的内容. 为了解决不加载所 ...
- HDU 5744 Keep On Movin (贪心) 2016杭电多校联合第二场
题目:传送门. 如果每个字符出现次数都是偶数, 那么答案显然就是所有数的和. 对于奇数部分, 显然需要把其他字符均匀分配给这写奇数字符. 随便计算下就好了. #include <iostream ...
- [Linux] AWK命令详解(大全)
转载自:http://caoyanbao.iteye.com/blog/570868 什么是awk? 你可能对UNIX比较熟悉,但你可能对awk很陌生,这一点也不奇怪,的确,与其优秀的功能相比,awk ...
- 昨天用的流量有点多60M
就是因为值班这里没有无线,然后自己又是受前几次的影响,没有收到微信,然后就看了热点,这是用的快的.
- 用PHP实现定时器功能
1.直接使用PHP来完成定时 <?php ignore_user_abort(false);//当用户关闭页面时服务停止 set_time_limit(0); //设置执行时间,单位是秒.0表示 ...
- Ninja - chromium核心构建工具
转自:http://guiquanz.me/2014/07/28/a_intro_to_Ninja/ Ninja - chromium核心构建工具Jul 28, 2014 [在线编辑] 缘由 经过上次 ...
- PHP扩展开发
安装好php,进入安装目录. zbseoag@ubuntu:/usr/local/php-5.6.28/ext$ ./ext_skel --extname=mytest zbseoag@ubuntu: ...