logic:iterate的嵌套循环

ddxx5317 2013-08-05 09:29:03
html代码:
<logic:iterate id="li" name="options">
<bean:write name="li" property="id"/>
<bean:write name="li" property="name"/>
<html:select property="code">
<html:options collection="list" property="value" labelProperty="label"/></html:select>
</logic:iterate>

action代码:
List list = new ArrayList();
list.add(new LabelValueBean("1","A"));
list.add(new LabelValueBean("2","B"));

Bean bean = new Bean();
bean.setId("id1");
bean.setName("name1");
bean.setList(list);

List list1 = new ArrayList();
list1.add(new LabelValueBean("3","C"));
list1.add(new LabelValueBean("4","E"));

Bean bean1 = new Bean();
bean1.setId("id1");
bean1.setName("name1");
bean1.setList(list1);

options.add(bean);
options.add(bean1);
bean代码:
private String id = null;
private String name = null;
private List list = new ArrayList();
get和set方法省略

在action里面把options设置到request里面(request.setAttribute("options", options);)

然后在运行的时候报错:Cannot find bean under name list
...全文
203 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ddxx5317 2013-08-05
  • 打赏
  • 举报
回复
引用 18 楼 wyyan5320 的回复:
是相当于把你的里面的UserForm加到一个里面去(比如是list),然后在把list设到request里面去,然后在去取list里面的countyList的值 在在jsp里面下面的操作: <logic:iterate id="li" name="list"> <bean:write name="li" property="id"/> <bean:write name="li" property="name"/> <html:select name="uf" property="code" > <html:optionsCollection name="uf" property="countryList" label="simple" value="zip" /> </html:select> </logic:iterate>
<logic:iterate id="uf" name="list"> <bean:write name="uf" property="code"/> <html:select name="uf" property="code" > <html:optionsCollection name="uf" property="countryList" label="simple" value="zip" /> </html:select> </logic:iterate>
ddxx5317 2013-08-05
  • 打赏
  • 举报
回复
是相当于把你的里面的UserForm加到一个里面去(比如是list),然后在把list设到request里面去,然后在去取list里面的countyList的值 在在jsp里面下面的操作: <logic:iterate id="li" name="list"> <bean:write name="li" property="id"/> <bean:write name="li" property="name"/> <html:select name="uf" property="code" > <html:optionsCollection name="uf" property="countryList" label="simple" value="zip" /> </html:select> </logic:iterate>
ddxx5317 2013-08-05
  • 打赏
  • 举报
回复
谢谢,你这么晚了还回复,但是这个好像不是我想要的,我是要去list里面数组的值啊
街头小贩 2013-08-05
  • 打赏
  • 举报
回复
我用的struts是 1.3.10
街头小贩 2013-08-05
  • 打赏
  • 举报
回复
相关的类: uf

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

public class UserForm {

	String code;
	private List<Country> countryList=new ArrayList<Country>();
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public List<Country> getCountryList() {
		return countryList;
	}
	public void setCountryList(List<Country> countryList) {
		this.countryList = countryList;
	}
	
	
}
Country:

public class Country {

	String zip;
	String simple;
	
	public Country() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Country(String zip, String simple) {
		super();
		this.zip = zip;
		this.simple = simple;
	}
	public String getZip() {
		return zip;
	}
	public void setZip(String zip) {
		this.zip = zip;
	}
	public String getSimple() {
		return simple;
	}
	public void setSimple(String simple) {
		this.simple = simple;
	}
	
}
action

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class UserFormAction extends DispatchAction {


	public ActionForward build(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		UserForm uf=new UserForm();
		uf.setCode("china");
		List<Country> countryList=new ArrayList<Country>();
		countryList.add(new Country("264000","yantai"));
		uf.setCountryList(countryList);
		
		arg2.setAttribute("uf", uf);
		ActionForward forward=arg0.findForward("new");
		return forward;
	}

}
struts.xml

        <action 
            path="/build" 
            parameter="type"
            scope="request"
            type="net.apobates.struts.UserFormAction">
                <forward name="new" path="/form.jsp" />
        </action>
街头小贩 2013-08-05
  • 打赏
  • 举报
回复
测试的是这样的:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<html:select name="uf" property="code" >
<html:optionsCollection name="uf" property="countryList" label="simple" value="zip" />
</html:select>
</body>
</html>
ddxx5317 2013-08-05
  • 打赏
  • 举报
回复
action代码: List list1 = new ArrayList(); list1.add(new LabelValueBean("3","C")); list1.add(new LabelValueBean("4","E")); request.setAttribute("list", list1); jsp代码: <html:select property="code"> <html:options collection="list" property="value" labelProperty="label"/> </html:select> 如果这样都能取得值,但是如果设置到bean里面的list,就不知道怎么样获取bean里面的list了。
ddxx5317 2013-08-05
  • 打赏
  • 举报
回复
我用的是struts1.2的版本,我把源码发你给吧!QQ:470042815 加我QQ吧,方便联系。
街头小贩 2013-08-05
  • 打赏
  • 举报
回复
引用 10 楼 wyyan5320 的回复:
html:optionsCollection html:options Collection 好像都可以吧,我也不太清楚,网上好像说是可以的
你用的是struts哪个版本我在本地测一下
ddxx5317 2013-08-05
  • 打赏
  • 举报
回复
html:optionsCollection html:options Collection 好像都可以吧,我也不太清楚,网上好像说是可以的
ddxx5317 2013-08-05
  • 打赏
  • 举报
回复
没有code是我随便定义的,这个与这个属性有关系吗?
街头小贩 2013-08-05
  • 打赏
  • 举报
回复
html:options collection options collection之间有空格吗?我怎么看到的是都是html:optionsCollection
街头小贩 2013-08-05
  • 打赏
  • 举报
回复
引用 6 楼 wyyan5320 的回复:
我的本意是要把bean里面的list数组放到<html:select>里面的,我不知道怎么写,就写成这样了。
你的bean中有code这个属性吗
ddxx5317 2013-08-05
  • 打赏
  • 举报
回复
我的本意是要把bean里面的list数组放到<html:select>里面的,我不知道怎么写,就写成这样了。
街头小贩 2013-08-05
  • 打赏
  • 举报
回复
你的list是在html:中呀,不是在bean中
ddxx5317 2013-08-05
  • 打赏
  • 举报
回复
拿掉这些肯定是对的,关键就是获取不到第二个数组,bean里面的list获取不到。
街头小贩 2013-08-05
  • 打赏
  • 举报
回复
把这些拿掉再试试:

    	<html:select property="code">
  <html:options collection="list" property="value" labelProperty="label"/></html:select>
ddxx5317 2013-08-05
  • 打赏
  • 举报
回复
就在action里面有个定义:List options = new ArrayList();
街头小贩 2013-08-05
  • 打赏
  • 举报
回复
options的定义也没贴?

10,607

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧