Struts2 +获取参数值()
我能够使其在 Struts 中工作,但在 Struts2 中我无法理解问题是什么,我得到的结果为 null。
我的 Action 代码:
String[] stuff = request.getParameterValues("stuff");
if (stuff != null) {
for (int i = 0; i < stuff.length; i++) {
Integer id = new Integer(stuff[i]);
System.out.println("stuff id: " + id);
Stuff stuffObj = stuffService.find(id);
System.out.println("stuff name: " + stuffObj.getStuffName());
}
}
我的 JSP 代码:
<s:form action="add-menus" method="POST" enctype="multipart/form-data" theme="simple">
Stuff <s:checkboxlist name="stuff" list="stuffList.{stuffName}"/>
</s:form>
我也使用了该方法,带有 setter 和 getter,同样的事情,我从 JSP 中得到 null 结果,这可以与 Struts2 一起使用吗? PS我正在尝试获取用户选择的复选框
I was able to make this to work in Struts, but in Struts2 I can not understand what the problem is, I am getting the null as result.
My Action code:
String[] stuff = request.getParameterValues("stuff");
if (stuff != null) {
for (int i = 0; i < stuff.length; i++) {
Integer id = new Integer(stuff[i]);
System.out.println("stuff id: " + id);
Stuff stuffObj = stuffService.find(id);
System.out.println("stuff name: " + stuffObj.getStuffName());
}
}
My JSP code:
<s:form action="add-menus" method="POST" enctype="multipart/form-data" theme="simple">
Stuff <s:checkboxlist name="stuff" list="stuffList.{stuffName}"/>
</s:form>
Also I used the method, with setter and getter, the same thing, I am getting the null result from JSP, is this working with Struts2? P.S. I am trying to get the checkboxes that was selected by the user
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议也许在你的 struts 操作类中实现 ParameterAware。这将允许您以映射的形式遍历 servlet 请求参数:
I would suggest perhaps implementing ParameterAware in your struts action class. This will allow you to traverse the servlet request parameters as a map:
我不确定你想做什么,也不知道你为什么要使用它。
Struts2 具有开箱即用的功能,可以将数据传输到您的操作类/bean,所有您都是操作类中受人尊敬的 setter 方法。
例如,您的 jsp 中有此复选框列表:
在您的操作类中,您需要类似这样的内容
如果选中了多个选项,则可以通过 String 对象存储它。
I am not sure what you are trying to do and morover why you are using this.
Struts2 has out of the box functionality to transfer data to your action class/bean, all you are the respected setter method in your action class.
For e.g you have this checkbox list in your jsp:
in your action class all you need something like this
If multiple options are checked, you can store it via a String object.
嗯,比我想象的要简单,Struts2确实是一个强大的框架,有些事情看起来比你想象的更简单。我能够通过将“东西”声明为来获取选定的框
,瞧,这里我们有选定框的列表。哦,是的,还有 getter 和 setter 方法。就这样。
Well, was easier than I thought, Struts2 is really a powerful framework, some things seem more simpler than you think. I was able to get selected boxes by declaring the "stuff" as
and voila, here we have the list of selected boxes. Oh yes, and the getter and setter methods. That's all.
我使用此代码来获取复选框值字段中的值。这些复选框的 id
field id = "Checkbox"
。String[]
值将仅包含勾选复选框的那些值。此类扩展
ActionSupport
并实现HttpServletRequest
、HttpServletResponse
I used this code to get the values which were there on the checkboxes' value field. These checkboxes had the id
field id = "Checkbox"
. TheString[]
values will only contain those values for which the checkbox is ticked.This class extends
ActionSupport
and implementsHttpServletRequest
,HttpServletResponse