package com.soft.gread1.converter;
import java.util.ArrayList;import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.struts2.util.StrutsTypeConverter;
import com.soft.gread1.model.User;
public class UserConverter extends StrutsTypeConverter{
@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
List<User> list=new ArrayList<User>();
for(String str:values)
{
StringTokenizer st=new StringTokenizer(str,":");
User user=new User();
user.setUsername(st.nextToken());
user.setPassword(st.nextToken());
user.setAge(Integer.parseInt(st.nextToken()));
list.add(user);
}
return list;
}
@Override
public String convertToString(Map context, Object o) {
List<User> user=(List<User>)o;
String str="";
for(User u:user)
{
str+=" 姓名:"+u.getUsername()+" 密码:"+u.getPassword()+" 年龄:"+u.getAge()+"^W^";
}
System.out.println(str);
return str;
}
}
需要在UserAction-conversion.properties中添加这样几句:
list=com.soft.gread1.converter.UserConverter
注意:全局变量则是这样 xwork-conversion.properties
内容key:是类的名字 value:com.soft.gread1.converter.UserConverter
package com.soft.gread1.action;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.soft.gread1.model.*;
public class UserAction extends ActionSupport {
private List<User> list;
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
package com.soft.gread1.model;
public class User {
private String username;
private String password;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<s:form action="login" method="post">
<s:textfield name="list" label="用户信息1"/>
<s:textfield name="list" label="用户信息2"/>
<s:textfield name="list" label="用户信息3"/>
<s:submit value="提交"></s:submit>
</s:form>
</html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>成功!!!</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<s:debug></s:debug>
<s:property value="list"/>
</body>
</html>