浏览 2110 次
锁定老帖子 主题:struts2转化器—数组
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-07-29
public Object convertFromString(Map context, String[] values, Class toClass) { if (values.length > 1) { User[] result = new User[values.length]; for (int i = 0; i < values.length ; i++ ) { User user = new User(); String[] userValues = values[i].split(","); user.setName(userValues[0]); user.setPassword(userValues[1]); result[i] = user; } return result; } else { User user = new User(); String[] userValues = values[0].split(","); user.setName(userValues[0]); user.setPassword(userValues[1]); return user; } } @Override public String convertToString(Map context, Object o) { if (o instanceof User) { User user = (User)o; return "<" + user.getName() + "," + user.getPassword() + ">"; } else if (o instanceof User[]) { User[] users = (User[])o; String result = "["; for (User user : users ) { result += "<" + user.getName() + "," + user.getPassword() + ">"; } return result + "]"; } else { return ""; } } jsp显示 提交页面 <s:form action="converterAction.action" method="post"> <s:textfield label="user" name="user" ></s:textfield> <s:textfield label="user" name="user" ></s:textfield> <s:textfield label="user" name="user" ></s:textfield> <s:submit></s:submit> </s:form> 显示页面 user:<s:property value="user[0]"/><br> user:<s:property value="user[1]"/><br> user:<s:property value="user[2]"/><br> 配置文件 xwork-conversion.properties org.apache.struts.model.User=org.apache.struts.converter.UserConverter 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |