Struts2类型转换之批量类型转换

Terwer Struts2评论23字数 2622阅读8分44秒阅读模式

输入页面 input2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Struts2.3 converter</title>
</head>
<body>



    <form action="userAction2.action">

        user info 1: <input type="text" name="user"/><br/>
        user info 2: <input type="text" name="user"/><br/>
        user info 3: <input type="text" name="user"/><br/>
        user info 4: <input type="text" name="user"/><br/>

        <input type="submit" value="submit"/>

    </form>

</body>
</html>

流程跳转 struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <package name="struts2" extends="struts-default">
        <action name="userAction2" class="space.terwer.struts23.UserAction2">
            <result name="success">/output2.jsp</result>
        </action>
    </package>

</struts>

业务处理 Action 类 UserAction2

package space.terwer.struts23;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import space.terwer.bean.User;

public class UserAction2 extends ActionSupport {

    private List<User> user;

    public List<User> getUser() {
        return user;
    }

    public void setUser(List<User> user) {
        this.user = user;
    }

    @Override
    public String execute() throws Exception {
        for (User u : this.getUser()) {
            System.out.println(u.getUsername() + "," + u.getPassword());
        }

        return SUCCESS;
    }

}

类型转换器 UserAction2-conversion.properties

user=space.terwer.converter.UserConverter3
package space.terwer.converter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.apache.struts2.util.StrutsTypeConverter;

import space.terwer.bean.User;

public class UserConverter3 extends StrutsTypeConverter {

    @Override
    public Object convertFromString(Map context, String[] values, Class toClass) {
        List<User> list = new ArrayList<>();

        for (String value : values) {
            StringTokenizer st = new StringTokenizer(value, ";");

            User user = new User();

            String username = st.nextToken();
            String pasword = st.nextToken();

            user.setUsername(username);
            user.setPassword(pasword);

            list.add(user);
        }

        return list;
    }

    @Override
    public String convertToString(Map context, Object o) {
        List<User> list = (List) o;

        StringBuilder sb = new StringBuilder();
        sb.append("[");

        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            sb.append("{username=" + user.getUsername());
            sb.append(",password=" + user.getPassword());
            sb.append("}");

            if (i < list.size() - 1) {
                sb.append(",");
            }
        }
        sb.append("]");

        return sb.toString();
    }

}

输出结果 output2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>  

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Struts2.3 converter result</title>
</head>
<body>
    <s:property value="user"/>
</body>
</html>

效果

Struts2类型转换之批量类型转换文章源自浅海拾贝-https://blog.terwergreen.com/batch-type-conversion-of-struts2-type-conversion-2wxwac.html

Struts2类型转换之批量类型转换文章源自浅海拾贝-https://blog.terwergreen.com/batch-type-conversion-of-struts2-type-conversion-2wxwac.html

大功告成。与单个不一样的地方只在于把对象换成了 List​ ,其他的类似。文章源自浅海拾贝-https://blog.terwergreen.com/batch-type-conversion-of-struts2-type-conversion-2wxwac.html

文章源自浅海拾贝-https://blog.terwergreen.com/batch-type-conversion-of-struts2-type-conversion-2wxwac.html

相关文章
  • 扫码加我微信
  • 验证消息请输入:来自你的博客
  • weinxin
  • 我的微信公众号
  • 微信扫一扫与我交流吧
  • weinxin
Terwer
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: