`
Luob.
  • 浏览: 1570905 次
  • 来自: 上海
社区版块
存档分类
最新评论

Struts2 JSP中将list,set ,Map传递到Action然后<s:iterator>遍历(三十五)

阅读更多
---------------List-----------------
1.使用Strut2的的集合对象:在jsp初始化action中的list然后提交到action

2.使用Struts标签,实现多个用户同时注册(注意属性配置文件)

3.pojo
package com.sh.pojo;

import java.util.Date;

public class Register {
private String name;
private String pwd;
private int age;
private Date birthday;
private String address;

//get set
}


4.action
package com.sh.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.sh.pojo.Register;

public class RegisterAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private List<Register> registers;
	public List<Register> getRegisters() {
		return registers;
	}
	public void setRegisters(List<Register> registers) {
		this.registers = registers;
	}
	public String execute() throws Exception {
		return SUCCESS;
	}
	
}


5.RegisterAction-conversion.properties(配置action中list的泛型对象,放在action同一目录下,属性文件的命名为:actionName-version.properties)
Element_registers=com.sh.pojo.Register //Element_是固定的后面接action中的list集合变量名,后面是泛型中的对象类。


6.struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts> 
     <!-- 定义国际化资源文件的基本名称 -->
	<constant name="struts.i18n.encoding" value="utf-8"/>
    <package name="default" extends="struts-default">
    
        <!-- 使用list集合 -->
    	<action name="registerAction" class="com.sh.action.RegisterAction">
			<result name="success">/success.jsp</result>  
			<result name="input">/login.jsp</result>     	
    	</action>
    	
    	<!-- 使用Set集合 -->
    	<action name="registerSetAction" class="com.sh.action.RegisterSetAction">
			<result name="success">/success1.jsp</result>  
			<result name="input">/login3.jsp</result>     	
    	</action>
    	<!-- 使用 HashMap -->
    	<action name="registerHashMapAction" class="com.sh.action.RegisterHashMapAction">
			<result name="success">/success3.jsp</result>  
			<result name="input">/login3.jsp</result>     	
    	</action>
    	
    </package>
</struts>


7.login.jsp  使用 struts2标签 和 OGNL 表达式
<body>
	<s:form action="registerAction" method="post" theme="simple">
		<ul style="list-style:none; text-align: center;">
		  	<li style="float: left;width: 155px">用户名</li>
		  	<li style="float: left;width: 155px">密码</li>
		  	<li style="float: left;width: 155px">年龄</li>
		  	<li style="float: left;width: 155px">生日</li>
		  	<li style="float: left;width: 155px">地址</li>
		  </ul>
		  <div style="clear: both;"></div>
<!-- 手动声明一个 new int[4] 长度为4 的int 类型的数组-->
		<s:iterator value="new int[4]" status="st">
		  <ul style="list-style:none;">
		  	<li style="float: left">
		  		<s:textfield name="%{'registers['+#st.index+'].name'}" label="用户名"/>
		  	</li>
		  	<li style="float: left">
		  		<s:password name="%{'registers['+#st.index+'].pwd'}" label="密码"/>
		  	</li>
		  	<li style="float: left">
		  		<s:textfield name="%{'registers['+#st.index+'].age'}" label="年龄"/>
		  	</li>
		  	<li style="float: left">
		  		<s:textfield name="%{'registers['+#st.index+'].birthday'}" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'});" label="生日"/>
		  	</li>
		  	<li>
		  		<s:textfield name="%{'registers['+#st.index+'].address'}" label="地址"/>
		  	</li>
		  </ul>
		</s:iterator>
	   <div><s:submit value="submit"/></div>
	</s:form>
  </body>


8.success.jsp 循环遍历 list 集合
<body>
  <ul style="list-style:none; text-align: center;">
		  	<li style="float: left;width: 155px">用户名</li>
		  	<li style="float: left;width: 155px">密码</li>
		  	<li style="float: left;width: 155px">年龄</li>
		  	<li style="float: left;width: 155px">生日</li>
		  	<li style="float: left;width: 155px">地址</li>
		  </ul>
		  <div style="clear: both;"></div>
    <s:iterator value="registers" status="st">
   		 <ul style="list-style:none;">
		  	<li style="float: left;width: 155px;">
		  		<s:property value="name"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="pwd"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="age"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="birthday"/>
		  	</li>
		  	<li>
		  		<s:property value="address"/>
		  	</li>
		  </ul>
		  <div></div>
    </s:iterator>
  </body>


9.访问
--localhost:8080/Struts2_CollectConversion/login.jsp
填上信息后提交就可以看到成功页面的循环的输出

10.使用 jstl c 标签 和  EL 表达式 实现上面的 批量注册 (注意 数组初始化)
long1.jsp
<body>
	<form action="${pageContext.request.contextPath}/registerAction.action" method="post">
		<ul style="list-style:none; text-align: center;">
		  	<li style="float: left;width: 155px">用户名</li>
		  	<li style="float: left;width: 155px">密码</li>
		  	<li style="float: left;width: 155px">年龄</li>
		  	<li style="float: left;width: 155px">生日</li>
		  	<li style="float: left;width: 155px">地址</li>
		  </ul>
		  <div style="clear: both;"></div>
<!--注意这里 声明的时候和上面的不一样 new int[4] c标签识别不出来 ,识别的只有一个元素-->
		<c:forEach items="new int[]{0,0,0,0}" varStatus="st">
		  <ul style="list-style:none;">
		  	<li style="float: left">
		  		<input name="registers[${st.index}].name"/>
		  	</li>
		  	<li style="float: left">
		  		<input name="registers[${st.index}].pwd" />
		  	</li>
		  	<li style="float: left">
		  		<input name="registers[${st.index}].age"/>
		  	</li>
		  	<li style="float: left">
		  		<input name="registers[${st.index}].birthday" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'});"/>
		  	</li>
		  	<li>
		  		<input name="registers[${st.index}].address" />
		  	</li>
		  </ul>
		</c:forEach>
	   <div><input type="submit"/></div>
	</form>
  </body>


11.访问

--localhost:8080/Struts2_CollectConversion/login1.jsp
填上信息后和上面的一样。


---------------Set----------------
12.使用Strutgs2的 Set 类型. 遍历所有 和 取其中一个
action
package com.sh.action;

import java.util.LinkedHashSet;
import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;
import com.sh.pojo.Register;

public class RegisterSetAction extends ActionSupport {

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

	public Set<Register> getRegisters() {
		return registers;
	}

	public void setRegisters(Set<Register> registers) {
		this.registers = registers;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}	
	
}



13.RegisterSetAction-conversion.properties
KeyProperty_registers=name  //KeyProperty 如果是取 单个 就需要这个
Element_registers=com.sh.pojo.Register 


14.login3.jsp  (注意 初始化 set 的时候 采用 makeNew[] )
<body>
	<s:form action="registerSetAction" method="post" theme="simple">
		<ul style="list-style:none; text-align: center;">
		  	<li style="float: left;width: 155px">用户名</li>
		  	<li style="float: left;width: 155px">密码</li>
		  	<li style="float: left;width: 155px">年龄</li>
		  	<li style="float: left;width: 155px">生日</li>
		  	<li style="float: left;width: 155px">地址</li>
		  </ul>
		  <div style="clear: both;"></div>
		  <!-- 注意 使用了makeNew[] -->
		<s:iterator value="new int[4]" status="st">
		  <ul style="list-style:none;">
		  	<li style="float: left">
		  		<s:textfield name="%{'registers.makeNew['+#st.index+'].name'}" label="用户名"/>
		  	</li>
		  	<li style="float: left">
		  		<s:password name="%{'registers.makeNew['+#st.index+'].pwd'}" label="密码"/>
		  	</li>
		  	<li style="float: left">
		  		<s:textfield name="%{'registers.makeNew['+#st.index+'].age'}" label="年龄"/>
		  	</li>
		  	<li style="float: left">
		  		<s:textfield name="%{'registers.makeNew['+#st.index+'].birthday'}" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'});" label="生日"/>
		  	</li>
		  	<li>
		  		<s:textfield name="%{'registers.makeNew['+#st.index+'].address'}" label="地址"/>
		  	</li>
		  </ul>
		</s:iterator>
	   <div><s:submit value="submit"/></div>
	</s:form>
  </body>



15.success2.jsp 遍历 Set 和获取 单个
<body>
  <ul style="list-style:none; text-align: center;">
		  	<li style="float: left;width: 155px">用户名</li>
		  	<li style="float: left;width: 155px">密码</li>
		  	<li style="float: left;width: 155px">年龄</li>
		  	<li style="float: left;width: 155px">生日</li>
		  	<li style="float: left;width: 155px">地址</li>
		  </ul>
		  <div style="clear: both;"></div>
	<div>===========遍历所有的=========</div>
    <s:iterator value="registers" status="st">
   		 <ul style="list-style:none;">
		  	<li style="float: left;width: 155px;">
		  		<s:property value="name"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="pwd"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="age"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="birthday"/>
		  	</li>
		  	<li>
		  		<s:property value="address"/>
		  	</li>
		  </ul>
		  <div></div>
    </s:iterator>
     <div>===========单独去其中的一个(知道其中的key wei admin)========</div>
     <ul style="list-style:none;">
		  	<li style="float: left;width: 155px;">
		  		<s:property value="registers('admin').name"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="registers('admin').pwd"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="registers('admin').age"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="registers('admin').birthday"/>
		  	</li>
		  	<li>
		  		<s:property value="registers('admin').address"/>
		  	</li>
		  </ul>
  </body>



16.访问
--http://localhost:8080/Struts2_CollectConversion/login3.jsp
填写信息后  就会到成功页面 看到遍历所有  和 取单个


---------------Map----------------

17.使用 Strut2的 Map 类型
action
package com.sh.action;

import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.sh.pojo.Register;

public class RegisterHashMapAction extends ActionSupport {

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

	public Map<String, Register> getMaps() {
		return maps;
	}

	public void setMaps(Map<String, Register> maps) {
		this.maps = maps;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
	
	
}



18.属性配置文件 RegisterHashMapAction-conversion.properties
Key_maps=java.lang.String   // Key_ 固定  后面为action的Map属性名
Element_maps=com.sh.pojo.Register


19.login5.jsp
<body>
	<s:form action="registerHashMapAction" method="post" theme="simple">
		<ul style="list-style:none; text-align: center;">
		  	<li style="float: left;width: 155px">用户名</li>
		  	<li style="float: left;width: 155px">密码</li>
		  	<li style="float: left;width: 155px">年龄</li>
		  	<li style="float: left;width: 155px">生日</li>
		  	<li style="float: left;width: 155px">地址</li>
		  </ul>
		  <div style="clear: both;"></div>
		  <!-- 注意 【key】 中key 的取值类型和 配置文件中一直-->
		<s:iterator value="new int[4]" status="st">
		  <ul style="list-style:none;">
		  	<li style="float: left">
		  		<s:textfield name="%{'maps['+#st.index+'].name'}" label="用户名"/>
		  	</li>
		  	<li style="float: left">
		  		<s:password name="%{'maps['+#st.index+'].pwd'}" label="密码"/>
		  	</li>
		  	<li style="float: left">
		  		<s:textfield name="%{'maps['+#st.index+'].age'}" label="年龄"/>
		  	</li>
		  	<li style="float: left">
		  		<s:textfield name="%{'maps['+#st.index+'].birthday'}" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'});" label="生日"/>
		  	</li>
		  	<li>
		  		<s:textfield name="%{'maps['+#st.index+'].address'}" label="地址"/>
		  	</li>
		  </ul>
		</s:iterator>
	   <div><s:submit value="submit"/></div>
	</s:form>
  </body>


20 .success3.jsp 遍历 Map  和   取 单个
<body>
  <ul style="list-style:none; text-align: center;">
		  	<li style="float: left;width: 155px">用户名</li>
		  	<li style="float: left;width: 155px">密码</li>
		  	<li style="float: left;width: 155px">年龄</li>
		  	<li style="float: left;width: 155px">生日</li>
		  	<li style="float: left;width: 155px">地址</li>
		  </ul>
		  <div style="clear: both;"></div>
	<div>===========遍历所有的=========</div>
    <s:iterator value="maps" status="st">
   		 <ul style="list-style:none;">
		  	<li style="float: left;width: 155px;">
		  		<s:property value="value.name"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="value.pwd"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="value.age"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="value.birthday"/>
		  	</li>
		  	<li>
		  		<s:property value="value.address"/>
		  	</li>
		  </ul>
		  <div></div>
    </s:iterator>
     <div>===========单独去其中的一个= (知道其中的key=0)========</div>
     <ul style="list-style:none;">
		  	<li style="float: left;width: 155px;">
		  		<s:property value="maps['0'].name"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="maps['0'].pwd"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="maps['0'].age"/>
		  	</li>
		  	<li style="float: left;width: 155px;">
		  		<s:property value="maps['0'].birthday"/>
		  	</li>
		  	<li>
		  		<s:property value="maps['0'].address"/>
		  	</li>
		  </ul>
  </body>


21.访问
--http://localhost:8080/Struts2_CollectConversion/login5.jsp
填写信息就可以看到结果了

注意 action中 set 和hashmap 都要 初始化 和 有  get 和set 方法

3
3
分享到:
评论
5 楼 双子树 2016-05-17  
东西太好啦受教啊
4 楼 一笑_奈何 2015-11-25  
谢谢了,学习了。真心有用
3 楼 lonfiy 2014-01-07  
正好要用到这个,学习了
2 楼 Glory_Nol 2013-11-26  
  lz 我想问下可以用 中文 作为 map 的 key 吗?
我试了下 <s:password name="%{maps['第一条数据'].pwd" label="密码"/> 
好像不行呃。
如果可以?那应该怎么改?谢谢!
1 楼 maakey 2012-09-14  
,分享不错,好东西,学习了

相关推荐

Global site tag (gtag.js) - Google Analytics