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 阅读(971)
评论(3) 编辑 收藏 所属分类:
struts