Jsp向action傳list set或map

Jsp向action傳list set或map

List
1.使用Strut2的的集合對象:在jsp初始化action中的list然後提交到action
2.使用Struts標籤,實現多個用戶同時註冊(注意屬性配置文件)
3.pojo

Java代碼

1 package com.sh.pojo;

2

3 import java.util.Date;

4

5 publicclass Register {

6 private String name;

7 private String pwd;

8 privateint age;

9 private Date birthday;

10 private String address;

11

12 //get set

13 }


4.action

Java代碼

14 package com.sh.action;

15

16 import java.util.ArrayList;

17 import java.util.List;

18

19 import com.opensymphony.xwork2.ActionSupport;

20 import com.sh.pojo.Register;

21

22 publicclass RegisterAction extends ActionSupport {

23

24 privatestaticfinallong serialVersionUID = 1L;

25 private List<Register> registers;

26 public List<Register> getRegisters() {

27 return registers;

28 }

29 publicvoid setRegisters(List<Register> registers) {

30 this.registers = registers;

31 }

32 public String execute() throws Exception {

33 return SUCCESS;

34 }

35

36 }


5.RegisterAction-conversion.properties(配置action中list的泛型對象,放在action同一目錄下,屬性文件的命名爲:actionName-version.properties)

Java代碼

37 Element_registers=com.sh.pojo.Register

38 CreateIfNull_sysConfigs = true

//Element_是固定的後面接action中的list集合變量名,後面是泛型中的對象類。


6.struts.xml

Xml代碼

39 <?xml version="1.0" encoding="UTF-8" ?>

40 <!DOCTYPE struts PUBLIC

41 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

42 "http://struts.apache.org/dtds/struts-2.3.dtd">

43

44 <struts>

45 <!-- 定義國際化資源文件的基本名稱 -->

46 <constant name="struts.i18n.encoding" value="utf-8"/>

47 <package name="default" extends="struts-default">

48

49 <!-- 使用list集合 -->

50 <action name="registerAction" class="com.sh.action.RegisterAction">

51 <result name="success">/success.jsp</result>

52 <result name="input">/login.jsp</result>

53 </action>

54

55 <!-- 使用Set集合 -->

56 <action name="registerSetAction" class="com.sh.action.RegisterSetAction">

57 <result name="success">/success1.jsp</result>

58 <result name="input">/login3.jsp</result>

59 </action>

60 <!-- 使用 HashMap -->

61 <action name="registerHashMapAction" class="com.sh.action.RegisterHashMapAction">

62 <result name="success">/success3.jsp</result>

63 <result name="input">/login3.jsp</result>

64 </action>

65

66 </package>

67 </struts>



7.login.jsp 使用 struts2標籤 和 OGNL 表達式

Html代碼

68 <body>

69 <s:form action="registerAction" method="post" theme="simple">

70 <ul style="list-style:none; text-align: center;">

71 <li style="float: left;width: 155px">用戶名</li>

72 <li style="float: left;width: 155px">密碼</li>

73 <li style="float: left;width: 155px">年齡</li>

74 <li style="float: left;width: 155px">生日</li>

75 <li style="float: left;width: 155px">地址</li>

76 </ul>

77 <div style="clear: both;"></div>

78 <!-- 手動聲明一個 new int[4] 長度爲4 的int 類型的數組-->

79 <s:iterator value="new int[4]" status="st">

80 <ul style="list-style:none;">

81 <li style="float: left">

82 <s:textfield name="%{'registers['+#st.index+'].name'}" label="用戶名"/>

83 </li>

84 <li style="float: left">

85 <s:password name="%{'registers['+#st.index+'].pwd'}" label="密碼"/>

86 </li>

87 <li style="float: left">

88 <s:textfield name="%{'registers['+#st.index+'].age'}" label="年齡"/>

89 </li>

90 <li style="float: left">

91 <s:textfield name="%{'registers['+#st.index+'].birthday'}" HH:mm:ss'});" label="生日"/>

92 </li>

93 <li>

94 <s:textfield name="%{'registers['+#st.index+'].address'}" label="地址"/>

95 </li>

96 </ul>

97 </s:iterator>

98 <div><s:submit value="submit"/></div>

99 </s:form>

100 </body>


8.success.jsp 循環遍歷 list 集合

Html代碼

101 <body>

102 <ul style="list-style:none; text-align: center;">

103 <li style="float: left;width: 155px">用戶名</li>

104 <li style="float: left;width: 155px">密碼</li>

105 <li style="float: left;width: 155px">年齡</li>

106 <li style="float: left;width: 155px">生日</li>

107 <li style="float: left;width: 155px">地址</li>

108 </ul>

109 <div style="clear: both;"></div>

110 <s:iterator value="registers" status="st">

111 <ul style="list-style:none;">

112 <li style="float: left;width: 155px;">

113 <s:property value="name"/>

114 </li>

115 <li style="float: left;width: 155px;">

116 <s:property value="pwd"/>

117 </li>

118 <li style="float: left;width: 155px;">

119 <s:property value="age"/>

120 </li>

121 <li style="float: left;width: 155px;">

122 <s:property value="birthday"/>

123 </li>

124 <li>

125 <s:property value="address"/>

126 </li>

127 </ul>

128 <div></div>

129 </s:iterator>

130 </body>



10.使用 jstl c 標籤 和 EL 表達式 實現上面的 批量註冊 (注意 數組初始化)
long1.jsp

Html代碼

131 <body>

132 <form action="${pageContext.request.contextPath}/registerAction.action" method="post">

133 <ul style="list-style:none; text-align: center;">

134 <li style="float: left;width: 155px">用戶名</li>

135 <li style="float: left;width: 155px">密碼</li>

136 <li style="float: left;width: 155px">年齡</li>

137 <li style="float: left;width: 155px">生日</li>

138 <li style="float: left;width: 155px">地址</li>

139 </ul>

140 <div style="clear: both;"></div>

141 <!--注意這裏 聲明的時候和上面的不一樣 new int[4] c標籤識別不出來 ,識別的只有一個元素-->

142 <c:forEach items="new int[]{0,0,0,0}" varStatus="st">

143 <ul style="list-style:none;">

144 <li style="float: left">

145 <input name="registers[${st.index}].name"/>

146 </li>

147 <li style="float: left">

148 <input name="registers[${st.index}].pwd" />

149 </li>

150 <li style="float: left">

151 <input name="registers[${st.index}].age"/>

152 </li>

153 <li style="float: left">

154 <input name="registers[${st.index}].birthday" HH:mm:ss'});"/>

155 </li>

156 <li>

157 <input name="registers[${st.index}].address" />

158 </li>

159 </ul>

160 </c:forEach>

161 <div><input type="submit"/></div>

162 </form>

163 </body>




Set
12.使用Strutgs2的 Set 類型. 遍歷所有 和 取其中一個
action

Java代碼

164 package com.sh.action;

165

166 import java.util.LinkedHashSet;

167 import java.util.Set;

168

169 import com.opensymphony.xwork2.ActionSupport;

170 import com.sh.pojo.Register;

171

172 publicclass RegisterSetAction extends ActionSupport {

173

174 private Set<Register> registers=new LinkedHashSet<Register>();

175

176 public Set<Register> getRegisters() {

177 return registers;

178 }

179

180 publicvoid setRegisters(Set<Register> registers) {

181 this.registers = registers;

182 }

183

184 @Override

185 public String execute() throws Exception {

186 // TODO Auto-generated method stub

187 return SUCCESS;

188 }

189

190 }


13.RegisterSetAction-conversion.properties

Java代碼

191 KeyProperty_registers=name //KeyProperty 如果是取 單個 就需要這個

192 Element_registers=com.sh.pojo.Register


14.login3.jsp (注意 初始化 set 的時候 採用 makeNew[] )

Html代碼

193 <body>

194 <s:form action="registerSetAction" method="post" theme="simple">

195 <ul style="list-style:none; text-align: center;">

196 <li style="float: left;width: 155px">用戶名</li>

197 <li style="float: left;width: 155px">密碼</li>

198 <li style="float: left;width: 155px">年齡</li>

199 <li style="float: left;width: 155px">生日</li>

200 <li style="float: left;width: 155px">地址</li>

201 </ul>

202 <div style="clear: both;"></div>

203 <!-- 注意 使用了makeNew[] -->

204 <s:iterator value="new int[4]" status="st">

205 <ul style="list-style:none;">

206 <li style="float: left">

207 <s:textfield name="%{'registers.makeNew['+#st.index+'].name'}" label="用戶名"/>

208 </li>

209 <li style="float: left">

210 <s:password name="%{'registers.makeNew['+#st.index+'].pwd'}" label="密碼"/>

211 </li>

212 <li style="float: left">

213 <s:textfield name="%{'registers.makeNew['+#st.index+'].age'}" label="年齡"/>

214 </li>

215 <li style="float: left">

216 <s:textfield name="%{'registers.makeNew['+#st.index+'].birthday'}" HH:mm:ss'});" label="生日"/>

217 </li>

218 <li>

219 <s:textfield name="%{'registers.makeNew['+#st.index+'].address'}" label="地址"/>

220 </li>

221 </ul>

222 </s:iterator>

223 <div><s:submit value="submit"/></div>

224 </s:form>

225 </body>


15.success2.jsp 遍歷 Set 和獲取 單個

Html代碼

226 <body>

227 <ul style="list-style:none; text-align: center;">

228 <li style="float: left;width: 155px">用戶名</li>

229 <li style="float: left;width: 155px">密碼</li>

230 <li style="float: left;width: 155px">年齡</li>

231 <li style="float: left;width: 155px">生日</li>

232 <li style="float: left;width: 155px">地址</li>

233 </ul>

234 <div style="clear: both;"></div>

235 <div>===========遍歷所有的=========</div>

236 <s:iterator value="registers" status="st">

237 <ul style="list-style:none;">

238 <li style="float: left;width: 155px;">

239 <s:property value="name"/>

240 </li>

241 <li style="float: left;width: 155px;">

242 <s:property value="pwd"/>

243 </li>

244 <li style="float: left;width: 155px;">

245 <s:property value="age"/>

246 </li>

247 <li style="float: left;width: 155px;">

248 <s:property value="birthday"/>

249 </li>

250 <li>

251 <s:property value="address"/>

252 </li>

253 </ul>

254 <div></div>

255 </s:iterator>

256 <div>===========單獨去其中的一個(知道其中的key wei admin)========</div>

257 <ul style="list-style:none;">

258 <li style="float: left;width: 155px;">

259 <s:property value="registers('admin').name"/>

260 </li>

261 <li style="float: left;width: 155px;">

262 <s:property value="registers('admin').pwd"/>

263 </li>

264 <li style="float: left;width: 155px;">

265 <s:property value="registers('admin').age"/>

266 </li>

267 <li style="float: left;width: 155px;">

268 <s:property value="registers('admin').birthday"/>

269 </li>

270 <li>

271 <s:property value="registers('admin').address"/>

272 </li>

273 </ul>

274 </body>



Map
17.使用 Strut2的 Map 類型
action

Java代碼

275 package com.sh.action;

276

277 import java.util.HashMap;

278 import java.util.Map;

279

280 import com.opensymphony.xwork2.ActionSupport;

281 import com.sh.pojo.Register;

282

283 publicclass RegisterHashMapAction extends ActionSupport {

284

285 private Map<String,Register> maps=new HashMap<String, Register>();

286

287 public Map<String, Register> getMaps() {

288 return maps;

289 }

290 publicvoid setMaps(Map<String, Register> maps) {

291 this.maps = maps;

292 }

293 @Override

294 public String execute() throws Exception {

295 // TODO Auto-generated method stub

296 return SUCCESS;

297 }

298 }


18.屬性配置文件 RegisterHashMapAction-conversion.properties

Java代碼

299 Key_maps=java.lang.String // Key_ 固定 後面爲action的Map屬性名

300 Element_maps=com.sh.pojo.Register


19.login5.jsp

Html代碼

301 <body>

302 <s:form action="registerHashMapAction" method="post" theme="simple">

303 <ul style="list-style:none; text-align: center;">

304 <li style="float: left;width: 155px">用戶名</li>

305 <li style="float: left;width: 155px">密碼</li>

306 <li style="float: left;width: 155px">年齡</li>

307 <li style="float: left;width: 155px">生日</li>

308 <li style="float: left;width: 155px">地址</li>

309 </ul>

310 <div style="clear: both;"></div>

311 <!-- 注意 【key】 中key 的取值類型和 配置文件中一直-->

312 <s:iterator value="new int[4]" status="st">

313 <ul style="list-style:none;">

314 <li style="float: left">

315 <s:textfield name="%{'maps['+#st.index+'].name'}" label="用戶名"/>

316 </li>

317 <li style="float: left">

318 <s:password name="%{'maps['+#st.index+'].pwd'}" label="密碼"/>

319 </li>

320 <li style="float: left">

321 <s:textfield name="%{'maps['+#st.index+'].age'}" label="年齡"/>

322 </li>

323 <li style="float: left">

324 <s:textfield name="%{'maps['+#st.index+'].birthday'}" HH:mm:ss'});" label="生日"/>

325 </li>

326 <li>

327 <s:textfield name="%{'maps['+#st.index+'].address'}" label="地址"/>

328 </li>

329 </ul>

330 </s:iterator>

331 <div><s:submit value="submit"/></div>

332 </s:form>

333 </body>


20 .success3.jsp 遍歷 Map 和 取 單個

Html代碼

334 <body>

335 <ul style="list-style:none; text-align: center;">

336 <li style="float: left;width: 155px">用戶名</li>

337 <li style="float: left;width: 155px">密碼</li>

338 <li style="float: left;width: 155px">年齡</li>

339 <li style="float: left;width: 155px">生日</li>

340 <li style="float: left;width: 155px">地址</li>

341 </ul>

342 <div style="clear: both;"></div>

343 <div>===========遍歷所有的=========</div>

344 <s:iterator value="maps" status="st">

345 <ul style="list-style:none;">

346 <li style="float: left;width: 155px;">

347 <s:property value="value.name"/>

348 </li>

349 <li style="float: left;width: 155px;">

350 <s:property value="value.pwd"/>

351 </li>

352 <li style="float: left;width: 155px;">

353 <s:property value="value.age"/>

354 </li>

355 <li style="float: left;width: 155px;">

356 <s:property value="value.birthday"/>

357 </li>

358 <li>

359 <s:property value="value.address"/>

360 </li>

361 </ul>

362 <div></div>

363 </s:iterator>

364 <div>===========單獨去其中的一個= (知道其中的key=0)========</div>

365 <ul style="list-style:none;">

366 <li style="float: left;width: 155px;">

367 <s:property value="maps['0'].name"/>

368 </li>

369 <li style="float: left;width: 155px;">

370 <s:property value="maps['0'].pwd"/>

371 </li>

372 <li style="float: left;width: 155px;">

373 <s:property value="maps['0'].age"/>

374 </li>

375 <li style="float: left;width: 155px;">

376 <s:property value="maps['0'].birthday"/>

377 </li>

378 <li>

379 <s:property value="maps['0'].address"/>

380 </li>

381 </ul>

382 </body>


事例:

Jsp:

<s:iterator value="sysConfigs" status="stat">

<s:hidden name="sysConfigs[%{#stat.index}].id"

value="%{sysConfigs[#stat.index].id}" />

<s:hidden name="sysConfigs[%{#stat.index}].name"

value="%{sysConfigs[#stat.index].name}" />

<s:hidden name="sysConfigs[%{#stat.index}].info"

value="%{sysConfigs[#stat.index].info}" />

<labelname='sysConfigs[%{#stat.index}].info"/>'>

<s:propertyvalue="sysConfigs[#stat.index].info" />

</label>

<input type='text'name="sysConfigs[${stat.index }].value"

id='edit___cfg_basehost'value="

<s:propertyvalue='sysConfigs[#stat.index].value' />"

</s:iterator>










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章