qiyadeng

专注于Java示例及教程
posts - 84, comments - 152, trackbacks - 0, articles - 34

Struts Checkbox radio怎么进行动态的选择

Posted on 2005-12-14 10:19 qiyadeng 阅读(1926) 评论(0)  编辑  收藏 所属分类: J2EE

使用Struts的朋友,可能经常发现使用页面的一些标签的时候觉得不够得心应手。比如我们要设计一个用户修改得信息的界面,需要调出一些用户的信息。不幸的是,当页面中含有Checkbox或是radio的时候有没有想过怎么处理?以前我也为这个苦恼,不少论坛上的人也发帖子问这个问题。最近看到了两篇文章,可以解决这个问题。来自IBM developer:
http://www-128.ibm.com/developerworks/java/library/j-sr3.html
http://www-128.ibm.com/developerworks/java/library/j-sr4.html
multibox.bmpradio.bmp
先调简单的说起吧,我们来实现动态的checkbox:
1、Form的设计:我们这个例子有个String[] mountains,这个变量数组中包含很多的山的名字;还有个数组String[] selectedMountains就是默认选中的一些山,像下面这样很简单。

public class MultiboxForm extends ActionForm {
 
 private String[] selectedMountains = {"Everest","K2","Lhotse","Cho Oyu"};
 private String[] mountains ={"Everest","K2","Kangchenjunga","Lhotse","Makalu","Kangchenjunga South","Lhotse Middle","Kangchenjunga West","Lhotse Shar","Cho Oyu"};

 public String[] getMountains() {
  return mountains;
 }
 public void setMountains(String[] mountains) {
  this.mountains = mountains;
 }
 public String[] getSelectedMountains() {
  return selectedMountains;
 }
 public void setSelectedMountains(String[] selectedMountains) {
  this.selectedMountains = selectedMountains;
 }

2、Action的设计:这个就更简单了,一句话,由控制器切换到视图。

 return mapping.findForward("success");
3、JSP的设计:

  <html:form action="/multibox" name="multiboxForm" type="qiya.deng.struts.form.MultiboxForm">
   <logic:iterate name="multiboxForm" property="mountains" id="mountain">
    <html:multibox property="selectedMountains">
     <bean:write name="mountain"/>
    </html:multibox>
    <bean:write name="mountain"/><br/>
   </logic:iterate>
   <br/>
   <html:submit/><html:cancel/>
  </html:form>
这个相对比较复杂,也是比较中要的。<logic:iterate/>是迭代MultiboxForm中的mountians,
<html:multibox property="selectedMountains">
 <bean:write name="mountain"/>
</html:multibox>
这个标签的作用就是selecteDMountains的值和迭代的mountain值一致的时候就会选中。

接下来说Radio,其实Form和Action部分都差不多,只是JSP页面有点不一样,Radio里面甚至牵涉到了JavaScript。
1.From的设计:


 private String selectedMountain = "Kangchenjunga West";
 private String[] mountains ={"Everest","K2","Kangchenjunga","Lhotse","Makalu","Kangchenjunga South","Lhotse Middle","Kangchenjunga West","Lhotse Shar","Cho Oyu"};
 
 public String[] getMountains() {
  return mountains;
 }
 public void setMountains(String[] mountains) {
  this.mountains = mountains;
 }
 public String getSelectedMountain() {
  return selectedMountain;
 }
 public void setSelectedMountain(String selectedMountain) {
  this.selectedMountain = selectedMountain;
 }
2.Action同上一样,切换到JSP页面去。 
3.重点部分JSP:

  <html:form action="/radio" name="radioForm" type="qiya.deng.struts.form.RadioForm">
   <bean:define id="selectedRadio" property="selectedMountain" name="radioForm"></bean:define>
   <logic:iterate id="mountain" property="mountains" name="radioForm">
    <bean:define id="mountainValue">
     <bean:write name="mountain"/>
    </bean:define>
    <html:radio property="selectedMountain" value="<%=mountainValue%>" styleId="<%=mountainValue%>"/>
    <bean:write name="mountain"/><br/>
   </logic:iterate><br/>
   <html:submit/><html:cancel/>
   <script type="text/javascript">
             <!--
          var selectedRadio =
             document.forms["radioForm"].elements["<bean:write name="selectedRadio"/>"];
              selectedRadio.checked=true;
             -->
         </script>
  </html:form>


"<bean:define/>"定义了一个PageContext作用域的属性selectedRadio,下面的JavaScript的作用就是根据这个属性找到应该选中的Radio。其他的地方和上面那个很相似的。如果还是不太理解先运行,看看生成的HTML代码。


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


网站导航: