【问题标题】:Struts Hidden field does not hold the valuesStruts 隐藏字段不保存值
【发布时间】:2014-09-19 06:47:02
【问题描述】:

我有一个网页显示具有以下数据结构的值列表。

Class MyBean 
private List<NewObj> newList;
getNewList();
setNewList(List);

Class NewObj
private String name;
private List<CustObj> custList;
getCustList();
setCustList(List);

Class CustObj
private String age;

我能够遍历列表并显示值。我在两者之间包含了隐藏的标签,以便我可以从 Form 中获取 Action 类中的值。

JSP:

<s:iterator value="myBean.newList" status="matStat">
    <s:hidden name="myBean.newList[%(#matStat.index)].name"></s:hidden>
    <tr>
       <td><s:property value="name" /></td>
    </tr>
    <s:iterator value="custList" status="reqStatus">
     <s:hidden name="myBean.newList[%(#matStatus.index)].custList[%(#reqStatus.index)].requestId">         </s:hidden>
     <tr>
        <td><s:property value="age" /></td>
     </tr>
   </s:iterator>
</s:iterator>

当我尝试从我的操作类访问值时,该值为 NULL,我的意思是对象 myBean 为 NULL。

动作类:

myBean.getNewList()

由于 myBean 对象为空,因此我在上面的行中获取了 NPE。仅供参考,我在动作类中有 myBean 的 getter 和 setter。

来自浏览器的 JSP 源代码: 以下是我通过查看查看页面源代码

从浏览器中获取的内容摘录
<input type="hidden" name="myBean.newList[%(#matStat.index)].name" value="" id="myForm_myBean_newList[%(#matStat.index)].name"/>

name的值实际上应该是myBean.newList[0].name

【问题讨论】:

  • 嗯,M 和 V 看起来不错。 C呢? :)
  • 你在哪里看到%(这种表情?
  • 你在编码什么语言?
  • @ne1410s:我无法理解你的问题。你能详细说明一下吗?
  • @AleksandrM/Roman C:我正在使用 Struts2,这就是我过去使用 struts-iterator 在 jsp 中迭代值的方式。隐藏标签 用于设置屏幕中的值,以便在 Action 类中可用。如果我错了,请纠正我。另外,请指出任何相同的示例代码。

标签: jsp struts2


【解决方案1】:

感谢您的回复。

在对每个单词进行分析后,我发现我使用的表达方式是错误的。 我使用 () 而不是 {} 来访问 struts 迭代器中的列表索引。更正后的 JSP 如下所示。现在,它工作正常。

<s:iterator value="myBean.newList" status="matStat">
<s:hidden name="myBean.newList[%{#matStat.index}].name"></s:hidden>
<tr>
   <td><s:property value="name" /></td>
</tr>
<s:iterator value="custList" status="reqStatus">
 <s:hidden name="myBean.newList[%{#matStatus.index}].custList[%{#reqStatus.index}].requestId">         </s:hidden>
 <tr>
    <td><s:property value="age" /></td>
 </tr>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多