zoukankan      html  css  js  c++  java
  • struts2类型转换器

    类型转换器分为全局类型转换器和局部类型转换器。全局类型转换针对所有Action都起作用,而局部类型转换器针对某个Action

    一、全局类型转换器。将字符串x,y转换成Point

    Login.jsp页面

    <s:form method="post" action="test">
            <s:textfield name="username" label="账号"></s:textfield>
            <s:password name="password" label="密码"></s:password>
            <s:textfield name="point" label="坐标"></s:textfield>
            <s:submit></s:submit>
        </s:form>

    定义实体类

    package models;
    
    public class Point {
    
        private int x;
        private int y;
        public int getX() {
            return x;
        }
        public void setX(int x) {
            this.x = x;
        }
        public int getY() {
            return y;
        }
        public void setY(int y) {
            this.y = y;
        }
        
        public Point() {
        }
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
        
    }
    Point.java
    package models;
    
    public class User {
    
        private String username;
        private String password;
        private Point point;
        
        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 Point getPoint() {
            return point;
        }
        public void setPoint(Point point) {
            this.point = point;
        }
        
    }
    User.java

    定义Action类

    package action;
    
    import com.opensymphony.xwork2.ModelDriven;
    
    import models.Point;
    import models.User;
    
    public class Test implements ModelDriven<User> {
    
        private User user=new User();
        public String execute(){
            System.out.println(user.getPoint());
            return "success";    
        }
        public User getModel() {
            // TODO Auto-generated method stub
            return user;
        }
    }
    Test.java

    定义类型转换器

    package coverter;
    
    import java.util.Map;
    
    import models.Point;
    
    import org.apache.struts2.util.StrutsTypeConverter;
    
    public class PointConverter extends StrutsTypeConverter{
    
        @Override
        public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
            Point point=null;
            if(arg1.length>0){
                point=new Point();
                String[] cods=arg1[0].split(",");
                point.setX(Integer.parseInt(cods[0]));
                point.setY(Integer.parseInt(cods[1]));
            }
            return point;
        }
    
        @Override
        public String convertToString(Map arg0, Object arg1) {
            Point point=(Point)arg1;
            return point.getX()+","+point.getY();
        }
    
    }
    PointConverter.java

    在src下新建一个xwork-conversion.properties文件,内容如下:

    models.Point=coverter.PointConverter

    前面是需要类型转换的类,后面是转换器

    二、局部类型转换器

    定义两个日期类型转换器

    package coverter;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Map;
    
    import org.apache.struts2.util.StrutsTypeConverter;
    
    public class DateConverter1 extends StrutsTypeConverter{
    
        private static final SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
        @Override
        public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
            Date date=null;
            if(arg1.length>0){
                try {
                    date=sdf.parse(arg1[0]);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return date;
        }
    
        @Override
        public String convertToString(Map arg0, Object arg1) {
            Date date=(Date)arg1;
            return sdf.format(date);
        }
    }
    DateConverter1.java
    package coverter;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Map;
    
    import org.apache.struts2.util.StrutsTypeConverter;
    
    public class DateConverter2 extends StrutsTypeConverter{
    
        private static final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        @Override
        public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
            Date date=null;
            if(arg1.length>0){
                try {
                    date=sdf.parse(arg1[0]);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return date;
        }
    
        @Override
        public String convertToString(Map arg0, Object arg1) {
            Date date=(Date)arg1;
            return sdf.format(date);
        }
        
    }
    DateConverter2.java

    在User.java文件中增加一个createDate属性

        private Date createDate;
        
        public Date getCreateDate() {
            return createDate;
        }
        public void setCreateDate(Date createDate) {
            this.createDate = createDate;
        }

    在Test.java中增加一个endDate属性

        private Date endDate;
        public Date getEndDate() {
            return endDate;
        }
        public void setEndDate(Date endDate) {
            this.endDate = endDate;
        }

    最后在Test.java同目录下增加一个Test-conversion.properties(命名规则:Action类名-conversion.properties)

    endDate=coverter.DateConverter1
    user.createDate=coverter.DateConverter2

    配置规则:属性名=包名...转换器

    下图是项目文件图:

  • 相关阅读:
    亲测——pycharm下运行第一个scrapy项目 ©seven_clear
    [转]pycharm的一些快捷键
    一个豆瓣API的使用——拒绝思维定式
    小试牛刀--利用豆瓣API爬取豆瓣电影top250
    Python GUI编程--Tkinter
    多线程详解
    K8S+GitLab-自动化分布式部署ASP.NET Core(一) 部署环境
    初学者浅度剖析eShopOnContainers 里面用到的MediatR .
    通过Task异步加快对数组的运算
    为什么是容器?
  • 原文地址:https://www.cnblogs.com/caoyc/p/5584642.html
Copyright © 2011-2022 走看看