在struts2中的同一隐藏变量中传递多个相同类型的值

时间:2020-12-14 16:46:48

At the outset, i am using struts2, java and javascript to get my job done. I have outlined the problem as follows.

首先,我使用struts2,java和javascript来完成我的工作。我已经概述了如下问题。

I have the following table on my page -

我的页面上有下表 -

___________________________
checkbox  |  phone  | Name      
---------------------------
   []     | 123-456 | abcde

   []     | 234-567 | testname
____________________________

(It is an outline, but stay with me)

(这是一个大纲,但留在我身边)

The code for the table is as follows:

该表的代码如下:

<s:if test="reportFilter.huntGroups!=null && reportFilter.huntGroups.size()>0">
    <s:iterator var="huntGroupVO" value="reportFilter.huntGroups" status="huntGroupKey">
      <tr <s:if test="#huntGroupKey.odd">class="oddRows"</s:if>>
        <td width="40px"><s:checkbox id="specHg_checkbox_%{#huntGroupKey.index}" name="reportFilter.selectedHuntGroups"  fieldValue="%{#huntGroupVO.phoneNumber}" value="%{reportFilter.selectedHuntGroups!=null && reportFilter.selectedHuntGroups.contains(#huntGroupVO.phoneNumber)?true:false}" theme="simple"></s:checkbox></td>
      <td>
        <s:if test="#huntGroupVO.phoneNumber.length()>0">
          <s:property value="#huntGroupVO.phoneNumber"/>
        </s:if>
        <s:else>
          <s:property value="#huntGroupVO.userGroupId"/>
        </s:else>
      </td>

      <td class="hntName"><s:property value="#huntGroupVO.name"></s:property></td>
    </tr>
  </s:iterator>
</s:if>

As is evident, i am passing the phone number as value when i tick the check-box. The problem is, i want to pass the name too.

很明显,当我勾选复选框时,我将电话号码作为值传递。问题是,我也想传递这个名字。

I understand that we can pass it along with the phone number using something like a hyphen.

我知道我们可以使用类似连字符的东西将它与电话号码一起传递。

Assuming i have another variable ArrayList<String> hiddenName , i would like to populate the names corresponding to the ticked checkboxes in this variable. (I am thinking via a s:hidden parameter)

假设我有另一个变量ArrayList hiddenName,我想填充对应于此变量中勾选复选框的名称。 (我在想一个s:隐藏参数)

I apologize if it sounds a little confusing, but please help!

如果听起来有点令人困惑,我道歉,但请帮忙!

1 个解决方案

#1


0  

Obviously you can only pass a single value in any given form field.

显然,您只能在任何给定的表单字段中传递单个值。

You have a number of options, from the simple–use a string and split it to get both values, through the middle–pass an ID and retrieve the appropriate values in Java, to the complex–custom type conversion.

您有许多选项,从简单使用字符串并将其拆分为两个值,通过中间传递ID并在Java中检索适当的值,到复杂自定义类型转换。

Which makes the most sense depends a lot on where your data is, how your data is, and so on.

哪种方式最有意义取决于数据的位置,数据的方式等等。


Unrelated, but I find that JSP very difficult to read. I would refactor a significant amount. The following is untested, but much closer to what I would expect after a code review.

不相关,但我发现JSP很难阅读。我会重构很多。以下是未经测试的,但更接近于我在代码审查后的预期。

<s:if test="reportFilter.showGroups">
  <s:iterator value="reportFilter.huntGroups" status="status">
    <tr>
      <td>
        <s:checkbox id="specHg_checkbox_%{#status.index}" theme="simple"
                    name="reportFilter.selectedHuntGroups" 
                    fieldValue="phoneNumber" 
                    value="%{reportFilter.hasNumber(phoneNumber)}" />
      </td>

      <td>
        <s:set var="num" value="${phoneNumber.length() > 0 ? phoneNumber : userGroupId" />
        <s:property value="#num"/>
      </td>

      <td class="hntName"><s:property value="name"/></td>
    </tr>
  </s:iterator>
</s:if>

A few things of note:

一些注意事项:

  • Logic to decide if we'll show groups encapsulated in Java. Makes testing easier, too.
  • 决定我们是否会显示用Java封装的组的逻辑。使测试更容易。

  • Reasonable, shorter name for the iteration object: it's a group (or huntGroup).
  • 迭代对象的合理,较短的名称:它是一个组(或huntGroup)。

  • Extract the (phone number || group ID) value, eliminating a bunch of JSP tags.
  • 提取(电话号码||组ID)值,消除一堆JSP标记。

  • (Ideally that logic would be hidden in Java, either in the VO, or a decorator.)
  • (理想情况下,逻辑将隐藏在Java中,无论是在VO中还是装饰器中。)

  • The iterator tag pushes the iteration object onto the value stack, eliminating the need for directly referencing it by name. This one is a bit more iffy; it depends on your needs.
  • 迭代器标记将迭代对象推送到值堆栈,无需按名称直接引用它。这个有点不确定;这取决于你的需求。

  • In real life I'd probably put the entire group thing into a separate JSP or custom tag.
  • 在现实生活中,我可能会将整个组件放入单独的JSP或自定义标签中。

#1


0  

Obviously you can only pass a single value in any given form field.

显然,您只能在任何给定的表单字段中传递单个值。

You have a number of options, from the simple–use a string and split it to get both values, through the middle–pass an ID and retrieve the appropriate values in Java, to the complex–custom type conversion.

您有许多选项,从简单使用字符串并将其拆分为两个值,通过中间传递ID并在Java中检索适当的值,到复杂自定义类型转换。

Which makes the most sense depends a lot on where your data is, how your data is, and so on.

哪种方式最有意义取决于数据的位置,数据的方式等等。


Unrelated, but I find that JSP very difficult to read. I would refactor a significant amount. The following is untested, but much closer to what I would expect after a code review.

不相关,但我发现JSP很难阅读。我会重构很多。以下是未经测试的,但更接近于我在代码审查后的预期。

<s:if test="reportFilter.showGroups">
  <s:iterator value="reportFilter.huntGroups" status="status">
    <tr>
      <td>
        <s:checkbox id="specHg_checkbox_%{#status.index}" theme="simple"
                    name="reportFilter.selectedHuntGroups" 
                    fieldValue="phoneNumber" 
                    value="%{reportFilter.hasNumber(phoneNumber)}" />
      </td>

      <td>
        <s:set var="num" value="${phoneNumber.length() > 0 ? phoneNumber : userGroupId" />
        <s:property value="#num"/>
      </td>

      <td class="hntName"><s:property value="name"/></td>
    </tr>
  </s:iterator>
</s:if>

A few things of note:

一些注意事项:

  • Logic to decide if we'll show groups encapsulated in Java. Makes testing easier, too.
  • 决定我们是否会显示用Java封装的组的逻辑。使测试更容易。

  • Reasonable, shorter name for the iteration object: it's a group (or huntGroup).
  • 迭代对象的合理,较短的名称:它是一个组(或huntGroup)。

  • Extract the (phone number || group ID) value, eliminating a bunch of JSP tags.
  • 提取(电话号码||组ID)值,消除一堆JSP标记。

  • (Ideally that logic would be hidden in Java, either in the VO, or a decorator.)
  • (理想情况下,逻辑将隐藏在Java中,无论是在VO中还是装饰器中。)

  • The iterator tag pushes the iteration object onto the value stack, eliminating the need for directly referencing it by name. This one is a bit more iffy; it depends on your needs.
  • 迭代器标记将迭代对象推送到值堆栈,无需按名称直接引用它。这个有点不确定;这取决于你的需求。

  • In real life I'd probably put the entire group thing into a separate JSP or custom tag.
  • 在现实生活中,我可能会将整个组件放入单独的JSP或自定义标签中。