简体   繁体   中英

How to convert field of the list item via custom Struts type converter?

I need to implement custom conversion for ID field in Company and Employee classes. I have already implemented custom converter extended from StrutsTypeConverter and it is successfully used to convert Company.ID field, but it does not work for Employee.ID .

Seems like the main problem is in conversion properties file. How should I specify converter class for employee ID field in conversion properties file?

MyAction-conversion.properties :

company.id = com.struts2.convertors.MyCustomConverter
company.??????.id = com.struts2.convertors.MyCustomConverter

MyAction :

public class MyAction extends ActionSupport {

    private Company company;

    public Company getCompany () {
        return company;
    }
    public void setCompany (Company company) {
        this.company= company;
    }
    @Override
    public String execute() {
        return SUCCESS;
    }
}

Company :

public class Company {

    private ID id;

    private List<Employee> employees;

    // getters and setters
}

Employee

public class Employee{

    private ID id;

    private String name;

    // getters and setters
}

TypeConversion Annotation :

This annotation is used for class and application wide conversion rules.

The TypeConversion annotation can be applied at property and method level.

 @TypeConversion(converter = “com.test.struts2.MyConverter”)
 public void setAmount(String amount) 
 {
    this.amount = amount;
 }

This annotation specifies the location of one of my converters. literally, by using this annotation, I register my class com.test.struts2.MyConverter as a converter, and gets executed every time when setAmount(String amount) method is invoked.

通过将ID类型的转换器添加到xwork-conversion.properties文件来尝试以下操作

com.struts2.ID = com.struts2.convertors.MyCustomConverter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM