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

风流意气都作罢 提交于 2019-12-11 07:45:13

问题


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
}

回答1:


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.




回答2:


Try the following by adding a converter for the ID type to the xwork-conversion.properties file

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


来源:https://stackoverflow.com/questions/17898920/how-to-convert-field-of-the-list-item-via-custom-struts-type-converter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!