---
title: "Struts1.2.4学习心得!(四)"
author: "fanjava"
date: "2004-09-25T08:42:00Z"
tags: []
slug: "struts1-2-4"
draft: false
lastmod: "2004-09-25T08:42:00Z"
id: 601890
url: "https://fanjava.iteye.com/blog/601890"
---

第四次学习:

这次学习碰到了与struts1.1很不同的地方,就是ActionForm类不仅仅是String型,可以是任意数据类型了.

1.表单录入(含bean的嵌套).

设有一个testBean类,这个类中又含有一个nested属性(nested为一个类的实例),当要为nested类的booleanProperty属性设定值时用nested.booleanProperty指定位置.下面例子录入数据时,数据填充到testBean和nested中去.

**java代码:**

1  
2&lt;%@ taglib uri="/tags/struts-bean" prefix="bean" %&gt;  
3&lt;%@ taglib uri="/tags/struts-html" prefix="html" %&gt;  
4&lt;%@ taglib uri="/tags/struts-logic" prefix="logic" %&gt;  
5&lt;html:html&gt;  
6 &lt;head&gt;  
7 &lt;title&gt;Test struts-html Property Setters&lt;/title&gt;  
8 &lt;/head&gt;  
9 &lt;body&gt;  
10 &lt;div align="center"&gt;  
11 &lt;h1&gt;Test struts-html Property Setters&lt;/h1&gt;  
12 &lt;/div&gt;  
13 &lt;p&gt;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.&lt;/p&gt;  
14 &lt;html:form action="/html-setters-submit"&gt;  
15 &lt;table border="0" width="100%"&gt;  
16 &lt;tr&gt;  
17 &lt;th align="center" colspan="4"&gt;Scalar Properties&lt;/th&gt;  
18 &lt;/tr&gt;  
19 &lt;tr&gt;  
20 &lt;th align="right"&gt;booleanProperty&lt;/th&gt;  
21 &lt;td align="left"&gt;  
22 &lt;html:checkbox property="booleanProperty" /&gt;  
23 &lt;/td&gt;  
24 &lt;th align="right"&gt;nested.booleanProperty&lt;/th&gt;  
25 &lt;td align="left"&gt;  
26 &lt;html:checkbox property="nested.booleanProperty" /&gt;  
27 &lt;/td&gt;  
28 &lt;/tr&gt;  
29 &lt;tr&gt;  
30 &lt;th align="right"&gt;doubleProperty&lt;/th&gt;  
31 &lt;td align="left"&gt;  
32 &lt;html:text property="doubleProperty" size="32" /&gt;  
33 &lt;/td&gt;  
34 &lt;th align="right"&gt;nested.doubleProperty&lt;/th&gt;  
35 &lt;td align="left"&gt;  
36 &lt;html:text property="nested.doubleProperty" size="32" /&gt;  
37 &lt;/td&gt;  
38 &lt;/tr&gt;  
39 &lt;tr&gt;  
40 &lt;th align="right"&gt;floatProperty&lt;/th&gt;  
41 &lt;td align="left"&gt;  
42 &lt;html:text property="floatProperty" size="32" /&gt;  
43 &lt;/td&gt;  
44 &lt;th align="right"&gt;nested.floatProperty&lt;/th&gt;  
45 &lt;td align="left"&gt;  
46 &lt;html:text property="nested.floatProperty" size="32" /&gt;  
47 &lt;/td&gt;  
48 &lt;/tr&gt;  
49 &lt;tr&gt;  
50 &lt;th align="right"&gt;intProperty&lt;/th&gt;  
51 &lt;td align="left"&gt;  
52 &lt;html:text property="intProperty" size="32" /&gt;  
53 &lt;/td&gt;  
54 &lt;th align="right"&gt;nested.intProperty&lt;/th&gt;  
55 &lt;td align="left"&gt;  
56 &lt;html:text property="nested.intProperty" size="32" /&gt;  
57 &lt;/td&gt;  
58 &lt;/tr&gt;  
59 &lt;tr&gt;  
60 &lt;th align="right"&gt;longProperty&lt;/th&gt;  
61 &lt;td align="left"&gt;  
62 &lt;html:text property="longProperty" size="32" /&gt;  
63 &lt;/td&gt;  
64 &lt;th align="right"&gt;nested.longProperty&lt;/th&gt;  
65 &lt;td align="left"&gt;  
66 &lt;html:text property="nested.longProperty" size="32" /&gt;  
67 &lt;/td&gt;  
68 &lt;/tr&gt;  
69 &lt;tr&gt;  
70 &lt;th align="right"&gt;stringProperty&lt;/th&gt;  
71 &lt;td align="left"&gt;  
72 &lt;html:text property="stringProperty" size="32" /&gt;  
73 &lt;/td&gt;  
74 &lt;th align="right"&gt;nested.stringProperty&lt;/th&gt;  
75 &lt;td align="left"&gt;  
76 &lt;html:text property="nested.stringProperty" size="32" /&gt;  
77 &lt;/td&gt;  
78 &lt;/tr&gt;  
79 &lt;tr&gt;  
80 &lt;th align="center" colspan="4"&gt;Indexed Properties&lt;/th&gt;  
81 &lt;/tr&gt;  
82 &lt;tr&gt;  
83 &lt;th align="right"&gt;intIndexed\[0]&lt;/th&gt;  
84 &lt;td align="left"&gt;  
85 &lt;html:text property="intIndexed\[0]" size="32" /&gt;  
86 &lt;/td&gt;  
87 &lt;th align="right"&gt;nested.intIndexed\[0]&lt;/th&gt;  
88 &lt;td align="left"&gt;  
89 &lt;html:text property="nested.intIndexed\[0]" size="32" /&gt;  
90 &lt;/td&gt;  
91 &lt;/tr&gt;  
92 &lt;tr&gt;  
93 &lt;th align="right"&gt;intIndexed\[1]&lt;/th&gt;  
94 &lt;td align="left"&gt;  
95 &lt;html:text property="intIndexed\[1]" size="32" /&gt;  
96 &lt;/td&gt;  
97 &lt;th align="right"&gt;nested.intIndexed\[1]&lt;/th&gt;  
98 &lt;td align="left"&gt;  
99 &lt;html:text property="nested.intIndexed\[1]" size="32" /&gt;  
100 &lt;/td&gt;  
101 &lt;/tr&gt;  
102 &lt;tr&gt;  
103 &lt;th align="right"&gt;stringIndexed\[0]&lt;/th&gt;  
104 &lt;td align="left"&gt;  
105 &lt;html:text property="stringIndexed\[0]" size="32" /&gt;  
106 &lt;/td&gt;  
107 &lt;th align="right"&gt;nested.stringIndexed\[0]&lt;/th&gt;  
108 &lt;td align="left"&gt;  
109 &lt;html:text property="nested.stringIndexed\[0]" size="32" /&gt;  
110 &lt;/td&gt;  
111 &lt;/tr&gt;  
112 &lt;tr&gt;  
113 &lt;th align="right"&gt;stringIndexed\[1]&lt;/th&gt;  
114 &lt;td align="left"&gt;  
115 &lt;html:text property="stringIndexed\[1]" size="32" /&gt;  
116 &lt;/td&gt;  
117 &lt;th align="right"&gt;nested.stringIndexed\[1]&lt;/th&gt;  
118 &lt;td align="left"&gt;  
119 &lt;html:text property="nested.stringIndexed\[1]" size="32" /&gt;  
120 &lt;/td&gt;  
121 &lt;/tr&gt;  
122 &lt;tr&gt;  
123 &lt;td&gt;&lt;/td&gt;  
124 &lt;td align="right"&gt;  
125 &lt;html:submit&gt;Save&lt;/html:submit&gt;  
126 &lt;/td&gt;  
127 &lt;td align="left"&gt;  
128 &lt;html:reset&gt;Reset&lt;/html:reset&gt;  
129 &lt;html:cancel&gt;Cancel&lt;/html:cancel&gt;  
130 &lt;/td&gt;  
131 &lt;td&gt;&lt;/td&gt;  
132 &lt;/tr&gt;  
133 &lt;/table&gt;  
134 &lt;/html:form&gt;  
135 &lt;/body&gt;  
136&lt;/html:html&gt;  
137  
138

对应的TestBean

**java代码:![](http://forum.iteye.com/copy.gif)**

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  
23package org.apache.struts.webapp.exercise;  
24  
25  
26import java.util.ArrayList;  
27import java.util.Collection;  
28import java.util.HashMap;  
29import java.util.List;  
30import java.util.Map;  
31import java.util.Vector;  
32import javax.servlet.http.HttpServletRequest;  
33import org.apache.struts.action.ActionForm;  
34import org.apache.struts.action.ActionMapping;  
35import 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  
![](http://forum.iteye.com/code.gif)44public class TestBean extends ActionForm {

...}

45  
46  
47 // ------------------------------------------------------------- Properties  
48  
49  
50 /\**  
51 * A collection property where the elements of the collection are  
52 * of type &lt;code&gt;LabelValueBean&lt;/code&gt;.  
53 \*/  
54 private Collection beanCollection = null;  
55  
![](http://forum.iteye.com/code.gif)56 public Collection getBeanCollection() {

...}

![](http://forum.iteye.com/code.gif)57 if (beanCollection == null) {

...}

58 Vector entries = new Vector(10);  
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 }

73  
74 return (beanCollection);  
75 }

76  
![](http://forum.iteye.com/code.gif)77 public void setBeanCollection(Collection beanCollection) {

...}

78 this.beanCollection = beanCollection;  
79 }

80  
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  
![](http://forum.iteye.com/code.gif)88 public String\[] getBeanCollectionSelect() {

...}

89 return (this.beanCollectionSelect);  
90 }

91  
![](http://forum.iteye.com/code.gif)92 public void setBeanCollectionSelect(String beanCollectionSelect\[]) {

...}

93 this.beanCollectionSelect = beanCollectionSelect;  
94 }

95  
96  
97 /\**  
98 * A boolean property whose initial value is true.  
99 \*/  
100 private boolean booleanProperty = true;  
101  
![](http://forum.iteye.com/code.gif)102 public boolean getBooleanProperty() {

...}

103 return (booleanProperty);  
104 }

105  
![](http://forum.iteye.com/code.gif)106 public void setBooleanProperty(boolean booleanProperty) {

...}

107 this.booleanProperty = booleanProperty;  
108 }

109  
110  
111 /\**  
112 * A multiple-String SELECT element using a collection.  
113 \*/  
114 private String\[] collectionSelect = { "Value 2", "Value 4",  
115 "Value 6" };  
116  
![](http://forum.iteye.com/code.gif)117 public String\[] getCollectionSelect() {

...}

118 return (this.collectionSelect);  
119 }

120  
![](http://forum.iteye.com/code.gif)121 public void setCollectionSelect(String collectionSelect\[]) {

...}

122 this.collectionSelect = collectionSelect;  
123 }

124  
125  
126 /\**  
127 * A double property.  
128 \*/  
129 private double doubleProperty = 321.0;  
130  
![](http://forum.iteye.com/code.gif)131 public double getDoubleProperty() {

...}

132 return (this.doubleProperty);  
133 }

134  
![](http://forum.iteye.com/code.gif)135 public void setDoubleProperty(double doubleProperty) {

...}

136 this.doubleProperty = doubleProperty;  
137 }

138  
139  
140 /\**  
141 * A boolean property whose initial value is false  
142 \*/  
143 private boolean falseProperty = false;  
144  
![](http://forum.iteye.com/code.gif)145 public boolean getFalseProperty() {

...}

146 return (falseProperty);  
147 }

148  
![](http://forum.iteye.com/code.gif)149 public void setFalseProperty(boolean falseProperty) {

...}

150 this.falseProperty = falseProperty;  
151 }

152  
153  
154 /\**  
155 * A float property.  
156 \*/  
157 private float floatProperty = (float) 123.0;  
158  
![](http://forum.iteye.com/code.gif)159 public float getFloatProperty() {

...}

160 return (this.floatProperty);  
161 }

162  
![](http://forum.iteye.com/code.gif)163 public void setFloatProperty(float floatProperty) {

...}

164 this.floatProperty = floatProperty;  
165 }

166  
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  
![](http://forum.iteye.com/code.gif)173 public int\[] getIntArray() {

...}

174 return (this.intArray);  
175 }

176  
![](http://forum.iteye.com/code.gif)177 public void setIntArray(int intArray\[]) {

...}

178 this.intArray = intArray;  
179 }

180  
181 private int intIndexed\[] = { 0, 10, 20, 30, 40 };  
182  
![](http://forum.iteye.com/code.gif)183 public int getIntIndexed(int index) {

...}

184 return (intIndexed\[index]);  
185 }

186  
![](http://forum.iteye.com/code.gif)187 public void setIntIndexed(int index, int value) {

...}

188 intIndexed\[index] = value;  
189 }

190  
191  
192 private int intMultibox\[] = new int\[0];  
193  
![](http://forum.iteye.com/code.gif)194 public int\[] getIntMultibox() {

...}

195 return (this.intMultibox);  
196 }

197  
![](http://forum.iteye.com/code.gif)198 public void setIntMultibox(int intMultibox\[]) {

...}

199 this.intMultibox = intMultibox;  
200 }

201  
202 /\**  
203 * An integer property.  
204 \*/  
205 private int intProperty = 123;  
206  
![](http://forum.iteye.com/code.gif)207 public int getIntProperty() {

...}

208 return (this.intProperty);  
209 }

210  
![](http://forum.iteye.com/code.gif)211 public void setIntProperty(