首页 > 代码库 > javabean操作的工具类

javabean操作的工具类

 1 /**
 2      * 从页面参数获取值来更新bean
 3      * 使用说明:页面input框框name值要和javabean的属性值相同
 4      */
 5     public static void updateBean(HttpServletRequest request,Object bean){
 6         Enumeration<?> em = request.getParameterNames();
 7         while(em.hasMoreElements()){
 8             String param = em.nextElement().toString();
 9             if(jodd.bean.BeanUtil.hasProperty(bean, param)){
10                 try {
11                     Class<?> type = jodd.bean.BeanUtil.getPropertyType(bean, param);
12                     String value =http://www.mamicode.com/ request.getParameter(param);
13                     if (Clob.class.getName().equals(type.getName())) {
14                         Clob clob = ClobUtil.StrToClob(value);
15                         jodd.bean.BeanUtil.setProperty(bean, param, clob);
16                     }else if(Blob.class.getName().equals(type.getName())){
17                         Blob blob = BlobUtil.stringToBlob(value);
18                         jodd.bean.BeanUtil.setProperty(bean, param, blob);
19                     }else if("int".equals(type.getName())){
20                         if(!"".equals(value)){
21                             int num = Integer.parseInt(value);
22                             jodd.bean.BeanUtil.setProperty(bean, param, num);
23                         }
24                     }else {
25                         jodd.bean.BeanUtil.setProperty(bean, param, value);
26                     }
27                 } catch (Exception e) {
28                     
29                 }
30             }
31         }
32     }

这个小方法很有意思,仅仅用了一个request和普通的javabean对象就将form表单提交的参数封装进了javabean里面,即使现在很多技术可以实现formbean到javabean的映射,但是很多代码在项目进度允许的情况下,而且也实现了我们需要功能的时候,我会觉得更有意思。

使用方法:

1)创建一个javabean

 1 package com.sunyard.bean;
 2 
 3 public class Department {
 4     private Integer deptId;
 5 
 6     private String deptName;
 7 
 8     public Department() {
 9     }
10 
11     public Department(Integer deptId, String deptName) {
12         this.deptId = deptId;
13         this.deptName = deptName;
14     }
15 
16     public Integer getDeptId() {
17         return deptId;
18     }
19 
20     public void setDeptId(Integer deptId) {
21         this.deptId = deptId;
22     }
23 
24     public String getDeptName() {
25         return deptName;
26     }
27 
28     public void setDeptName(String deptName) {
29         this.deptName = deptName == null ? null : deptName.trim();
30     }
31 
32     @Override
33     public String toString() {
34         return "Department [deptId=" + deptId + ", deptName=" + deptName + "]";
35     }
36     
37 }

2)创建一个index.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="http://www.mamicode.com/">
11     
12     <title>My JSP ‘index.jsp‘ starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css">
21     -->
22     <script type="text/javascript" src="http://www.mamicode.com/WEB-INF/static/js/jquery-1.7.2.js"></script>
23     <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">  
24     <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
25     <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
26     
27     <script type="text/javascript">
28     
29         
30     </script>
31     
32   </head>
33   
34 <body>
35     <form action="test.do" method="post">
36         <input type="text" name="deptId"/>
37         <input type="text" name="deptName"/>
38         <input type="submit" value="http://www.mamicode.com/submit"/>
39     </form>
40 </body>
41 </html>

这里需要注意的是input的name属性必须和javabean的属性名称一样,才能做到后台的formbean到javabean的映射。

4)创建servlet

 

 1 package com.sunyard.controller;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 
 9 import com.sunyard.bean.Department;
10 import com.sunyard.util.BeanUtil;
11 
12 @Controller
13 public class EmpController {
14     
15     @RequestMapping("test.do")
16     public void test(HttpServletRequest request,HttpServletResponse response){
17         Department bean = new Department();
18         BeanUtil.updateBean(request, bean);
19         System.out.println(bean);
20     }
21 }

 

5)输出结果

技术分享

技术分享

 

 

 

javabean操作的工具类