第四次学习:
这次学习碰到了与struts1.1很不同的地方,就是ActionForm类不仅仅是String型,可以是任意数据类型了.
1.表单录入(含bean的嵌套).
设有一个testBean类,这个类中又含有一个nested属性(nested为一个类的实例),当要为nested类的booleanProperty属性设定值时用nested.booleanProperty指定位置.下面例子录入数据时,数据填充到testBean和nested中去.
对应的TestBean
这次学习碰到了与struts1.1很不同的地方,就是ActionForm类不仅仅是String型,可以是任意数据类型了.
1.表单录入(含bean的嵌套).
设有一个testBean类,这个类中又含有一个nested属性(nested为一个类的实例),当要为nested类的booleanProperty属性设定值时用nested.booleanProperty指定位置.下面例子录入数据时,数据填充到testBean和nested中去.
java代码: |
1 2 <%@ taglib uri="/tags/struts-bean" prefix="bean" %> 3 <%@ taglib uri="/tags/struts-html" prefix="html" %> 4 <%@ taglib uri="/tags/struts-logic" prefix="logic" %> 5 <html:html> 6 <head> 7 <title>Test struts-html Property Setters</title> 8 </head> 9 <body> 10 <div align="center"> 11 <h1>Test struts-html Property Setters</h1> 12 </div> 13 <p>Whatever changes you make to properties should be reflected when the page is redisplayed. Press "Save" to update, or "Cancel" to return to the main menu.</p> 14 <html:form action="/html-setters-submit"> 15 <table border=" 0" width=" 100%"> 16 <tr> 17 <th align="center" colspan=" 4">Scalar Properties</th> 18 </tr> 19 <tr> 20 <th align="right">booleanProperty</th> 21 <td align="left"> 22 <html:checkbox property="booleanProperty" /> 23 </td> 24 <th align="right">nested. booleanProperty</th> 25 <td align="left"> 26 <html:checkbox property="nested. booleanProperty" /> 27 </td> 28 </tr> 29 <tr> 30 <th align="right">doubleProperty</th> 31 <td align="left"> 32 <html:text property="doubleProperty" size=" 32" /> 33 </td> 34 <th align="right">nested. doubleProperty</th> 35 <td align="left"> 36 <html:text property="nested. doubleProperty" size=" 32" /> 37 </td> 38 </tr> 39 <tr> 40 <th align="right">floatProperty</th> 41 <td align="left"> 42 <html:text property="floatProperty" size=" 32" /> 43 </td> 44 <th align="right">nested. floatProperty</th> 45 <td align="left"> 46 <html:text property="nested. floatProperty" size=" 32" /> 47 </td> 48 </tr> 49 <tr> 50 <th align="right">intProperty</th> 51 <td align="left"> 52 <html:text property="intProperty" size=" 32" /> 53 </td> 54 <th align="right">nested. intProperty</th> 55 <td align="left"> 56 <html:text property="nested. intProperty" size=" 32" /> 57 </td> 58 </tr> 59 <tr> 60 <th align="right">longProperty</th> 61 <td align="left"> 62 <html:text property="longProperty" size=" 32" /> 63 </td> 64 <th align="right">nested. longProperty</th> 65 <td align="left"> 66 <html:text property="nested. longProperty" size=" 32" /> 67 </td> 68 </tr> 69 <tr> 70 <th align="right">stringProperty</th> 71 <td align="left"> 72 <html:text property="stringProperty" size=" 32" /> 73 </td> 74 <th align="right">nested. stringProperty</th> 75 <td align="left"> 76 <html:text property="nested. stringProperty" size=" 32" /> 77 </td> 78 </tr> 79 <tr> 80 <th align="center" colspan=" 4">Indexed Properties</th> 81 </tr> 82 <tr> 83 <th align="right">intIndexed [ 0 ]</th> 84 <td align="left"> 85 <html:text property="intIndexed [ 0 ]" size=" 32" /> 86 </td> 87 <th align="right">nested. intIndexed [ 0 ]</th> 88 <td align="left"> 89 <html:text property="nested. intIndexed [ 0 ]" size=" 32" /> 90 </td> 91 </tr> 92 <tr> 93 <th align="right">intIndexed [ 1 ]</th> 94 <td align="left"> 95 <html:text property="intIndexed [ 1 ]" size=" 32" /> 96 </td> 97 <th align="right">nested. intIndexed [ 1 ]</th> 98 <td align="left"> 99 <html:text property="nested. intIndexed [ 1 ]" size=" 32" /> 100 </td> 101 </tr> 102 <tr> 103 <th align="right">stringIndexed [ 0 ]</th> 104 <td align="left"> 105 <html:text property="stringIndexed [ 0 ]" size=" 32" /> 106 </td> 107 <th align="right">nested. stringIndexed [ 0 ]</th> 108 <td align="left"> 109 <html:text property="nested. stringIndexed [ 0 ]" size=" 32" /> 110 </td> 111 </tr> 112 <tr> 113 <th align="right">stringIndexed [ 1 ]</th> 114 <td align="left"> 115 <html:text property="stringIndexed [ 1 ]" size=" 32" /> 116 </td> 117 <th align="right">nested. stringIndexed [ 1 ]</th> 118 <td align="left"> 119 <html:text property="nested. stringIndexed [ 1 ]" size=" 32" /> 120 </td> 121 </tr> 122 <tr> 123 <td> </td> 124 <td align="right"> 125 <html:submit>Save</html:submit> 126 </td> 127 <td align="left"> 128 <html:reset>Reset</html:reset> 129 <html:cancel>Cancel</html:cancel> 130 </td> 131 <td> </td> 132 </tr> 133 </table> 134 </html:form> 135 </body> 136 </html:html> 137 138 |
对应的TestBean
java代码: ![]() |
1 2 /* 3 * $Header: /home/cvs/jakarta-struts/src/examples/org/apache/struts/webapp/exercise/TestBean.java,v 1.4 2004/03/14 06:23:52 sraeburn Exp $ 4 * $Revision: 1.4 $ 5 * $Date: 2004/03/14 06:23:52 $ 6 * 7 * Copyright 1999-2004 The Apache Software Foundation. 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 23 package org. apache. struts. webapp. exercise; 24 25 26 import java. util. ArrayList; 27 import java. util. Collection; 28 import java. util. HashMap; 29 import java. util. List; 30 import java. util. Map; 31 import java. util. Vector; 32 import javax. servlet. http. HttpServletRequest; 33 import org. apache. struts. action. ActionForm; 34 import org. apache. struts. action. ActionMapping; 35 import org. apache. struts. util. LabelValueBean; 36 37 38 /** 39 * General purpose test bean for Struts custom tag tests. 40 * 41 * @version $Revision: 1.4 $ $Date: 2004/03/14 06:23:52 $ 42 */ 43 ![]()
...}
45
478
46 47 // ------------------------------------------------------------- Properties 48 49 50 /** 51 * A collection property where the elements of the collection are 52 * of type <code>LabelValueBean</code>. 53 */ 54 private Collection beanCollection = null; 55 ![]()
...}
![]()
...}
58
Vector entries =
new
Vector
(
10
);
73
59 60 entries. add ( new LabelValueBean (" Label 0", "Value 0" ) ); 61 entries. add ( new LabelValueBean (" Label 1", "Value 1" ) ); 62 entries. add ( new LabelValueBean (" Label 2", "Value 2" ) ); 63 entries. add ( new LabelValueBean (" Label 3", "Value 3" ) ); 64 entries. add ( new LabelValueBean (" Label 4", "Value 4" ) ); 65 entries. add ( new LabelValueBean (" Label 5", "Value 5" ) ); 66 entries. add ( new LabelValueBean (" Label 6", "Value 6" ) ); 67 entries. add ( new LabelValueBean (" Label 7", "Value 7" ) ); 68 entries. add ( new LabelValueBean (" Label 8", "Value 8" ) ); 69 entries. add ( new LabelValueBean (" Label 9", "Value 9" ) ); 70 71 beanCollection = entries; 72 } 74 return (beanCollection ); 75 } ![]()
...}
78 this.
beanCollection = beanCollection;
80
79 } 81 82 /** 83 * A multiple-String SELECT element using a bean collection. 84 */ 85 private String [ ] beanCollectionSelect = { "Value 1", "Value 3", 86 "Value 5" }; 87 ![]()
...}
89
return
(this.
beanCollectionSelect
);
91
90 } ![]()
...}
93 this.
beanCollectionSelect = beanCollectionSelect;
95
94 } 96 97 /** 98 * A boolean property whose initial value is true. 99 */ 100 private boolean booleanProperty = true; 101 ![]()
...}
103
return
(booleanProperty
);
105
104 } ![]()
...}
107 this.
booleanProperty = booleanProperty;
109
108 } 110 111 /** 112 * A multiple-String SELECT element using a collection. 113 */ 114 private String [ ] collectionSelect = { "Value 2", "Value 4", 115 "Value 6" }; 116 ![]()
...}
118
return
(this.
collectionSelect
);
120
119 } ![]()
...}
122 this.
collectionSelect = collectionSelect;
124
123 } 125 126 /** 127 * A double property. 128 */ 129 private double doubleProperty = 321. 0; 130 ![]()
...}
132
return
(this.
doubleProperty
);
134
133 } ![]()
...}
136 this.
doubleProperty = doubleProperty;
138
137 } 139 140 /** 141 * A boolean property whose initial value is false 142 */ 143 private boolean falseProperty = false; 144 ![]()
...}
146
return
(falseProperty
);
148
147 } ![]()
...}
150 this.
falseProperty = falseProperty;
152
151 } 153 154 /** 155 * A float property. 156 */ 157 private float floatProperty = (float ) 123. 0; 158 ![]()
...}
160
return
(this.
floatProperty
);
162
161 } ![]()
...}
164 this.
floatProperty = floatProperty;
166
165 } 167 168 /** 169 * Integer arrays that are accessed as an array as well as indexed. 170 */ 171 private int intArray [ ] = { 0, 10, 20, 30, 40 }; 172 ![]()
...}
174
return
(this.
intArray
);
176
175 } ![]()
...}
178 this.
intArray = intArray;
180
179 } 181 private int intIndexed [ ] = { 0, 10, 20, 30, 40 }; 182 ![]()
...}
184
return
(intIndexed
[index
]
);
186
185 } ![]()
...}
188 intIndexed
[index
] = value;
190
189 } 191 192 private int intMultibox [ ] = new int [ 0 ]; 193 ![]()
...}
195
return
(this.
intMultibox
);
197
196 } ![]()
...}
199 this.
intMultibox = intMultibox;
201
200 } 202 /** 203 * An integer property. 204 */ 205 private int intProperty = 123; 206 ![]()
...}
208
return
(this.
intProperty
);
210
209 } ![]()
...}
212 this.
intProperty = intProperty;
214
213 } 215 216 /** 217 * A long property. 218 */ 219 private long longProperty = 321; 220 ![]()
...}
222
return
(this.
longProperty
);
224
223 } ![]()
...}
226 this.
longProperty = longProperty;
228
227 } 229 230 /** 231 * A multiple-String SELECT element. 232 */ 233 private String [ ] multipleSelect = { "Multiple 3", "Multiple 5", 234 "Multiple 7" }; 235 ![]()
...}
237
return
(this.
multipleSelect
);
239
238 } ![]()
...}
241 this.
multipleSelect = multipleSelect;
243
242 } 244 245 /** 246 * A nested reference to another test bean (populated as needed). 247 */ 248 private TestBean nested = null; 249 ![]()
...}
251
if
(nested ==
null
)
255
252 nested = new TestBean ( ); 253 return (nested ); 254 } 256 257 /** 258 * A String property with an initial value of null. 259 */ 260 private String nullProperty = null; 261 ![]()
...}
263
return
(this.
nullProperty
);
265
264 } ![]()
...}
267 this.
nullProperty = nullProperty;
269
268 } 270 271 /** 272 * A short property. 273 */ 274 private short shortProperty = (short ) 987; 275 ![]()
...}
277
return
(this.
shortProperty
);
279
278 } ![]()
...}
281 this.
shortProperty = shortProperty;
283
282 } 284 285 /** 286 * A single-String value for a SELECT element. 287 */ 288 private String singleSelect = "Single 5"; 289 ![]()
...}
291
return
(this.
singleSelect
);
293
292 } ![]()
...}
295 this.
singleSelect = singleSelect;
297
296 } 298 299 /** 300 * String arrays that are accessed as an array as well as indexed. 301 */ 302 private String stringArray [ ] = 303 { " String 0", " String 1", " String 2", " String 3", " String 4" }; 304 ![]()
...}
306
return
(this.
stringArray
);
308
307 } ![]()
...}
310 this.
stringArray = stringArray;
312
311 } 313 private String stringIndexed [ ] = 314 { " String 0", " String 1", " String 2", " String 3", " String 4" }; 315 ![]()
...}
317
return
(stringIndexed
[index
]
);
319
318 } ![]()
...}
321 stringIndexed
[index
] = value;
323
322 } 324 325 private String stringMultibox [ ] = new String [ 0 ]; 326 ![]()
...}
328
return
(this.
stringMultibox
);
330
329 } ![]()
...}
332 this.
stringMultibox = stringMultibox;
334
333 } 335 /** 336 * A String property. 337 */ 338 private String stringProperty = "This is a string"; 339 ![]()
...}
341
return
(this.
stringProperty
);
343
342 } ![]()
...}
345 this.
stringProperty = stringProperty;
347
346 } 348 /** 349 * An empty String property. 350 */ 351 private String emptyStringProperty = ""; 352 ![]()
...}
354
return
(this.
emptyStringProperty
);
356
355 } ![]()
...}
358 this.
emptyStringProperty = emptyStringProperty;
360
359 } 361 362 /** 363 * A single-String value for a SELECT element based on resource strings. 364 */ 365 private String resourcesSelect = "Resources 2"; 366 ![]()
...}
368
return
(this.
resourcesSelect
);
370
369 } ![]()
...}
372 this.
resourcesSelect = resourcesSelect;
374
373 } 375 376 /** 377 * A property that allows a null value but is still used in a SELECT. 378 */ 379 private String withNulls = null; 380 ![]()
...}
382
return
(this.
withNulls
);
384
383 } ![]()
...}
386 this.
withNulls = withNulls;
388
387 } 389 390 /** 391 * A List property. 392 */ 393 private List listProperty = null; 394 ![]()
...}
![]()
...}
397 listProperty =
new
ArrayList
(
);
400
return listProperty;
398 listProperty. add ("dummy" ); 399 } 401 } ![]()
...}
404 this.
listProperty = listProperty;
406
405 } 407 /** 408 * An empty List property. 409 */ 410 private List emptyListProperty = null; 411 ![]()
...}
![]()
...}
414 emptyListProperty =
new
ArrayList
(
);
416
return emptyListProperty;
415 } 417 } ![]()
...}
420 this.
emptyListProperty = emptyListProperty;
422
421 } 423 424 /** 425 * A Map property. 426 */ 427 private Map mapProperty = null; 428 ![]()
...}
![]()
...}
431 mapProperty =
new
HashMap
(
);
434
return mapProperty;
432 mapProperty. put ("dummy", "dummy" ); 433 } 435 } ![]()
...}
438 this.
mapProperty = mapProperty;
440
439 } 441 /** 442 * An empty Map property. 443 */ 444 private Map emptyMapProperty = null; 445 ![]()
...}
![]()
...}
448 emptyMapProperty =
new
HashMap
(
);
450
return emptyMapProperty;
449 } 451 } ![]()
...}
454 this.
emptyMapProperty = emptyMapProperty;
456
455 } 457 458 // --------------------------------------------------------- Public Methods 459 460 461 /** 462 * Reset the properties that will be received as input. 463 */ ![]()
...}
465
475
466 booleanProperty = false; 467 collectionSelect = new String [ 0 ]; 468 intMultibox = new int [ 0 ]; 469 multipleSelect = new String [ 0 ]; 470 stringMultibox = new String [ 0 ]; 471 if (nested != null ) 472 nested. reset (mapping, request ); 473 474 } 476 477 } |