KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > bean > loaders > RequestLoader


1 package jodd.bean.loaders;
2
3 import java.util.Enumeration;
4
5 import javax.servlet.http.HttpServletRequest;
6
7 import jodd.bean.BeanUtil;
8
9 /**
10  * Populates java bean from HttpServletRequest objects.
11  * <p>
12  *
13  * Names of requests parameters represents the names of bean properties.
14  * Values of requests parameters represents values of bean properties.
15  * <p>
16  *
17  * By java servlet specifications, parameter values are always String arrays
18  * (String[]). That is how they are sent to BeanUtil.setProperty(),
19  * except in case when this array contains just one String element. In that
20  * case it is sent as a single String.
21  * <p>
22  *
23  * BeanUtil.setProperty() will populate correctly target property.
24  */

25 public class RequestLoader implements jodd.bean.Loader {
26
27     public void load(Object bean, Object request) {
28         if (request instanceof HttpServletRequest) {
29             Enumeration paramNames = ((HttpServletRequest)request).getParameterNames();
30             while (paramNames.hasMoreElements()) {
31                 String paramName = (String) paramNames.nextElement();
32                 String[] paramValues = ((HttpServletRequest)request).getParameterValues(paramName);
33                 if (paramValues == null) {
34                     continue;
35                 }
36                 if (paramValues.length == 0) {
37                     continue;
38                 }
39                 if (paramValues.length == 1) {
40                     // send just String
41
BeanUtil.setProperty(bean, paramName, paramValues[0]);
42                 } else {
43                     // send String array
44
BeanUtil.setProperty(bean, paramName, paramValues);
45                 }
46             }
47         }
48         return;
49     }
50 }
51
Popular Tags