Java 我们如何将列表对象从 jsp 传递给 struts2 动作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24710785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can we pass list object from jsp to struts2 action?
提问by 1097733
I'm facing one issue in Struts2, Please find below information.
我在 Struts2 中遇到一个问题,请查找以下信息。
Java Code:
Java代码:
public class Employ{
int empNo;
String empName;
}
class EmployAction{
List<Employ> empList=new ArrayList<Employ>();
int eno;
String ename;
}
<s:iterator value="empList">
<s:textfield name="eno" value="%{empNo}"/>
<s:textfield name="eName" value="%{empName}"/>
</s:iterator>
First I'm doing search operation and getting all employs list and iterating in JSP.
首先,我正在执行搜索操作并获取所有员工列表并在 JSP 中进行迭代。
Whenever I'm submitting form this empList
object is not passing to Action class. I want this List Object for some other processing.
每当我提交表单时,这个empList
对象都不会传递给 Action 类。我想要这个列表对象进行其他一些处理。
How can i pass this list object to Action class? Note: Here I'm not using any of List Object fields as Named parameters in fields.
如何将此列表对象传递给 Action 类?注意:这里我没有使用任何列表对象字段作为字段中的命名参数。
采纳答案by 1097733
After long investigation found solution and fixed this issue. This solution might be helpful to anyone else.
经过长时间的调查找到了解决方案并修复了这个问题。此解决方案可能对其他人有所帮助。
<s:iterator value="empList" status="emp">
<s:textfield name="eno" value="%{empNo}"/>
<s:textfield name="eName" value="%{empName}"/>
<s:hidden name="empList[%{#emp.index}].eno" value=%{empNo}/>
<s:hidden name="empList[%{#emp.index}].ename" value=%{empName}/>
</s:iterator>
回答by Abhijeet Panwar
empList object will be not passed. Struts2 will pass your selected form values to your action class.
empList 对象不会被传递。Struts2 会将您选择的表单值传递给您的操作类。
If you want to use that empList object to action class,just put that empList object to Session .You can retrive value of emplist object from session in your action class and can process it.
如果您想将该 empList 对象用于操作类,只需将该 empList 对象放入 Session 即可。您可以从操作类中的会话中检索 emplist 对象的值并可以对其进行处理。
回答by Complicated
The solution that Ravi gave is correct, but it can be improved no need for hidden value you can give the name of the field like what you gave in hidden
Ravi 给出的解决方案是正确的,但是可以改进不需要隐藏值,您可以像在 hidden 中给出的那样给出字段的名称
<s:iterator value="empList" status="emp">
<s:textfield name="empList[%{#emp.index}].eno" value="%{empNo}"/>
<s:textfield name="empList[%{#emp.index}].ename" value="%{empName}"/>
</s:iterator>