Struts2 将列表数据从 JSP 发送到 Action 类:替代方式 [英] Struts2 sending list data from JSP to Action class: alternative way

本文介绍了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.


Note that all parameter values for a given name will be returned, so the type of the objects in the map is java.lang.String[].

然后 XWork Type Conversion 会尽力将此映射转换为 beans 属性.请参阅内置类型转换支持.

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

框架中的常规类型转换是透明的.一般来说,您需要做的就是确保 HTML 输入的名称可以在 OGNL 表达式中使用.(HTML 输入是表单元素和其他GET/POST 参数.)

Routine type conversion in the framework is transparent. Generally, all you need to do is ensure that HTML inputs have names that can be used in OGNL expressions. (HTML inputs are form elements and other GET/POST parameters.)


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 类:替代方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
发送“验证码”获取 | 15天全站免登陆