网上图书商城项目学习笔记-036工具类之CommonUtils及日期转换器
1.CommonUtils.java
package cn.itcast.commons; import java.util.Map;
import java.util.UUID; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils; /**
* 小小工具
* @author qdmmy6
*
*/
public class CommonUtils {
/**
* 返回一个不重复的字符串
* @return
*/
public static String uuid() {
return UUID.randomUUID().toString().replace("-", "").toUpperCase();
} /**
* 把map转换成对象
* @param map
* @param clazz
* @return
*
* 把Map转换成指定类型
*/
@SuppressWarnings("rawtypes")
public static <T> T toBean(Map map, Class<T> clazz) {
try {
/*
* 1. 通过参数clazz创建实例
* 2. 使用BeanUtils.populate把map的数据封闭到bean中
*/
T bean = clazz.newInstance();
ConvertUtils.register(new DateConverter(), java.util.Date.class);
BeanUtils.populate(bean, map);
return bean;
} catch(Exception e) {
throw new RuntimeException(e);
}
}
}
2.DateConverter.java
package cn.itcast.commons; import java.text.ParseException;
import java.text.SimpleDateFormat; import org.apache.commons.beanutils.Converter; /**
* 把String转换成java.util.Date的类型转换器
* @author qdmmy6
*
*/
public class DateConverter implements Converter { @SuppressWarnings("rawtypes")
@Override
public Object convert(Class type, Object value) {
if(value == null) return null;//如果要转换成值为null,那么直接返回null
if(!(value instanceof String)) {//如果要转换的值不是String,那么就不转换了,直接返回
return value;
}
String val = (String) value;//把值转换成String // 使用SimpleDateFormat进行转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(val);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
3..BeanUtils.java
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.apache.commons.beanutils; import java.lang.reflect.InvocationTargetException;
import java.util.Map; /**
* <p>Utility methods for populating JavaBeans properties via reflection.</p>
*
* <p>The implementations are provided by {@link BeanUtilsBean}.
* These static utility methods use the default instance.
* More sophisticated behaviour can be provided by using a <code>BeanUtilsBean</code> instance.</p>
*
* @author Craig R. McClanahan
* @author Ralph Schaer
* @author Chris Audley
* @author Rey Francois
* @author Gregor Rayman
* @version $Revision: 690380 $ $Date: 2008-08-29 21:04:38 +0100 (Fri, 29 Aug 2008) $
* @see BeanUtilsBean
*/ public class BeanUtils { // ------------------------------------------------------ Private Variables /**
* The debugging detail level for this component.
*
* Note that this static variable will have unexpected side-effects if
* this class is deployed in a shared classloader within a container.
* However as it is actually completely ignored by this class due to its
* deprecated status, it doesn't do any actual harm.
*
* @deprecated BeanUtils now uses commons-logging for all log messages.
* Use your favorite logging tool to configure logging for
* this class.
*/
private static int debug = 0; /**
* The <code>debug</code> static property is no longer used
* @return debug property
* @deprecated BeanUtils now uses commons-logging for all log messages.
* Use your favorite logging tool to configure logging for
* this class.
*/
public static int getDebug() {
return (debug);
} /**
* The <code>debug</code> static property is no longer used
* @param newDebug debug property
* @deprecated BeanUtils now uses commons-logging for all log messages.
* Use your favorite logging tool to configure logging for
* this class.
*/
public static void setDebug(int newDebug) {
debug = newDebug;
} // --------------------------------------------------------- Class Methods /**
* <p>Clone a bean based on the available property getters and setters,
* even if the bean class itself does not implement Cloneable.</p>
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @param bean Bean to be cloned
* @return the cloned bean
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InstantiationException if a new instance of the bean's
* class cannot be instantiated
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#cloneBean
*/
public static Object cloneBean(Object bean)
throws IllegalAccessException, InstantiationException,
InvocationTargetException, NoSuchMethodException { return BeanUtilsBean.getInstance().cloneBean(bean); } /**
* <p>Copy property values from the origin bean to the destination bean
* for all cases where the property names are the same.</p>
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @param dest Destination bean whose properties are modified
* @param orig Origin bean whose properties are retrieved
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception IllegalArgumentException if the <code>dest</code> or
* <code>orig</code> argument is null or if the <code>dest</code>
* property type is different from the source type and the relevant
* converter has not been registered.
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @see BeanUtilsBean#copyProperties
*/
public static void copyProperties(Object dest, Object orig)
throws IllegalAccessException, InvocationTargetException { BeanUtilsBean.getInstance().copyProperties(dest, orig);
} /**
* <p>Copy the specified property value to the specified destination bean,
* performing any type conversion that is required.</p>
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @param bean Bean on which setting is to be performed
* @param name Property name (can be nested/indexed/mapped/combo)
* @param value Value to be set
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @see BeanUtilsBean#copyProperty
*/
public static void copyProperty(Object bean, String name, Object value)
throws IllegalAccessException, InvocationTargetException { BeanUtilsBean.getInstance().copyProperty(bean, name, value);
} /**
* <p>Return the entire set of properties for which the specified bean
* provides a read method.</p>
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @param bean Bean whose properties are to be extracted
* @return Map of property descriptors
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#describe
*/
public static Map describe(Object bean)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException { return BeanUtilsBean.getInstance().describe(bean);
} /**
* <p>Return the value of the specified array property of the specified
* bean, as a String array.</p>
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @param bean Bean whose property is to be extracted
* @param name Name of the property to be extracted
* @return The array property value
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getArrayProperty
*/
public static String[] getArrayProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException { return BeanUtilsBean.getInstance().getArrayProperty(bean, name);
} /**
* <p>Return the value of the specified indexed property of the specified
* bean, as a String.</p>
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @param bean Bean whose property is to be extracted
* @param name <code>propertyname[index]</code> of the property value
* to be extracted
* @return The indexed property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getIndexedProperty(Object, String)
*/
public static String getIndexedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException { return BeanUtilsBean.getInstance().getIndexedProperty(bean, name); } /**
* Return the value of the specified indexed property of the specified
* bean, as a String. The index is specified as a method parameter and
* must *not* be included in the property name expression
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @param bean Bean whose property is to be extracted
* @param name Simple property name of the property value to be extracted
* @param index Index of the property value to be extracted
* @return The indexed property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getIndexedProperty(Object, String, int)
*/
public static String getIndexedProperty(Object bean,
String name, int index)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException { return BeanUtilsBean.getInstance().getIndexedProperty(bean, name, index); } /**
* </p>Return the value of the specified indexed property of the specified
* bean, as a String.</p>
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @param bean Bean whose property is to be extracted
* @param name <code>propertyname(index)</code> of the property value
* to be extracted
* @return The mapped property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getMappedProperty(Object, String)
*/
public static String getMappedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException { return BeanUtilsBean.getInstance().getMappedProperty(bean, name); } /**
* </p>Return the value of the specified mapped property of the specified
* bean, as a String.</p>
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @param bean Bean whose property is to be extracted
* @param name Simple property name of the property value to be extracted
* @param key Lookup key of the property value to be extracted
* @return The mapped property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getMappedProperty(Object, String, String)
*/
public static String getMappedProperty(Object bean,
String name, String key)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException { return BeanUtilsBean.getInstance().getMappedProperty(bean, name, key); } /**
* <p>Return the value of the (possibly nested) property of the specified
* name, for the specified bean, as a String.</p>
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @param bean Bean whose property is to be extracted
* @param name Possibly nested name of the property to be extracted
* @return The nested property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception IllegalArgumentException if a nested reference to a
* property returns null
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getNestedProperty
*/
public static String getNestedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException { return BeanUtilsBean.getInstance().getNestedProperty(bean, name); } /**
* <p>Return the value of the specified property of the specified bean,
* no matter which property reference format is used, as a String.</p>
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @param bean Bean whose property is to be extracted
* @param name Possibly indexed and/or nested name of the property
* to be extracted
* @return The property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getProperty
*/
public static String getProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException { return BeanUtilsBean.getInstance().getProperty(bean, name); } /**
* <p>Return the value of the specified simple property of the specified
* bean, converted to a String.</p>
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @param bean Bean whose property is to be extracted
* @param name Name of the property to be extracted
* @return The property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getSimpleProperty
*/
public static String getSimpleProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException { return BeanUtilsBean.getInstance().getSimpleProperty(bean, name); } /**
* <p>Populate the JavaBeans properties of the specified bean, based on
* the specified name/value pairs.</p>
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @param bean JavaBean whose properties are being populated
* @param properties Map keyed by property name, with the
* corresponding (String or String[]) value(s) to be set
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @see BeanUtilsBean#populate
*/
public static void populate(Object bean, Map properties)
throws IllegalAccessException, InvocationTargetException { BeanUtilsBean.getInstance().populate(bean, properties);
} /**
* <p>Set the specified property value, performing type conversions as
* required to conform to the type of the destination property.</p>
*
* <p>For more details see <code>BeanUtilsBean</code>.</p>
*
* @param bean Bean on which setting is to be performed
* @param name Property name (can be nested/indexed/mapped/combo)
* @param value Value to be set
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @see BeanUtilsBean#setProperty
*/
public static void setProperty(Object bean, String name, Object value)
throws IllegalAccessException, InvocationTargetException { BeanUtilsBean.getInstance().setProperty(bean, name, value);
} /**
* If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
*
* @param throwable The throwable.
* @param cause The cause of the throwable.
* @return true if the cause was initialized, otherwise false.
* @since 1.8.0
*/
public static boolean initCause(Throwable throwable, Throwable cause) {
return BeanUtilsBean.getInstance().initCause(throwable, cause);
} /**
* Create a cache.
* @return a new cache
* @since 1.8.0
*/
public static Map createCache() {
return new WeakFastHashMap();
} /**
* Return whether a Map is fast
* @param map The map
* @return Whether it is fast or not.
* @since 1.8.0
*/
public static boolean getCacheFast(Map map) {
if (map instanceof WeakFastHashMap) {
return ((WeakFastHashMap)map).getFast();
} else {
return false;
}
} /**
* Set whether fast on a Map
* @param map The map
* @param fast Whether it should be fast or not.
* @since 1.8.0
*/
public static void setCacheFast(Map map, boolean fast) {
if (map instanceof WeakFastHashMap) {
((WeakFastHashMap)map).setFast(fast);
}
}
}
网上图书商城项目学习笔记-036工具类之CommonUtils及日期转换器的更多相关文章
- 网上图书商城项目学习笔记-037工具类之BaseServlet及统一中文编码
1.统一中文编码分析 tomcat默认esetISO-8859-1编码,在servlet中,可能通过request的setCharacterEncoding(charset)和response.set ...
- 网上图书商城项目学习笔记-035工具类之JdbcUtils及TxQueryRunner及C3P0配置
事务就是保证多个操作在同一个connection,TxQueryRunner通过JdbcUtils获取连接,而JdbcUtils通过ThreadLocal<Connection>确保了不同 ...
- 网上图书商城项目学习笔记-011Book模块查询(分页)
一.流程分析 1.图书模块 2.分布分析 二.代码 1.view层 1)list.jsp <%@ page language="java" import="java ...
- 网上图书商城项目学习笔记-012BOOK模块查询2
一.分析 > 按图名查询(模糊)(分页)> 按作者查询(分页)> 按出版社查询(分页)> 按id查询> 多条件组合查询(分页) 二.代码 1.view层 (1)gj.js ...
- 网上图书商城项目学习笔记-014购物车模块页面javascrip
一.流程分析 二.代码 1.view层 (1)list.jsp <%@ page language="java" import="java.util.*" ...
- Google Guava学习笔记——基础工具类Joiner的使用
Guava 中有一些基础的工具类,如下所列: 1,Joiner 类:根据给定的分隔符把字符串连接到一起.MapJoiner 执行相同的操作,但是针对 Map 的 key 和 value. 2,Spli ...
- 【Java EE 学习 25 上】【网上图书商城项目实战】
一.概述 1.使用的jdk版本:1.6 2.java EE版本:1.6 3.指导老师:传智播客 王建 二.小项目已经实现的功能 普通用户: 1.登陆 2.注册 3.购物 4.浏览 管理员用户(全部管理 ...
- Google Guava学习笔记——基础工具类针对Object类的使用
Guava 提供了一系列针对Object操作的方法. 1. toString方法 为了方便调试重写toString()方法是很有必要的,但写起来比较无聊,不管如何,Objects类提供了toStrin ...
- Google Guava学习笔记——基础工具类Preconditions类的使用
Preconditions类是一组静态方法用来验证我们代码的状态.Preconditons类很重要,它能保证我们的代码按照我们期望的执行,如果不是我们期望的,我们会立即得到反馈是哪里出来问题,现在我们 ...
随机推荐
- (转)Ehcache作为分布式缓存的研究
ehcache支持两种拓扑结构,一种是Distributed Caching,另一种是Replicated Caching Distributed Caching 这和一般意义上的分布式缓存非常类似, ...
- Mysql单实例脚本自动化安装
安装包:mysql-5.6.31.tar.gz 已有配置文件:my.cnf *注意:本次Mysql的配置文件是在my.cnf的基础上更改得到的,my.cnf存放路径为/opt/rh/my.cnf 脚本 ...
- 简单的MySQLDB类
<?php error_reporting(E_ALL ^ E_DEPRECATED); //数据库操作类 class MySQLDB{ //属性--必要的信息 private $_host; ...
- Angularjs在线编辑器
1.TextAngular: https://github.com/fraywing/textAngular textAngular是一个强大的Text-Editor/Wysiwyg 编辑器,用于An ...
- 用C#对ADO.NET数据库完成简单操作
数据库访问是程序中应用最普遍的部分.随着C#和ADO.NET的引入,这种操作变得更简单.这篇文章将示范四种最基础的数据库操作. ● 读取数据.其中包括多种数据类型:整型,字符串,日期型. ● 写数据. ...
- 开发问题记录——ArcEngine问题记录
ArcEngine 使用Winform进行坐标投影变换,用到AE空间,出现如下错误: “ESRI.ArcGIS.esriSystem.IXMLSerialize”在未被引用的程序集中定义.必须添加 ...
- SSO单点登陆
一句话,就是能让各个不同的域名带回相同的认证信息即可.实现方法,就是把其中一个登陆后,把认证的信息分别保存在不同域名下的 cookie,当在验证是否登陆时,验证 cookie,如果是子域名,这个则直接 ...
- JAVA对象是如何占用内存的
本文使用的是32位的JVM ,jdk1.6.本文基本是翻译的,加上了一些自己的理解,原文见文章底下链接. 在本文中,我们讨论如何计算或者估计一个JAVA对象占多少内存空间.(注意,使用 C ...
- [Linux] Linux学习笔记(5)-文件与目录管理
1.Linux目录结构为树状结构,最顶层的目录为跟目录"/",其它目录通过挂载可以将它添加到目录树中,通过解除挂载移除它们. 2.绝对路径与相对路径 绝对路径写法:由根目录&quo ...
- 普通用户开启AUTOTRACE 功能
AUTOTRACE是一个SQL*Plus工具,用于跟踪SQL的执行计划,收集执行时所耗用资源的统计信息.系统账户本身具有AUTOTRACE,其他账户需要通过手动赋予 一. 用系统账户登录(DBA) S ...