如何“逻辑:迭代”只是一个对象? [英] How to "logic:iterate" just one object?
问题描述
所以,我有这个代码:
logic:iterate name =nameFormproperty =nameid = nameIdindexId =index
bean:write name =nameIdproperty =field1/
bean:write name =nameIdproperty =field2/
这很好,因为我收到一个对象表,所以我可以做迭代没有问题。
现在,在另一个页面上,我需要做同样的事情,但问题是我没有收到对象表,而是一个对象本身。
我尝试了它,并且 - 如预期 - 得到错误:无法为此集合创建迭代器
我有RTFM' d和我比以前更困惑。
我得到如何在逻辑:iterate里的名称指向struts配置中的表单名称,现在我需要做同样的
您可以使用bean名称与 < logic:iterate> ,但它应该是一个集合或数组,或实现
So, I have this code:
logic:iterate name="nameForm" property="name" id="nameId" indexId="index"
bean:write name="nameId" property="field1"/
bean:write name="nameId" property="field2"/
This works great because I'm receiving a "table of objects" so I can do the iterate without issues.
Now, on another page I need to do the same but the issue is I am not receiving a "table of objects" but an object itself. I tried it nonetheless and - as expected - got the error: Cannot create iterator for this collection
I've RTFM'd and I'm still more confused than before.
I get how the "name" inside the "logic:iterate" points to the form name in the struts-config, now I need to do the same with just one bean, any help please?
You can use a bean name with the <logic:iterate>
, but it should be a collection or array, or implement Iterable
. Here's the example of the tag usage.
In Struts, you can use
logic:iterate
tag to iterate over collections. Here’re the example:Iterate over a list/array (Object)
Create a normal list with few
"user"
objects and store it intoHttpServletRequest
as name"listUsers"
.public class User{ String username; String url; //getter and setter methods } ... public class PrintMsgAction extends Action{ public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { List<User> listUsers = new ArrayList<User>(); listUsers.add(new User("user1", "http://www.user1.com")); listUsers.add(new User("user2", "http://www.user2.com")); listUsers.add(new User("user3", "http://www.user3.com")); listUsers.add(new User("user4", "http://www.user4.com")); request.setAttribute("listUsers", listUsers); return mapping.findForward("success"); } }
Inside the logic tag, you can use the
"name"
attribute (listUsers
) to get the list value, while"property"
attribute to display the object property value.<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> <html> <head> </head> <body> <h1>Struts <logic:iterate> example</h1> <logic:iterate name="listUsers" id="listUserId"> <p> List Users <bean:write name="listUserId" property="username"/> , <bean:write name="listUserId" property="url"/> </p> </logic:iterate> </body> </html>
这篇关于如何“逻辑:迭代”只是一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!