随笔 - 6  文章 - 129  trackbacks - 0
<2008年10月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(14)

随笔档案(6)

文章分类(467)

文章档案(423)

相册

收藏夹(18)

JAVA

搜索

  •  

积分与排名

  • 积分 - 814160
  • 排名 - 49

最新评论

阅读排行榜

评论排行榜

package com.ke.struts.bean;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;


public class ActionForm extends ActionForm {

 private List<String> friends = new ArrayList<String>();
 private static String[] skillLevels = new String[]{"Beginner","Intermediate","Advanced"};
 private Map<String,Object> skills = new HashMap<String,Object>();
 

public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
  // TODO Auto-generated method stub
  return null;
 }
 public void reset(ActionMapping mapping, HttpServletRequest request) {

  this.friends.clear();
 }
 public Object getSkill(String key)
 {
  return skills.get(key);
 }
 public void setSkill(String key,Object value)
 {
  skills.put(key, value);
 }
 public Map getSkills()
 {
  return skills ;
 }
 public String[] getSkillLevels()
 {
  return skillLevels;
 }
 public List<String> getFriends() {
  return friends;
 }

 public void setFriends(List<String> friends) {
  this.friends = friends;
 }
 
 public void setFriend(int index,String friend)
 {
  if(this.friends.size() > index)
  {
   /*用指定的元素替代此列表中指定位置上的元素*/
   this.friends.set(index, friend);
  }
  else
  {
   while(this.friends.size() < index)
   {
    this.friends.add(null);
   }
   this.friends.add(index, friend);
  }
 }
 
 public String getFriend(int index)
 {
  if(this.friends.size() > index)
  {
   return (String)this.friends.get(index);
  }
  else
  {
   return null ;
  }
 }
}
输入页面
     Friend 1 :<html:text property="friend[0]"></html:text><br>   <!-- 调用setFriend(int index,String friend)-->
     Friend 2 :<html:text property="friend[1]"></html:text><br>
     Friend 3 :<html:text property="friend[2]"></html:text><br>
java skill: <html:select property="skill(java)">   <!-- 调用setSkill(String key,Object value) -->
                <html:options property="skillLevels"/>
              </html:select><br>
  jsp skill:<html:select property="skill(jsp)">
                <html:options property="skillLevels"/>
              </html:select><br>
  struts skill:<html:select property="skill(struts)">
                   <html:options property="skillLevels"/>
                 </html:select><br>

输出页面
    Friend 1:<bean:write name="ActionForm" property="friend[0]"/><br><!-- 调用getFriend(int index)-->
    Friend 2:<bean:write name="ActionForm" property="friend[1]"/><br>
    Friend 3:<bean:write name="ActionForm" property="friend[2]"/><br>
  java skill :<bean:write name="ActionForm" property="skill(java)"/><br><!-- 调用getSkill(String key) -->
  jsp skill :<bean:write name="ActionForm" property="skill(jsp)"/><br>
  struts skill :<bean:write name="ActionForm" property="skill(struts)"/>

 



posted on 2007-08-27 11:09 Ke 阅读(957) 评论(3)  编辑  收藏 所属分类: struts

FeedBack:
# re: ActionForm中对集合属性的处理 2008-01-06 21:10 www
顶  回复  更多评论
  
# re: ActionForm中对集合属性的处理 2008-10-25 21:07 yupeng_raul7@hotmail.com
楼主,我用的跟你一样的方法,但是出现这样的情况:
我添加两条数据List 能正常用但是,我添加三条四条就出现
java.lang.IndexOutOfBoundsException

楼主能不能帮忙解释一下啊  回复  更多评论
  
# re: ActionForm中对集合属性的处理[未登录] 2008-10-25 21:25 KE
@yupeng_raul7@hotmail.com
数组下标越界了吧,照那异常来看,细心好好检查下吧  回复  更多评论
  

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


网站导航: