标签:
1 public class UserConvert extends StrutsTypeConverter { 2 @Override 3 public Object convertFromString(Map map, String[] strings, Class aClass) { 4 Set result = new HashSet(); 5 for (int i = 0; i < strings.length; i++) { 6 User user = new User(); 7 String[] vals = strings[0].split(","); 8 user.setName(vals[0]); 9 user.setPwd(vals[1]); 10 result.add(user); 11 } 12 return result; 13 } 14 15 @Override 16 public String convertToString(Map map, Object o) { 17 if (o.getClass() == Set.class) { 18 19 Set users = (Set) o; 20 String result = "["; 21 for (Object obj : users) { 22 User user = (User) obj; 23 if (user != null && user.getName() != null) { 24 result += "<" + user.getName() + "," + user.getPwd() + ">"; 25 } 26 27 } 28 return result + "]"; 29 } else { 30 return ""; 31 } 32 33 } 34 35 }
1 public class User { 2 public User() { 3 } 4 5 private String name; 6 private String pwd; 7 8 public String getPwd() { 9 return pwd; 10 } 11 12 public void setPwd(String pwd) { 13 this.pwd = pwd; 14 } 15 16 public String getName() { 17 return name; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 @Override 25 public boolean equals(Object obj) { 26 27 // 表明:action中的set user,使用元素name属性进行区分(set集合不能有重复项) 28 29 30 if (this==obj) { 31 return true; 32 } 33 if (obj != null && obj.getClass() == User.class) { 34 User user = (User) obj; 35 return this.getName().equals(user.getName()); 36 } 37 return false; 38 } 39 40 @Override 41 public int hashCode() { 42 return name.hashCode(); 43 } 44 }
users=convert.UserConvert
#指定action中users集合的索引(key)是name
KeyProperty_users=name
注意: 使用的是 == 而不是 equal
if (this==obj) {
return true;
}
1 <s:form action="login"> 2 <s:textfield name="users" label="第一个用户信息"/> 3 <s:textfield name="users" label="第二个用户信息"/> 4 <tr> 5 <td colspan="2"><s:submit value="转换" theme="simple"/> 6 <s:reset value="重填" theme="simple"/></td> 7 </tr> 8 </s:form>
1 <s:property value="users"/> 2 <!-- 访问users集合属性里索引属性值为zcx的元素的name属性--> 3 用户zcx的用户名为:<s:property value="users(‘zcx‘).name"/><br/> 4 <!-- 访问users集合属性里索引属性值为zjj的元素的name属性--> 5 zjj:<s:property value="users(‘zjj‘).name"/><br/>
标签:
原文地址:http://www.cnblogs.com/zijiyanxi/p/5559722.html