Chinaunix首页 | 论坛 | 博客
  • 博客访问: 335290
  • 博文数量: 96
  • 博客积分: 2041
  • 博客等级: 大尉
  • 技术积分: 1080
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-20 14:08
文章分类

全部博文(96)

文章存档

2015年(2)

2013年(1)

2012年(93)

分类: 系统运维

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 list = new ArrayList() ;

              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 ;//返回后自动复制给Action2List里的user

       }

 

       @Override

       public String convertToString(Map arg0, Object o)

       {

              List list = (List)o ;

              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 user ;

 

    public List getUser()

    {

       return user;

    }

 

    public void setUser(List user)

    {

       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

 

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;

    }

 

}

阅读(864) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~