30天轻松掌握JavaWeb_使用beanutils
导入commons-beanutils-1.8.3.jar及commons-logging-1.1.3.jar
使用commons-beanutils-1.8.3.jar包时需要同时使用commons-logging-1.1.3.jar包,因为需要写日志。
package com.wzh.test.beanutils; import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Assert;
import org.junit.Test; //使用BeanUtils操作Bean属性(第三方)
public class Demo1 { @Test
public void test1() throws IllegalAccessException,
InvocationTargetException {
Person p = new Person();
BeanUtils.setProperty(p, "name", "victor"); System.out.println("name:"+p.getName());
} @Test
public void test2() throws IllegalAccessException,
InvocationTargetException {
Person p = new Person();
// 自动将String转为int 支持8种基本数据类型
BeanUtils.setProperty(p, "age", "23");
// 默认不支持时间转换
BeanUtils.setProperty(p, "Birthday", "2012-3-1");
System.out.println("age:"+p.getAge());
System.out.println("birthday:"+p.getBirthday());
} @Test
public void test3() throws IllegalAccessException,
InvocationTargetException { // 为了让日期赋到Bean的Birthday属性上,我们给BeanUtils注册一个日期转换器
// 方法1.
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
if (value == null)
return null; if (!(value instanceof String)) {
System.out.println("不是日期类型");
throw new ConversionException("不是日期类型");
} String str = (String) value;
if (str.trim().equals("")) {
return null;
} SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(str);
} catch (ParseException e) {
throw new RuntimeException(e);// 异常链不能断
}
}
}, Date.class); // ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person p = new Person();
// 自动将String转为int 支持8种基本数据类型 BeanUtils.setProperty(p, "birthday", "2012-12-12");
System.out.println("birthday2:"+p.getBirthday());
} @Test
public void test4() throws IllegalAccessException,
InvocationTargetException { Map map = new HashMap();
map.put("name", "aa");
map.put("birthday", "2012-1-1");
//使用自带的转换器
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person p = new Person();
// 自动将String转为int 支持8种基本数据类型 BeanUtils.populate(p, map);//用Map集合中的值填充到Bean的属性
System.out.println(p.getBirthday());
}
}
30天轻松掌握JavaWeb_使用beanutils的更多相关文章
- 30天轻松学习javaweb_通过telnet连接http服务器
telnet是windows自带的网络连接工具,可以用于连接任何服务器. 通过Telnet连接服务端 Telnet localhost 8080GET /news/1.html HTTP/1.1Hos ...
- 30天轻松学习javaweb_打包web项目成war
jar -cvf news.war news 打包成 war 包后复制到webapps下,Tomcat将会解压.
- 30天轻松学习javaweb_模拟tomcat
运行 javac Server.java 编译java文件 执行 java Server 运行程序 在ie中输入 http://localhost:9999/ 打开模拟的服务程序 import jav ...
- 30天轻松学习javaweb_修改tomcat的servlet模板
在MyEclipse目录下搜索com.genuitec.eclipse.wizards 得到搜索结果 com.genuitec.eclipse.wizards_9.0.0.me201108091322 ...
- 30天轻松学习javaweb_通过javac编译java文件
通过javac编译java文件1.先导入需要引用的包D:\Program Files (x86)\apache-tomcat-7.0.53\webapps\test\WEB-INF\classes&g ...
- 30天轻松学习javaweb_Eclipse在修改了web.xml后将自动更新到tomcat服务器中
context.xml中增加<WatchedResource>WEB-INF/web.xml</WatchedResource>,Eclipse在修改了web.xml后将自动更 ...
- 30天轻松学习javaweb_http头信息实例
package com.wzh.test.http; import java.io.ByteArrayOutputStream;import java.io.IOException;import ja ...
- 30天轻松学习javaweb_https协议的密码学
https通过非对称加密实现数据安全1.CA机构提供数字证书,其中数字证书包含公钥.2.浏览器自带功能验证数字证书是否是CA机构颁发的.3.根据数字证书包含的公钥对表单数据进行加密.4.公钥提供方再根 ...
- 30天轻松学习javaweb_tomcat的虚拟目录设置
1.在server.xml文件的</Host>前面加入.需要重新启动Tomcat才能生效.<!--配置虚拟目录--><Context path="/itcast ...
随机推荐
- Linux-Nginx之sendfile与上下文切换
今天在看nginx thread pool的时候,频繁的看到sendfile,其实以前也经常看到sendfile,只是我平时选择性的忽视而已... 先说下sendfile,明天在好好聊下nginx 线 ...
- JSP 相关试题(三)
判断题 1.动态网页和静态网页的根本区别在于服务器端返回的HTML文件是事先存储好的还是由动态网页程序生成的(对 ) 2.Web开发技术包括客户端和服务器端的技术.(对 ) 3.Tomcat和JDK都 ...
- x2go
单词解析 productivity n. 生产力:生产率:生产能力seamlessly adv. 无缝地roam constantly 经常漫游agility and flex ...
- noip2012普及组——质因数分解
[问题描述]已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. [输入]输入文件名为 prime.in.输入只有一行,包含一个正整数 n. [输出]输出文件名为 prime.out.输出只 ...
- pouchdb Conflicts
Conflicts are an unavoidable reality when dealing with distributed systems. And make no mistake: cli ...
- Top JavaScript Frameworks, Libraries & Tools and When to Use Them
It seems almost every other week there is a new JavaScript library taking the web community by storm ...
- Linux -RAID
转自:http://www.cnblogs.com/xiaoluo501395377/archive/2013/05/25/3099464.html 硬盘类型 速度 SATA <150M/s S ...
- 【转】C#安装包(自动卸载低版本)
一:版本号升级 1.ProductCode.Version(version比前一版本要高比如以前1.0.0现在就是1.0.1), 2.保持UpgradeCode不变(当设置RemovePrev ...
- 运行Appium碰到的坑们
运行Appium的时候,碰到的那些坑 1. java命令会出现error:could not open ...jvm.cfg 出现这种情况大多是因为电脑上之前安装过JDK,卸载重装之后,运行java命 ...
- ASP.NET让FileUpload控件支持浏览自动上传功能的解决方法
ASP.NET的FileUpload控件默认是不支持服务端的onchange事件的,此时可以用一种变通的方法来实现这一功能. 这就需要借用客户端的onchange事件,调用__doPostBack方法 ...