Java反射机制:表单数据自动封装到JavaBean中【IT】
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
標簽:? it | 分類:?IT路人丁 |
利用Java的反射機制,模擬一個簡單的Struts控制器ActionServlet時,為了將請求的表單數(shù)據(jù)自動封裝到JavaBean中,用到BeanUtils和PropertyUtils兩個方法。網(wǎng)上找到個參考例子:
PropertyUtils用法示例
轉(zhuǎn)自:http://hi.baidu.com/tianlong1569/blog/item/7d6cff03317027723812bb9b.html
頁面表單數(shù)據(jù)的自動封裝到javaBean中
先定義一個Bean類
package com.test;
public class Bean {
private String name;
private Integer sex;
public String getName() {
???return name;
}
public void setName(String name) {
???this.name = name;
}
public Integer getSex() {
???return sex;
}
public void setSex(Integer sex) {
???this.sex = sex;
}
}
再定義一個封裝數(shù)據(jù)類用于把數(shù)據(jù)封裝到Bean中
package com.test;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class ParseHtml {
public Object parseRequest(HttpServletRequest request,Object bean){
???//取得所有參數(shù)列表
???Enumeration enum = request.getParameterNames();
???//遍歷所有參數(shù)列表
???while(enum.hasMoreElements()){
???
????Object obj = enum.nextElement();
????try {
?????//取得這個參數(shù)在Bean中的數(shù)據(jù)類開
?????Class cls = PropertyUtils.getPropertyType(bean, obj.toString());
?????//把相應的數(shù)據(jù)轉(zhuǎn)換成對應的數(shù)據(jù)類型
?????Object beanValue = ConvertUtils.convert(request.getParameter(obj.toString()), cls);
?????//添沖Bean值
?????PropertyUtils.setProperty(bean, obj.toString(), beanValue);
????} catch (IllegalAccessException e) {
?????// TODO Auto-generated catch block
?????e.printStackTrace();
????} catch (InvocationTargetException e) {
?????// TODO Auto-generated catch block
?????e.printStackTrace();
????} catch (NoSuchMethodException e) {
?????// TODO Auto-generated catch block
?????e.printStackTrace();
????}
???}
??
???return bean;
}
}
生成表單頁面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
????<base href="<%=basePath%>">
????
????<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">????
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
????<form action="Dispose" method="post">
????<input type=text name="name"><br>
????<input type=text name="sex"><br>
????<input type=submit value='submit'>
????</form>
</body>
</html>
定義一個Action用于處理表單傳過來的數(shù)據(jù)
#SinaEditor_Temp_FontName
package com.test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Dispose extends HttpServlet {
public Dispose() {
???super();
}
public void destroy() {
???super.destroy(); // Just puts "destroy" string in log
???// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
????throws ServletException, IOException {
????this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
????throws ServletException, IOException {
????//調(diào)用方法完成對表單數(shù)據(jù)封裝到Bean中
????Bean bean = (Bean)new ParseHtml().parseRequest(request, new Bean());
????response.getWriter().write("name="+bean.getName()+"sex="+bean.getSex());
???
}
public void init() throws ServletException {
???// Put your code here
}
}
BeanUtils使用方法
轉(zhuǎn)載自:http://paomo30000.javaeye.com/blog/408207
一、簡介:
BeanUtils提供對 Java反射和自省API的包裝。其主要目的是利用反射機制對JavaBean的屬性進行處理。我們知道,一個JavaBean通常包含了大量的屬性,很多情況下,對JavaBean的處理導致大量get/set代碼堆積,增加了代碼長度和閱讀代碼的難度。
二、用法:
BeanUtils是這個包里比較常用的一個工具類,這里只介紹它的copyProperties()方法。該方法定義如下:
如果你有兩個具有很多相同屬性的JavaBean,一個很常見的情況就是Struts里的PO對象(持久對象)和對應的ActionForm,例如 Teacher和TeacherForm。我們一般會在Action里從ActionForm構(gòu)造一個PO對象,傳統(tǒng)的方式是使用類似下面的語句對屬性逐個賦值:
Java代碼?
如果Teacher和TeacherForm間存在名稱不相同的屬性,則BeanUtils不對這些屬性進行處理,需要程序員手動處理。例如 Teacher包含modifyDate(該屬性記錄最后修改日期,不需要用戶在界面中輸入)屬性而TeacherForm無此屬性,那么在上面代碼的 copyProperties()后還要加上一句:
一個較好的例子:
see: ?http://oyprunner.javaeye.com/blog/573226
轉(zhuǎn)載于:https://my.oschina.net/heiyexue/blog/479352
總結(jié)
以上是生活随笔為你收集整理的Java反射机制:表单数据自动封装到JavaBean中【IT】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到闺蜜生小孩是怎么回事
- 下一篇: Java设计模式6:策略模式