简体   繁体   English

Struts 2型转换器问题

[英]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. Action类正在为Bean属性实现ModelDriven。

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: Bean类是:

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: 我将WelcomeAction-conversion.properties文件与action类并行转换为config:

bean.rectangle=struts2.typeconverters.RectangleTypeConverter

I tried putting key as bean, rectangle etc but its not working, its not using converter class. 我尝试将键作为bean,矩形等但是它不起作用,它不使用转换器类。

If i use @TypeConverter annotation or global converter then its working fine. 如果我使用@TypeConverter注释或全局转换器,那么它的工作正常。

My struts 2 version is 2.3.15.1, any idea what could be the issue. 我的struts 2版本是2.3.15.1,任何想法可能是什么问题。

UPDATE: Created an issue https://issues.apache.org/jira/browse/WW-4249 更新:创建了一个问题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 有正确的实施方式: 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. 您需要在类路径中创建xwork-conversion.properties In the file, you will map the full class name of Rectangle class to the Converter class. 在该文件中,您将Rectangle类的完整类名映射到Converter类。 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 bean.rectangle=struts2.typeconverters.RectangleTypeConverter - 这是错误的

You have to use the proper class names along with fullpath, not just property names. 您必须使用正确的类名和fullpath,而不仅仅是属性名称。

mypackage.Rectangle=struts2.typeconverters.RectangleTypeConverter - hope you really have a package named struts2, although I'll strictly avoid such a package name. mypackage.Rectangle=struts2.typeconverters.RectangleTypeConverter - 希望你真的有一个名为struts2的包,虽然我会严格避免这样的包名。

If your Action class is using Model Driven then you need to follow this methodology for custom type convertor: 如果您的Action类正在使用Model Driven,那么您需要对自定义类型转换器使用此方法:

http://struts.apache.org/release/2.3.x/docs/type-conversion.html (see section titled Applying a Type Converter to a bean or model). http://struts.apache.org/release/2.3.x/docs/type-conversion.html (请参阅标题为将类型转换器应用于bean或模型的部分)。

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. 在您的示例中,我将转换属性文件名更改为MyJavaBean-conversion.properties,并将其放在与MyJavaBean.java相同的包中。

In MyJavaBean-conversion.properties I changed the key to: 在MyJavaBean-conversion.properties中,我将密钥更改为:

      rectangle=com.journaldev.struts2.typeconverters.RectangleTypeConverter 

I then built the .war file and ran your example under Tomcat 7. The rectangle conversion worked correctly. 然后我构建了.war文件并在Tomcat 7下运行了您的示例。矩形转换正常工作。

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. 我不认为您可以使用http://struts.apache.org/release/2.3.x/docs/type-conversion.html中解释的方法(请参阅标题为将类型转换器应用于操作的部分) Action类正在使用ModelDriven。 If you find a previous Struts 2 version where that did work let me know. 如果您找到以前的Struts 2版本,那么确实可以使用。

Bruce Phillips 布鲁斯菲利普斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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