主题:如何得到logic:iterate循环中的indexId
1个回答
展开全部
有没有办法等到logic:iterate循环中的indexId的索引值,
请各位大侠指教,
<logic:iterate id="item" name="list" indexId="i">
<logic:equal name="i" value="<%= Integer.toString(list.size() - 1) %>" ><br /></logic:equal>
<bean:write name="item" />
</logic:iterate>
<logic:iterate id="text" name="bean" property="list" indexId="index"> <bean:write name="index" />: <bean:write name="text" />
</logic:iterate>
还有这种用法
<logic:iterate id = "name" name ="UseBean" property = "propname" indexId = "index">
<%= index %> <bean:write name ="name" property = "hoge" />
场景:有一个JavaBeans:如下:
public class LabelValueBean implements Serializable { public LabelValueBean(String label, String value) { this.label = label; this.value = value; } private String label; public String getLabel() { return this.label; } public void setLabel(String label) { this.label = label; } private String value; public String getValue() { return this.value; } public void setValue(String value) { this.value = value; } public String toString() { StringBuffer sb = new StringBuffer("LabelValueBean["); sb.append(this.label); sb.append(", "); sb.append(this.value); sb.append("]"); return (sb.toString()); }}
这是非常普通的JavaBeans, 如果ActionForm是这样:
public class TestBean extends ActionForm { ..... private List lvbeans = new ArrayList(); .....}
使用indexed属性,TestBean类如下:
public class TestBean extends ActionForm { ..... private List lvbeans = new ArrayList(); public TestBean(){ //初始化数值 lvbeans.add(new LabelValueBean("1", "1")); lvbeans.add(new LabelValueBean("2", "2")); lvbeans.add(new LabelValueBean("3", "3")); } public LabelValueBean getLvbeans(int index) { return (LabelValueBean)lvbeans.get(index); } public List getLvbeans() { return lvbeans; } .....}
注意上述两个get方法很重要,Jsp页面代码如下:
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %><%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %><jsp:useBean id="test2" scope="page" class="test.TestBean"/><h2>Test: <logic:iterate name="test2" property="lvbeans" id="foo" indexId="ctr"> <html:text name="test2" property='<%= "lvbeans[" + ctr + "].label" %>' /> </logic:iterate></h2>
这样,运行后的输出结果是:
<h2>Test: <input type="text" name="lvbeans[0].label" value="1"> <input type="text" name="lvbeans[1].label" value="2"> <input type="text" name="lvbeans[2].label" value="3"> </h2>
这样修改TestBean中的List中的LabelValueBean数值了。
TOP该问题已经结贴 ,得分记录:whyxx(10)、whyxx(20)、duanyuxy123(10)、
请各位大侠指教,
<logic:iterate id="item" name="list" indexId="i">
<logic:equal name="i" value="<%= Integer.toString(list.size() - 1) %>" ><br /></logic:equal>
<bean:write name="item" />
</logic:iterate>
<logic:iterate id="text" name="bean" property="list" indexId="index"> <bean:write name="index" />: <bean:write name="text" />
</logic:iterate>
还有这种用法
<logic:iterate id = "name" name ="UseBean" property = "propname" indexId = "index">
<%= index %> <bean:write name ="name" property = "hoge" />
场景:有一个JavaBeans:如下:
public class LabelValueBean implements Serializable { public LabelValueBean(String label, String value) { this.label = label; this.value = value; } private String label; public String getLabel() { return this.label; } public void setLabel(String label) { this.label = label; } private String value; public String getValue() { return this.value; } public void setValue(String value) { this.value = value; } public String toString() { StringBuffer sb = new StringBuffer("LabelValueBean["); sb.append(this.label); sb.append(", "); sb.append(this.value); sb.append("]"); return (sb.toString()); }}
这是非常普通的JavaBeans, 如果ActionForm是这样:
public class TestBean extends ActionForm { ..... private List lvbeans = new ArrayList(); .....}
使用indexed属性,TestBean类如下:
public class TestBean extends ActionForm { ..... private List lvbeans = new ArrayList(); public TestBean(){ //初始化数值 lvbeans.add(new LabelValueBean("1", "1")); lvbeans.add(new LabelValueBean("2", "2")); lvbeans.add(new LabelValueBean("3", "3")); } public LabelValueBean getLvbeans(int index) { return (LabelValueBean)lvbeans.get(index); } public List getLvbeans() { return lvbeans; } .....}
注意上述两个get方法很重要,Jsp页面代码如下:
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %><%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %><jsp:useBean id="test2" scope="page" class="test.TestBean"/><h2>Test: <logic:iterate name="test2" property="lvbeans" id="foo" indexId="ctr"> <html:text name="test2" property='<%= "lvbeans[" + ctr + "].label" %>' /> </logic:iterate></h2>
这样,运行后的输出结果是:
<h2>Test: <input type="text" name="lvbeans[0].label" value="1"> <input type="text" name="lvbeans[1].label" value="2"> <input type="text" name="lvbeans[2].label" value="3"> </h2>
这样修改TestBean中的List中的LabelValueBean数值了。
TOP该问题已经结贴 ,得分记录:whyxx(10)、whyxx(20)、duanyuxy123(10)、
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询