神秘的 J2ee 殿堂

·古之学者必有师·做学者亦要做师者·FIGHTING·

jsp通过logic:iterate或html:optionsCollection循环显示action中传过来的对象List

action中代码如下:
        List allUser=this.getLoginServiceImpl().find();
        request.setAttribute(
"user", allUser);
        
return mapping.findForward("listUser");
注:其中User对象有id和name属性

jsp显示:法一
            <html:select property="school">
            
<html:option value="">
                
<bean:message key="login.select" />
           
</html:option>
                
<logic:present name="user">
                    
<logic:iterate id="user" name="user" offset="0">
                        
<option value="<bean:write name="user" property="id" />">
                            
<bean:write name="user" property="name" />
                        
</option>
                    
</logic:iterate>
                
</logic:present>
        
</html:select>
jsp显示:法二
       <html:select property="school">
           
<html:optionsCollection name="user" value="id" label="name"/>
       
</html:select>

optionsCollection标签用法:
与options标签一样,optionsCollection标签可以从集合或者是包含集合的对象里获得选项的标签/值对。在这两种情况里,集合或包含集合的对象必须是一个作用域对象,否则定制标签将无法访问它。

1.与包含集合的对象配合使用
举例:userForm动作表单有一个如下所示的ArrayList类型的userList属性,相应的set,get方法,通过在action中设置好userForm后,request.setAttribute("userForm",userForm);
在jsp页面:
<html:select property="school">
<html:optionsCollection name="userForm"
      property
="userList"/>
</html:select>

2.与集合配合使用
action中
ArrayList userList=new ArrayList();
userList.add(
new LabelValueBean("1","haha"));
userList.add(
new LabelValueBean("2","dada"));
userList.add(
new LabelValueBean("3","xiaoxiao"));
request.setAttribute(
"userList",userList);
jsp页面
<html:select property="school">
<html:optionsCollection name="userList"
      label
="label" value="value"/>
</html:select>

(注:边学边share,如有问题,欢迎交流学习)


posted on 2007-09-15 23:58 月芽儿 阅读(6462) 评论(3)  编辑  收藏 所属分类: J2EE学习心得

评论

# re: jsp通过logic:iterate或html:optionsCollection循环显示action中传过来的对象List 2008-03-26 20:11 李晓勇

今天刚好碰上类似问题,此文帮我解开了疑惑,多谢!!!  回复  更多评论   

# re: jsp通过logic:iterate或html:optionsCollection循环显示action中传过来的对象List 2009-05-09 13:07 过冬的kimera

帮大忙了,谢谢  回复  更多评论   

# re: jsp通过logic:iterate或html:optionsCollection循环显示action中传过来的对象List 2012-05-30 15:25 cowrylee

<option value="<bean:write name="user" property="id" />">
这种写法不符合xhtml  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(2)

随笔分类

随笔档案

相册

搜索

最新评论

阅读排行榜

评论排行榜