java 使用struts2从jsp页面的隐藏字段获取arraylist到动作类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12976963/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:02:03  来源:igfitidea点击:

Getting arraylist from jsp page's hidden field to action class using struts2

javalistjspstruts2hidden-field

提问by arvin_codeHunk

I have an arraylist which i have set into jsp's hiddenField.Now I have to access this list in my action class. Below,code snippets are as follows

我有一个数组列表,我已经将它设置到了 jsp 的 hiddenField 中。现在我必须在我的操作类中访问这个列表。下面,代码片段如下

My action Class

我的行动课

private ArrayList<TXT_File_Action> statusResult_list=new ArrayList<TXT_File_Action>();
    private ArrayList<TXT_Beans> regenerateTXTList=new ArrayList<TXT_Beans>();
    private ArrayList<TXT_Beans> pagingList=new ArrayList<TXT_Beans>();
    private ArrayList<TXT_Beans> serverList=new ArrayList<TXT_Beans>();



public String getGenerateList()
    {

          for(int j=0;j<customers_accountList.size();j++)
               {

               dataList=txt_managerInstance.regenerateListData(id_no);  
               regenerateTXTList.add(dataList.get(0));
           }

               pagingList=getRegenerateTXTList();
               setRegenerateTXTList(getRegenerateTXTList());
               setPagingList(getPagingList());

        return SUCCESS;
      }

  getters..n setters

My JSP code is

我的 JSP 代码是

 <s:iterator value="pagingList">
<tr>
     <td align="center"><s:property value="customerId" /></td>  
     <td align="center"><s:property value="cspId" /></td>    
     <td align="center"><s:property value="branchCode" /></td>
     <td align="center" id="bcID"><s:property value="bcCode"/></td>
 </tr>

</s:iterator>

and below I set the list in jsp hidden field like this :

下面我在jsp隐藏字段中设置列表,如下所示:

   <input type="hidden" name="serverList" id="serverList"  value="<s:property value="pagingList"/>"/>

Now I want this list in my action class when I click to an event. Whenever I tried to print the size of this list serverListin my action I got following error:

现在,当我单击某个事件时,我希望在我的操作类中使用此列表。每当我尝试在操作中打印此列表serverList的大小时,都会出现以下错误:

java.lang.ArrayIndexOutOfBoundsException: -1
    java.util.ArrayList.get(Unknown Source)
    com.alw.action.TXT_File_Action.setPaginationList(TXT_File_Action.java:424)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
        .
        .
        .

But I sure i get all Arrayllist data in this hidden field because I have checked this on my page

但我确定我在这个隐藏字段中得到了所有的 Arraylllist 数据,因为我已经在我的页面上检查过了

**<input type="hidden" name="serverList" id="serverList"  value="<s:property value="pagingList"/>"/>**

what is going wrong with this, How to get this list in my action class. any help would be very helpfull.

这有什么问题,如何在我的动作类中获取此列表。任何帮助都会非常有帮助。

回答by Jaiwo99

You cannot put a Listor any Objectdirectly into a inputor s:hidden, here is the DOCUMENT, attribute valueshould be a string. If you intend to do this, you should have to use Struts2 Type Conversion.

您不能将 aList或 anyObject直接放入 a inputor s:hidden,这里是DOCUMENT,属性value应该是一个字符串。如果你打算这样做,你应该使用Struts2 Type Conversion

The easiest way to solve your problem is to put your list into session. here is the link SessionAware.

解决问题的最简单方法是将列表放入会话中。这是SessionAware链接。

回答by Satheesh Cheveri

You are not supposed to an java variable to html field for purpose of caching/storing. Since you are using Struts, you could have store this ArrayList variable as part of your form bean.

您不应该为了缓存/存储的目的将 java 变量添加到 html 字段。由于您使用的是 Struts,因此您可以将此 ArrayList 变量存储为表单 bean 的一部分。