分类: 系统运维
2012-01-20 14:32:49
input.jsp
<%@ page contentType = "text/html" pageEncoding="gbk"%>
<html>
<body>
使用分号;隔开用户名和密码
<form action = "userAction2.action">
user info1:<input type = "text" name = "user"><br>
user info2:<input type = "text" name = "user"><br>
user info3 :<input type = "text" name = "user"><br>
user info4 :<input type = "text" name = "user"><br>
<input type = "submit" value = "submit">
form>
body>
html>
struts.xml部分:
action>
<action name = "userAction2" class = "com.shengsiyuan.struts2.UserAction2">
<result name = "SUCCESS">/output.jspresult>
action>
UserAcion2-conversion.properties
user=com.shengsiyuan.converter.UserConverter3
UserConverter3.java
package com.shengsiyuan.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.shengsiyuan.bean.User;
public class UserConverter3 extends StrutsTypeConverter
{
@Override
public Object convertFromString(Map arg0, String[] values, Class toClass)
{
List
for(String value : values)
{
StringTokenizer st = new StringTokenizer(value,";") ;
User user = new User() ;
user.setUsername(st.nextToken()) ;
user.setPassword(st.nextToken()) ;
list.add(user) ;
}
return list ;//返回后自动复制给Action2中List
}
@Override
public String convertToString(Map arg0, Object o)
{
List
StringBuffer buffer = new StringBuffer() ;
for(User user : list)
{
String username = user.getUsername() ;
String password = user.getPassword() ;
buffer.append("username:").append(username).append(",password:").append(password) ;
}
return buffer.toString() ;
}
}
UserActoin2.java
package com.shengsiyuan.struts2;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.shengsiyuan.bean.User;
public class UserAction2 extends ActionSupport
{
private List
public List
{
return user;
}
public void setUser(List
{
this.user = user;
}
@Override
public String execute() throws Exception
{
for(User u :user)
{
System.out.println(u.getUsername()+","+u.getPassword());
}
return "SUCCESS" ;
}
}
struts.xml部分
output.jsp
<%@ page language="java" pageEncoding="gbk"%>
<%@taglib prefix = "s" uri = "/struts-tags" %>
<html>
<body>
<s:property value = "user"/>
body>
html>
User.java
package com.shengsiyuan.bean;
public class User
{
private String username ;
private String password ;
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;
}
}