随笔-26  评论-13  文章-46  trackbacks-0

问题原型:
     添加页面中有10个checkbox,用户选中了5个后提交
     修改页面中应显示10个checkbox,同时应该有5个已经被选中
 
详细实际问题:
    为了满足业务需要,从linux安装盘中的comps.xml中读取提供的服务所依赖的包
    显示在页面上,根据包的类型(default,mandatory,optional)决定显示的方法
    default默认选中,mandatory不提供选择,optional为用户可选
  

< c:choose >
  
< c:when  test ="${p.type == 'mandatory'}" >
    
< td >
     
< input  type ="checkbox"  name ="nodeRPMS"
    style
="VISIBILITY: hidden; "
    checked
="true"
    value
="<c:out value=" ${p.rpmName}" /> "/>
    
</ td >
  
</ c:when >

   
< c:when  test ="${p.type == 'default'}" >
    
< td >
      
< input  type ="checkbox"
       name
="nodeRPMS"   checked =true  
       
value ="<c:out  value=" ${p.rpmName}" /> "/>
    
</ td >
   
</ c:when >

  
< c:when  test ="${p.type == 'optional'}" >
    
< td >  
     
< input  type ="checkbox"  name ="nodeRPMS"
       value
="<c:out value=" ${p.rpmName}" /> "/>
    
</ td >
  
</ c:when >
</ c:choose >

在处理层,采用struts的form来接受,定义string [] nodeRPMs接受即可
接受后存在数据库里,然后读出来在显示在页面上

在修改页面,首先应该有全部的checkbox,同时用户选中的应该已经被打勾(这来自数据库读出的记录),经过中午的查找资料
才知道可以使用mutilbox解决这个问题

< c:choose >
  
< c:when test = " ${p.type == 'mandatory'} " >
    
< td >
      
< input type = " checkbox "  name = " nodeRPMS "
    style
= " VISIBILITY: hidden;  "
    
checked = " true "
    value
= " <c:out value= " $ {p.rpmName} " /> " />
   
</ td >
  
</ c:when >

  
< c:otherwise >
   
< td >
      
< html:multibox property = " nodeRPMS " >
      
< c: out  value = " ${p.rpmName} " />
      
</ html:multibox >     
   
</ td >
  
</ c:otherwise >
</ c:choose >


mutilbox会根据你读出的值来设定
 struts中的checkbox是单选的,它的值应该只有true和false或者yes no或者on off之分;
r如果希望使用一组可以多选的checkbox,在struts中的页面标签应该是html:multibox,它的用法:<html:multibox property="strArray" value="value1">或者
       <html:multibox property="strArray">value2</html:multibox>

真正载页面上使用时可能是这样:
<html:multibox property="strArray">a</html:multibox>
<html:multibox property="strArray">b</html:multibox>
<html:multibox property="strArray">c</html:multibox>

当然"strArray"一定是页面相对应的formbean中的一个属性,并且应该是数组:
private String[] strArray; //提供get set方法

这样,如果我们在页面上面选中了前两个,则strArray.length=2;strArray[0]里面的值是a,strArray[1]里面的值是b;
如果我们把form中的strArray设置为strArray[0]="b",strArray[1]="c",则转到页面之后,页面上只有后两项被选中
所以这样就可以解决问题了

posted on 2006-04-05 16:18 似水流年 阅读(5118) 评论(1)  编辑  收藏 所属分类: Struts

评论:
# re: struts html:multibox标签(转载) 2011-06-23 16:12 | 何杨
Good!  回复  更多评论
  

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


网站导航: