struts2类型转换器

Posted JustDo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

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

下图是项目文件图:

以上是关于struts2类型转换器的主要内容,如果未能解决你的问题,请参考以下文章

struts2类型转换中的错误处理

struts2_struts类型转换

Struts2类型转换

在代码片段中包含类型转换

struts2学习笔记之十一:struts2的类型转换器

struts2中的类型转换,自定义类型转换器需要继承啥类