Struts2(批量类型转换器)

package com.wildcat.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.wildcat.bean.User;

public class UserConverter3 extends StrutsTypeConverter {

 @Override
 public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
  
  List <User> list=new ArrayList<User>();
  User user=new User();
  for (String value:arg1){
   StringTokenizer ST=new StringTokenizer(value,";");
   String username=ST.nextToken();
   String password=ST.nextToken();
   user.setPassword(password);
   user.setUsername(username);
   list.add(user);
   
   
  }
  return list;
 }

 @Override
 public String convertToString(Map arg0, Object arg1) {
  List<User> list=(List<User>)arg1;
  StringBuffer buffer=new StringBuffer();
  
  for (User u:list){
   String username=u.getUsername();
   String password=u.getPassword();
   buffer.append("姓名: ").append(username).append("Password: ").append(password);
   
   
  }
  return buffer.toString();
 }

}
 

你可能感兴趣的:(struts,批量类型转换器)