Struts2将列表数据从JSP发送到Action类

Struts2将列表数据从JSP发送到Action类

本文介绍了Struts2将列表数据从JSP发送到Action类:替代方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向同事解释了如何通过使用索引从JSP页面获取列表数据返回动作类的方法和。他不太理解并且自己摸索了一下,直到他突然通过不使用索引使它成功!

I was explaining to a colleague the way of getting list data from a JSP page to back to the action class by using indices as explained here and here. He didn't quite understand and fumbled a bit on his own until he suddenly he made it work by not using indices at all!

在他的JSP页面中,他有:

In his JSP page he had:

<input type="checkbox" name="contactNameList" value="someValue1">
<input type="checkbox" name="contactNameList" value="someValue2">
<input type="checkbox" name="contactNameList" value="someValue3">
<input type="checkbox" name="contactNameList" value="someValue4">

在他的动作课上,他有'适当的'安装者:

In his action class he had the 'appropiate' setters:

public List<String> getContactNameList()

public void setContactNameList(List<String> list)

我很困惑为什么这项工作。我 认为 这是有效的,因为他正在发送非bean数据(在本例中为字符串),并且有一个智能构建到Struts2 / OGNL中以将值附加到列表而不是覆盖他们。

I'm baffled as to why this work. I think this works because he is sending non-bean data (in this case strings) and there is an intelligence build into Struts2/OGNL to append values to lists rather than overwrite them.

任何人都能详细解释这个无指数案件背后的问题吗?如何使用上面的代码段实例化和填充字符串列表?

Can anybody explain with great detail what is going behind the hood in this "no indices" case? How is the list of strings instantiated and populated with the snippets above?

推荐答案

您应该了解bean数据而不是bean数据是作为参数传递给动作。如果您实现 ParameterAware ,则参数具有一个结构。

You should understand that bean data and not bean data are passed as parameters to the action. The parameters has a structure that you can find if you implement ParameterAware.

然后XWork Type Conversion尽最大努力将此地图转换为bean属性。请参阅。

Then XWork Type Conversion make its best to convert this map to beans properties. See Built in Type Conversion Support.

在无索引的情况下,参数映射在一个键而不是索引名称以其自己的名称使用。

In the no indexes case parameters are mapped under the one key, rather than indexed names are used under their own names.

这篇关于Struts2将列表数据从JSP发送到Action类:替代方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 22:23