简体   繁体   English

我们如何将列表对象从jsp传递到struts2动作?

[英]How can we pass list object from jsp to struts2 action?

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. 每当我提交表单时,此empList对象都不会传递给Action类。 I want this List Object for some other processing. 我希望此列表对象进行其他处理。

How can i pass this list object to Action class? 如何将此列表对象传递给Action类? Note: Here I'm not using any of List Object fields as Named parameters in fields. 注意:在这里,我没有使用任何列表对象字段作为字段中的命名参数。

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>

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提供的解决方案是正确的,但可以改进它,而无需隐藏值,您可以像在隐藏中给定的字段一样给字段名称

<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>

empList object will be not passed. empList对象将不会传递。 Struts2 will pass your selected form values to your action class. 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中即可。您可以从Action类的session中检索emplist对象的值并进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM