提高生产力:SpringMVC中,使用扩展数据类型TypedMap接收Web请求参数
在Web项目中,如果前端MVC框架使用的是SpringMVC,可以使用Map接收前端请求参数,比bean要方便很多。
尤其是SpringMVC和Mybatis一起用的时候,用Map大大减少了需要的bean/vo/po之类的东东。
用Map也会遇到一个问题,就是类型转换的代码特别的多。
/**
* 得到当前页面的页数
*/
private int getCurrentPage(Map<String, Object> params){
return Integer.parseInt(params.get(PageUtils.CURRENT_PAGE).toString());
}
从Map中获得一个Object类型的值,然后toString,再次转换为目标Integer等类型。
不够简洁,即使提取了工具方法,代码也还是有点繁琐。
因此,我对Map进行了一次封装。(有人其实已经这么做了,我也是受到别人的启发,才决定进行封装的。 只不过,我需要简洁的符合自己偏好的而已。)
使用方式
@RequestMapping(value = "/list")
@ResponseBody
public PageVO list(@RequestParam TypedMap<String, Object> params) {
Integer age = params.getInt("age");
String name = params.getString("name");
}
TypedMap代码实现
package cn.fansunion.common.collection; import java.util.Date;
import java.util.HashMap; import org.apache.commons.lang3.StringUtils; import cn.fansunion.common.converter.Integers;
import cn.fansunion.common.converter.Longs;
import cn.fansunion.common.util.TimeUtil; /**
* 带类型的Map,在Map的基础上,增加了getInt,getLong等工具方法。
*
* @author leiwen
*
*/
public class TypedMap<K, V> extends HashMap<K, V> { private static final long serialVersionUID = 1L; /**
* 从集合中获得一个Integer类型的值。
*
* @param key
* 键
* @param defaultValue
* 默认值,如果根据key没有找到对应的值,使用该默认值
* @return 该key对应的值或者为defaultValue
*/
public Integer getInt(String key, Integer defaultValue) {
Object v = get(key);
Integer result = Integers.valueOf(v, defaultValue);
return result;
} /**
* 根据key获得Integer类型的值。
*
* @param key
* 键
* @return 该key对应的值,如果没有找到,则返回null
*/
public Integer getInt(String key) {
return getInt(key, null);
} /**
* 从集合中获得一个String类型的值。
*
* @param key
* 键
* @param defaultValue
* 默认值,如果根据key没有找到对应的值,使用该默认值
* @return 该key对应的值或者为defaultValue
*/
public String getString(String key, String defaultValue) { Object value = get(key);
if (value == null) {
return defaultValue;
} String result = defaultValue;
if (value instanceof String) {
result = (String) value;
} else {
result = String.valueOf(value);
}
return result;
} /**
* 根据key获得String类型的值。
*
* @param key
* 键
* @return 该key对应的值,如果没有找到,则返回null
*/
public String getString(String key) {
return getString(key, null);
} /**
* 根据key获得Long类型的值。
*
* @param key
* 键
* @return 该key对应的值,如果没有找到,则返回null
*/
public Long getLong(String key) {
return getLong(key, null);
} /**
* 从集合中获得一个Long类型的值。
*
* @param key
* 键
* @param defaultValue
* 默认值,如果根据key没有找到对应的值,使用该默认值
* @return 该key对应的值或者为defaultValue
*/
public Long getLong(String key, Long defaultValue) {
return Longs.valueOf(get(key), defaultValue);
} public Boolean getBoolean(String key, Boolean defaultValue) {
Object value = get(key);
if (value == null) {
return defaultValue;
}
if (Boolean.class.isInstance(value)) {
return (Boolean) value;
} else {
return defaultValue;
}
} /**
* 根据key获得Boolean类型的值
*
* @param key
* 键
* @return 该键对应的值,如果没有找到,返回null
*/
public Boolean getBoolean(String key) {
return getBoolean(key, null);
} /**
* 从集合中获得一个Date类型的值。
*
* @param key
* 键
* @return 该key对应的值,如果没有找到,返回null
*/
public Date getDate(String key) {
return getDate(key, null);
} /**
* 从集合中获得一个Date类型的值。
*
* @param key
* 键
* @param defaultValue
* 默认值,如果根据key没有找到对应的值,使用该默认值
* @return 该key对应的值或者为defaultValue
*/
public Date getDate(String key, Date defualtValue) {
Object value = get(key);
if (value == null) {
return null;
} if (value instanceof Date) {
return (Date) value;
} else {
String str = value.toString();
if (StringUtils.isNotEmpty(str)) {
return TimeUtil.parseYmdHms(key);
} }
return defualtValue;
} /**
* 根据key获得目标类型的值
*
* @param key
* 键
* @param type
* 目标类型
* @return 该键对应的值,如果没有找到,返回null
*/
public <T> T getTypedObject(String key, T type) {
Object value = get(key);
if (value == null) {
return null;
}
return (T) value;
} }
TypedMap单元测试代码
package cn.fansunion.common.collection; import java.util.Date; import junit.framework.TestCase; import org.junit.Test; /**
* @author leiwen
*/
public class TypedMapTest extends TestCase { @Test
public static void testGetInt() {
TypedMap<String, Object> map = createMap(); Integer value = 198962;
map.put("intKey", value);
assertEquals(value, map.getInt("intKey"));
assertNull(map.getInt("notExistIntKey"));
} @Test
public static void testGetIntWithDefaultValue() {
TypedMap<String, Object> map = createMap();
Integer defaultValue = 0;
Integer value = 198962;
map.put("intKey", value);
assertEquals(value, map.getInt("intKey", defaultValue));
assertEquals(defaultValue, map.getInt("notExistIntKey", defaultValue));
} @Test
public static void testGetLong() {
TypedMap<String, Object> map = createMap(); Long value = 198962L;
map.put("longKey", value);
assertEquals(value, map.getLong("longKey"));
assertNull(map.getLong("notExistLongKey"));
} @Test
public static void testGetLongWithDefualtValue() {
TypedMap<String, Object> map = createMap();
Long defaultValue = 0L;
Long value = 198962L;
map.put("longKey", value);
assertEquals(value, map.getLong("longKey", defaultValue));
assertEquals(defaultValue, map.getLong("notExistLongKey", defaultValue));
} @Test
public static void testGetString() {
TypedMap<String, Object> map = createMap(); String value = "http://FansUnion.cn";
map.put("stringKey", value);
assertEquals(value, map.getString("stringKey"));
assertNull(map.getString("notExistStringKey"));
} @Test
public static void testGetStringWithDefualtValue() {
TypedMap<String, Object> map = createMap();
String defaultValue = "leiwen";
String value = "http://FansUnion.cn";
map.put("stringKey", value);
assertEquals(value, map.getString("stringKey", defaultValue));
assertEquals(defaultValue,
map.getString("notExistStringKey", defaultValue));
} @Test
public static void testGetDate() {
TypedMap<String, Object> map = createMap(); Date value = new Date();
map.put("dateKey", value);
assertEquals(value, map.getDate("dateKey"));
assertNull(map.getDate("notExistDateKey"));
} @Test
public static void testGetDateWithDefualtValue() {
TypedMap<String, Object> map = createMap();
Date defaultValue = null;
Date value = new Date();
map.put("dateKey", value);
assertEquals(value, map.getDate("dateKey", defaultValue));
assertEquals(defaultValue, map.getDate("notExistDateKey", defaultValue));
} @Test
public static void testGetBoolean() {
TypedMap<String, Object> map = createMap(); Boolean value = false;
map.put("booleanKey", value);
assertEquals(value, map.getBoolean("booleanKey"));
assertNull(map.getBoolean("notExistBooleanKey"));
} @Test
public static void testGetBooleanWithDefaultValue() {
TypedMap<String, Object> map = createMap();
Boolean defaultValue = false;
Boolean value = true;
map.put("booleanKey", value);
assertEquals(value, map.getBoolean("booleanKey", defaultValue));
assertEquals(defaultValue,
map.getBoolean("notExistBooleanKey", defaultValue));
} @Test
public static void testGetTypedObject() {
TypedMap<String, Object> map = createMap();
Integer value = 198962;
map.put("intKey", value);
assertEquals(value, map.getTypedObject("intKey", Integer.class)); } public static TypedMap<String, Object> createMap() {
return new TypedMap<String, Object>();
}
}
原文参见: 提高生产力:SpringMVC中,使用扩展数据类型TypedMap接收Web请求参数
相关阅读: 提高生产力
提高生产力:SpringMVC中,使用扩展数据类型TypedMap接收Web请求参数的更多相关文章
- springMvc源码学习之:spirngMVC获取请求参数的方法2
@RequestParam,你一定见过:@PathVariable,你肯定也知道:@QueryParam,你怎么会不晓得?!还有你熟悉的他 (@CookieValue)!她(@ModelAndView ...
- QT 信号槽connect中解决自定义数据类型或数组作为函数参数的问题——QT qRegisterMetaType 注册MetaType——关键:注册自定义数据类型或QMap等容器类
一般情况下信号槽直接连接方式不会出现问题,但是如果信号与槽在不同线程或Qt::QueuedConnection方式连接,可能会在连接期间报以下类似问题,如: QObject::connect: Can ...
- springMvc源码学习之:spirngMvc获取请求参数的方法
一. 通过@PathVariabl获取路径中的参数 @RequestMapping(value="user/{id}/{name}",method=RequestMeth ...
- springMVC(spring)+WebSocket案例(获取请求参数)
开发环境(最低版本):spring 4.0+java7+tomcat7.0.47+sockjs 前端页面要引入: <script src="http://cdn.jsdelivr.ne ...
- SpringMVC中使用RedirectAttributes重定向传参,防止暴露参数
RedirectAttributes是SpringMVC3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的. 当我从jsp页面函数中带参数到controller层方法,方法执行完毕后返回 ...
- [置顶] 提高生产力:Web开发基础平台WebCommon的设计和实现
Web开发中,存在着各种各样的重复性的工作.为了提高开发效率,不在当码农,我在思考和实践如何搭建一个Web开发的基础平台. Web开发基础平台的目标和功能 1.提供一套基础的开发环境,整合了常用的框架 ...
- 提高生产力:Web开发基础平台WebCommon的设计和实现
Web开发中,存在着各种各样的重复性的工作.为了提高开发效率,不在当码农,我在思考和实践如何搭建一个Web开发的基础平台. Web开发基础平台的目标和功能 1.提供一套基础的开发环境,整合了常用的框架 ...
- Spring|SpringMVC中的注解
文章目录 一.Spring注解 @Controller @ResuController @Service @Autowired @RequestMapping @RequestParam @Model ...
- 提高生产力:Web前端验证的标准化
统一验证标准,减少重复劳动,提高生产力. 当公司内部有多个Web项目的时候,统一验证标准就很有必要了.统一不同项目的验证规则,比如 同为用户名 使用同一套标准,甚至用户名和机构名等也使用同一套标准.( ...
随机推荐
- 【Android界面实现】SlidingMenu最新版本号使用具体解释
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 在非常久之前的一篇文章中,简单的介绍了一下开源项目SlidingMenu控件的使用,这一篇文章,将比較具体的 ...
- HDU 2255 奔小康赚大钱 KM算法题解
KM算法求的是完备匹配下的最大权匹配,是Hungary算法的进一步,由于Hungary算法是最大匹配的算法,不带权. 经典算法,想不出来的了,要參考别人的.然后消化吸收吧. 由于真的非常复杂的算法. ...
- 6581 Number Triangle
6581 Number Triangle 时间限制:500MS 内存限制:1000K提交次数:57 通过次数:47 题型: 编程题 语言: G++;GCC Description 7 3 8 8 ...
- 黑马day01xml 解析方式与原理分析
dom解析方式和sax解析
- Codeforces Round #244 (Div. 2)D (后缀自己主动机)
Codeforces Round #244 (Div. 2)D (后缀自己主动机) (标号为0的节点一定是null节点,不管怎样都不能拿来用,切记切记,以后不能再错了) 这题用后缀自己主动机的话,对后 ...
- HDU 5063 Operation the Sequence(暴力)
HDU 5063 Operation the Sequence 题目链接 把操作存下来.因为仅仅有50个操作,所以每次把操作逆回去执行一遍,就能求出在原来的数列中的位置.输出就可以 代码: #incl ...
- 什么是 SHTML
什么是 SHTML 使用SSI(Server Side Include)的html文件扩展名,SSI(Server Side Include),通常称为“服务器端嵌入”或者叫“服务器端包含”,是一种类 ...
- luogu1072 Hankson的趣味题
题目大意 给出数a0, a1, b0, b1,求满足gcd(a0, x)=a1, lcm(b0, x)=b1的x的个数 解法一 枚举b1的因数,看看是否满足上述条件. 怎样枚举因数 试除法.对于1~s ...
- ioctl方法详解
设备控制接口(ioctl 函数)回想一下我们在字符设备驱动中介绍的struct file_operations 结构,这里我们将介绍一个新的方法: int (*ioctl) (struct inode ...
- js重定向
在现行的网站应用中URL重定向的应用有很多: 404页面处理.网址改变(t.sina转到weibo.com).多个网站地址(如:http://www.google.com/ .www.g.cn )等: ...