且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么 Struts2 将我的字符串转换为字符串数组?

更新时间:2023-11-14 13:10:34

如果您遵循 Java bean 约定,则没有问题.

There are no issues if you follow Java bean conventions.

以下是解决问题的一些指南:

Here are some guidelines to resolve the issue:

  • 如果您将私有成员命名为过滤器"并仅提供过滤器的 getter,则不会发生此问题.
  • 如果您在 getter 之外还提供公共 setter(即使您为私有成员使用不同的名称,例如 m_filter),则不会发生此问题
  • 此问题仅在您不提供 setter 并且该属性与 getter 名称不同时才会发生.

长话短说遵循惯例.以上行为已用 Struts 2.3.4 测试

Long story short follow conventions. Above behaviour tested with Struts 2.3.4

我猜正在发生什么:getter 也允许设置(如果只有一个 setter,你只能在地图中设置一个项目,对于当前的参数拦截器,这是案件).检查 bean 的属性以查看它应该如何进行类型转换,可能首先它会查找失败的 setter,然后查看该名称的属性的操作以确定失败的类型,因为它需要使用默认值.默认参数类型是 String 到 String[] 的映射,这就是您所看到的.

What I'm guessing is happening: The getter allows for setting too (If there was only a setter you could only set one item in the map, well with the current parameter interceptor this is the case). The bean is inspected for the property to see how it should do type conversion probably first it looks for a setter failing that it looks at the action for a propterty of that name to determine the type failing that it will need to use the default. The default parameter type is a mapping of String to String[] and this is what you are seeing.