简体   繁体   中英

Struts 2 type converter issue

I am trying to use custom type conversion with property file for action.

Action class is implementing ModelDriven for bean properties.

public class WelcomeAction extends ActionSupport implements ModelDriven<MyBean>{

public String execute(){
    return SUCCESS;
}

private MyBean bean = new MyBean();

@Override
public MyBean getModel() {
    return bean;
}
}

Bean class is:

public class MyBean{

private Rectangle rectangle;
public Rectangle getRectangle() {
    return rectangle;
}

public void setRectangle(Rectangle rectangle) {
    this.rectangle = rectangle;
}

}

and I have WelcomeAction-conversion.properties file parallel to action class with converter config as:

bean.rectangle=struts2.typeconverters.RectangleTypeConverter

I tried putting key as bean, rectangle etc but its not working, its not using converter class.

If i use @TypeConverter annotation or global converter then its working fine.

My struts 2 version is 2.3.15.1, any idea what could be the issue.

UPDATE: Created an issue https://issues.apache.org/jira/browse/WW-4249

Got the correct way for implementation: http://www.journaldev.com/2221/struts-2-ognl-tutorial-with-custom-type-converter-example

You need to create xwork-conversion.properties in your class path. In the file, you will map the full class name of Rectangle class to the Converter class. Follow the examples in the urls below. Check this and this posts. They will help you in resolving the issues.

bean.rectangle=struts2.typeconverters.RectangleTypeConverter - This is wrong

You have to use the proper class names along with fullpath, not just property names.

mypackage.Rectangle=struts2.typeconverters.RectangleTypeConverter - hope you really have a package named struts2, although I'll strictly avoid such a package name.

If your Action class is using Model Driven then you need to follow this methodology for custom type convertor:

http://struts.apache.org/release/2.3.x/docs/type-conversion.html (see section titled Applying a Type Converter to a bean or model).

In your example I changed the conversion properties file name to MyJavaBean-conversion.properties and placed it under the same package as the MyJavaBean.java.

In MyJavaBean-conversion.properties I changed the key to:

      rectangle=com.journaldev.struts2.typeconverters.RectangleTypeConverter 

I then built the .war file and ran your example under Tomcat 7. The rectangle conversion worked correctly.

I don't think you could ever use the methodology explained at http://struts.apache.org/release/2.3.x/docs/type-conversion.html (see section titled Applying a Type Converter to an Action) when your Action class is using ModelDriven. If you find a previous Struts 2 version where that did work let me know.

Bruce Phillips

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