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 registers;

26 public List getRegisters() {

27 return registers;

28 }

29 publicvoid setRegisters(List 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

40

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

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

43

44

45

46

47

48

49

50

51 /success.jsp

52 /login.jsp

53

54

55

56

57 /success1.jsp

58 /login3.jsp

59

60

61

62 /success3.jsp

63 /login3.jsp

64

65

66

67



7.login.jsp 使用 struts2标签 和 OGNL 表达式

Html代码

68

69

70

    71

  • 用户名
  • 72

  • 密码
  • 73

  • 年龄
  • 74

  • 生日
  • 75

  • 地址
  • 76

77

78

79

80

    81

  • 82

    83

  • 84

  • 85

    86

  • 87

  • 88

    89

  • 90

  • 91

    92

  • 93

  • 94

    95

  • 96

97

98

99

100


8.success.jsp 循环遍历 list 集合

Html代码

101

102

    103

  • 用户名
  • 104

  • 密码
  • 105

  • 年龄
  • 106

  • 生日
  • 107

  • 地址
  • 108

109

110

111

    112

  • 113

    114

  • 115

  • 116

    117

  • 118

  • 119

    120

  • 121

  • 122

    123

  • 124

  • 125

    126

  • 127

128

129

130



10.使用 jstl c 标签 和 EL 表达式 实现上面的 批量注册 (注意 数组初始化)
long1.jsp

Html代码

131

132

133

    134

  • 用户名
  • 135

  • 密码
  • 136

  • 年龄
  • 137

  • 生日
  • 138

  • 地址
  • 139

140

141

142

143

    144

  • 145

    146

  • 147

  • 148

    149

  • 150

  • 151

    152

  • 153

  • 154

    155

  • 156

  • 157

    158

  • 159

160

161

162

163




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 registers=new LinkedHashSet();

175

176 public Set getRegisters() {

177 return registers;

178 }

179

180 publicvoid setRegisters(Set 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

194

195

    196

  • 用户名
  • 197

  • 密码
  • 198

  • 年龄
  • 199

  • 生日
  • 200

  • 地址
  • 201

202

203

204

205

    206

  • 207

    208

  • 209

  • 210

    211

  • 212

  • 213

    214

  • 215

  • 216

    217

  • 218

  • 219

    220

  • 221

222

223

224

225


15.success2.jsp 遍历 Set 和获取 单个

Html代码

226

227

    228

  • 用户名
  • 229

  • 密码
  • 230

  • 年龄
  • 231

  • 生日
  • 232

  • 地址
  • 233

234

235

===========遍历所有的=========

236

237

    238

  • 239

    240

  • 241

  • 242

    243

  • 244

  • 245

    246

  • 247

  • 248

    249

  • 250

  • 251

    252

  • 253

254

255

256

===========单独去其中的一个(知道其中的key wei admin)========

257

    258

  • 259

    260

  • 261

  • 262

    263

  • 264

  • 265

    266

  • 267

  • 268

    269

  • 270

  • 271

    272

  • 273

274



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 maps=new HashMap();

286

287 public Map getMaps() {

288 return maps;

289 }

290 publicvoid setMaps(Map 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

302

303

    304

  • 用户名
  • 305

  • 密码
  • 306

  • 年龄
  • 307

  • 生日
  • 308

  • 地址
  • 309

310

311

312

313

    314

  • 315

    316

  • 317

  • 318

    319

  • 320

  • 321

    322

  • 323

  • 324

    325

  • 326

  • 327

    328

  • 329

330

331

332

333


20 .success3.jsp 遍历 Map 和 取 单个

Html代码

334

335

    336

  • 用户名
  • 337

  • 密码
  • 338

  • 年龄
  • 339

  • 生日
  • 340

  • 地址
  • 341

342

343

===========遍历所有的=========

344

345

    346

  • 347

    348

  • 349

  • 350

    351

  • 352

  • 353

    354

  • 355

  • 356

    357

  • 358

  • 359

    360

  • 361

362

363

364

===========单独去其中的一个= (知道其中的key=0)========

365

    366

  • 367

    368

  • 369

  • 370

    371

  • 372

  • 373

    374

  • 375

  • 376

    377

  • 378

  • 379

    380

  • 381

382


事例:

Jsp:

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

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

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

'>

id='edit___cfg_basehost'value="

"