Jsp/Servlet根据请求参数自动填充Java对象:表单Bean
(1)问题的引出:

为了简单方便我们需要一种可以一次性全部提交表单数据的技术,也就是要说的:Bean表单
(2)BeanUtilities代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.lc.ch04Biaodanshuju;import java.util.*;import javax.servlet.http.*;import org.apache.commons.beanutils.BeanUtils;public class BeanUtilities { public static void populateBean(Object formBean, HttpServletRequest request) { populateBean(formBean, request.getParameterMap()); } public static void populateBean(Object bean, Map propertyMap) { try { BeanUtils.populate(bean, propertyMap); } catch(Exception e) { // Empty catch. The two possible exceptions are // java.lang.IllegalAccessException and // java.lang.reflect.InvocationTargetException. // In both cases, just skip the bean operation. } }} |
(3)一下是利用一个收集保险雇员保险信息的servlet,用它来确定可行的保险计划和相关的费用。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package com.lc.ch04Biaodanshuju;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 SubmitInsuranceInfo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { InsuranceInfo info = new InsuranceInfo(); BeanUtilities.populateBean(info, request); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "\n"; String title = "Insurance Info for " + info.getName(); out.println(docType + "\n" + "<title>" + title + "</title>\n" + "\n" + "<center>\n" + "<h1>" + title + "</h1>\n" + "<ul>\n" + " <li>Employee ID: " + info.getEmployeeID() + "\n" + " </li><li>Number of children: " + info.getNumChildren() + "\n" + " </li><li>Married?: " + info.isMarried() + "\n" + "</li></ul></center>"); }} |
(4)连接数据库的javabean
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package com.lc.ch04Biaodanshuju;public class InsuranceInfo { private String name = "No name specified"; private String employeeID = "No ID specified"; private int numChildren = 0; private boolean isMarried = false; public String getName() { return(name); } public void setName(String name) { this.name = ServletUtilities.filter(name); } public String getEmployeeID() { return(employeeID); } public void setEmployeeID(String employeeID) { this.employeeID = ServletUtilities.filter(employeeID); } public int getNumChildren() { return(numChildren); } public void setNumChildren(int numChildren) { this.numChildren = numChildren; } public boolean isMarried() { return(isMarried); } public void setMarried(boolean isMarried) { this.isMarried = isMarried; }} |
(5)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<title>Employee Insurance Signup</title><center><h1>Employee Insurance Signup</h1><form action="/servlet/coreservlets.SubmitInsuranceInfo"> Name: <input type="TEXT" name="name"><br> Employee ID: <input type="TEXT" name="employeeID"><br> Number of Children: <input type="TEXT" name="numChildren"><br> <input type="CHECKBOX" name="married" value="true">Married?<br> <center><input type="SUBMIT"></center></form></center> |
(6)jar包的获取与使用:我们要使用工具类 必学的jar包,下载地址:http://download.csdn.net/detail/u010870518/7867181 别忘了添加配置!

感谢您的阅读,您的支持是我写博客动力。

浙公网安备 33010602011771号