Struts2 转换枚举数组用单个空值填充数组

发布于 2024-09-05 14:47:41 字数 638 浏览 1 评论 0原文

对于具有这些成员变量的简单操作类:

...
private TestConverterEnum test;
private TestConverterEnum[] tests;
private List<TestConverterEnum> tList;
...

和一个简单的枚举:

public enum TestConverterEnum {
    A,
    B,
    C;
}

使用默认的 struts2 枚举转换,当我像这样发送请求时:

TestConterter.action?test=&tests=&tList=&b=asdf

对于 test 我得到一个 null值,这是预期的。然而,对于数组和列表,我得到带有一个空元素的数组(或列表)。这是我们所期望的吗?有没有办法防止这种情况。

我们最近升级了struts2版本,我们有自己的转换器,在这种情况下也不起作用,所以我希望使用默认的转换方法。我们已经有了验证这些数组的 null 和长度的代码,并且我不想向这些分支添加另一个条件。有办法阻止这种行为吗?

For a simple action class with these member variables:

...
private TestConverterEnum test;
private TestConverterEnum[] tests;
private List<TestConverterEnum> tList;
...

And a simple enum:

public enum TestConverterEnum {
    A,
    B,
    C;
}

Using the default struts2 enum conversion, when I send the request like so:

TestConterter.action?test=&tests=&tList=&b=asdf

For test I get a null value, which is expected. For the Array and List, however, I get and Array (or list) with one null element. Is this what is expected? Is there a way to prevent this.

We recently upgraded our struts2 version, and we had our own converters, which also don't work in this case, so I was hoping to use the default conversion method. We already have code that is validating these arrays for null and length, and I don't want to have to add another condition to these branches. Is there a way to prevent this bevavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So尛奶瓶 2024-09-12 14:47:41

是的,这是国际海事组织的预期行为。

HTTP 参数原则上是多值的(通常在多值选择列表中使用),因此 struts2 参数拦截器要求(通过反射)接受数组或(作为第二个选项)单个值的设置器。您的操作可能有一个 setTests(TestConverterEnum[] a) setter,因此,当 Struts2 的拦截器必须映射 tests= 时,他会找到该 setter,并自言自语:“我必须用一个数组设置这个'tests'属性,让我们看看传递了多少个参数......只有一个(空),然后让我们创建一个长度为1的数组并用转换填充它......” 等。

我认为这是合理的:您的请求包含一个“测试”参数(尽管为空),因此创建了一个大小为一的数组。

我敢打赌,如果您尝试 TestConterter.action?tests=&tests= 将创建一个长度为 2 的数组。

更新

让我们看看我们是否同意这一点:在请求中传递空参数(原则上)与不传递它相同......尽管经常是这样希望将这两种情况视为同等。

如果你想告诉 Struts2:“完全忽略空参数”...我不认为(假设这是一个可以接受的想法)这种行为可以通过某些全局设置来强制执行。
您始终可以编写自己的拦截器(或扩展 Parameters 拦截器),以便在映射请求之前清除请求中的空参数。

顺便说一句,请注意,这个问题与枚举或数组无关,是完全普遍的。

现在,多值参数的情况需要额外考虑。我假设如果您的 Action 有一个数组类型的公共属性,那是因为它可以使用多个参数进行设置(通常是 SELECT MULTIPLE) - 如果没有,则这里存在一些设计问题。
现在,您对此请求的预期行为是什么?

TestConterter.action?tests=&tests=A&tests=&tests=B&tests=B

如果这会生成长度为 5 [null, A, null, B, B] 或 3 [A, B, B] 或 2 [A, B] 还是什么?
很难给出一般标准;这就是为什么一般来说,最好在 setter 方法中处理这个问题。

Yes, it is expected behaviour IMO.

Http parameters are in principle multivalued (typically used in multivalued select lists), hence the struts2 param interceptor asks (by reflection) for setters that accept arrays or (as a second option) a single value. Your action probably has a setTests(TestConverterEnum[] a) setter, so, when Struts2's interceptor has to map tests= he founds that setter, and thinks to himself: "I must set this 'tests' property with an array, let's see how many parameters were passed... only one (empty), then let's create an array of length one and fill it with the convertion..." etc.

I find it reasonable: your request included one 'tests' parameter (though empty), hence a one-sized array was created.

I'd bet that if you try TestConterter.action?tests=&tests= an array of length 2 will be created.

Updated:

Let's see if we agree in this : to pass an empty parameter in a request is (in principle) not the same as not passing it... though frequently one wants to treat both cases as equivalent.

If you'd want to tell Struts2: "ignore completely the empty parameters"... I don't think (supposing it is an acceptable idea) this behaviour can be enforced by some global setting.
You could always write your own interceptor (or extend the Parameters interceptor) so that it cleans out the empty parameters from the request before mapping them.

BTW, note that this issue is unrelated to Enums or to Arrays, is totally general.

Now, the case of multivalued parameters requires extra consideration. I assume that if your Action has a public property of type array, it's because it can be set with several parameters, (a SELECT MULTIPLE typically) - if not, there is some design problem here.
Now, what would be your expected behaviour for this request?

TestConterter.action?tests=&tests=A&tests=&tests=B&tests=B

Should this generate an array of length 5 [null, A, null, B, B] or 3 [A, B, B] or 2 [A, B] or what?
It's difficult to give general criteria; that's why this is better dealt with in your setter method, in general.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文