Struts2 OGNL运行时表达式

发布于 2025-01-04 11:51:52 字数 1696 浏览 1 评论 0原文

我正在验证对象列表。为了向用户显示他们输入的错误内容,我需要在转换错误时重新填充字段。我读到,为了重新填充页面上的字段值,我需要执行以下操作:

<s:textfield name="user.name" value="%{user.name}"/>

为了重新填充数组,我必须通过索引引用它们。正如这里提到的: 更新 struts2 中文本字段中的值列表

I有一个我需要验证的用户列表。 由于特定原因,我在迭代器上有自己的索引计数器。

<s:set name="counter" value="0"/>
<s:iterator value="users" var="user">
   <s:textfield name="users[%{#counter}].birthdate" value="%{users[#counter].birthdate}"/>
   <s:set name="counter" value ="%{#counter + 1}"/>
</s:iterator>

我正在对对象用户使用访问者验证器。

<field name="birthdate">
    <field-validator type="conversion">
        <param name="repopulateField">true</param>
            <message>${getText("E011", {"birthdate"})}</message>
    </field-validator>
</field>

正如我所说,出于特定原因,我没有使用迭代器索引计数器。

问题是这个 value="%{users[#counter].birthdate}" 部分不起作用。如果我将 counter 更改为 0 ,它会重新填充 users[0].birthdate 的值。似乎没有获取 OGNL 表达式中计数器的值。

我尝试过:

value="%{users[counter].birthdate}"
value="%{users[%{#counter}].birthdate}"
...
and so on.

有人可以帮助我完成这项工作吗?

更新

最后我应该提到我的特殊情况:

<s:iterator value="users" var="user" status="status">
  <s:if test="#status.index != removeIndex">

我发现我不需要为字段名称指定值来重新填充值。 但我需要使用它,因为这句话:

<s:if test="#status.index != removeIndex">

这将需要我大量的英语努力来解释为什么你们误解了我。我们可以删除这个帖子吗?谢谢

I am validating list of objects. In order to show users what they typed wrong, I need to repopulate fields on conversion error. I read that in order to repopulate fields value on page I need to do something like this:

<s:textfield name="user.name" value="%{user.name}"/>

For repopulating array, I must refer to them through indices. As mentioned here:
update a list of value in textfield in struts2

I have a list of Users, that I need to validate.
I have my own index counter on iterator for specific reason.

<s:set name="counter" value="0"/>
<s:iterator value="users" var="user">
   <s:textfield name="users[%{#counter}].birthdate" value="%{users[#counter].birthdate}"/>
   <s:set name="counter" value ="%{#counter + 1}"/>
</s:iterator>

I am using visitor validator for object User.

<field name="birthdate">
    <field-validator type="conversion">
        <param name="repopulateField">true</param>
            <message>${getText("E011", {"birthdate"})}</message>
    </field-validator>
</field>

As I said, I am not using iterator index counter for specific reason.

The problem is this value="%{users[#counter].birthdate}" part is not working. If I change counter to 0 it repopulates the value of users[0].birthdate right. Seems like it is not getting value of counter in OGNL expression.

I tried with:

value="%{users[counter].birthdate}"
value="%{users[%{#counter}].birthdate}"
...
and so on.

Can somebody help me to make this work?

UPDATE

It ended up that I should have mentioned my special case:

<s:iterator value="users" var="user" status="status">
  <s:if test="#status.index != removeIndex">

I found out that I don't need to specify value for field name for it to repopulate values.
But I needed to use that because of this line:

<s:if test="#status.index != removeIndex">

It will take lots of my English effort to explain why you guys misunderstood me. Can we delete this post?. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清旖 2025-01-11 11:51:52

如果你问如何做某事,这会更好……而不是说你有这样做的理由,当然这种方式行不通。这让我很头疼...

我不明白这一点...

<s:set name="counter" value="0"/>
<s:iterator value="users" var="user">
   <s:textfield name="users[%{#counter}].birthdate" value="{users[counter].birthdate}"/>
   <s:set name="counter" value ="%{#counter + 1}"/>
</s:iterator>

我会重写(不测试)我认为你的意思...

<s:iterator value="users">
   <s:textfield name="users.birthdate" value="birthdate"/>
</s:iterator>

上面要求满足以下条件...a)“用户" 是一个“User”类型的列表,b) struts2 转换框架已被告知 users 是一个“User”类型的列表,这就像添加注释一样简单:http://struts.apache.org/2.3.1.2/docs/keyproperty-annotation .html

满足这些条件...一次发送尽可能多的内容,而无需使用数组表示法,并且您可以重新填充字段。

现在,为什么您希望表单值在提交失败后重新填充?这就是你如何使用验证?如果由于某种原因您没有在 INPUT 或 ERROR 值上创建一些操作映射以直接返回输入页面(当验证框架存在时,这有点浪费精力)。

It's better if you ask how to do something... rather than say you have a reason for doing it this way and of course this way isn't working. It gives me a headache...

I don't understand this...

<s:set name="counter" value="0"/>
<s:iterator value="users" var="user">
   <s:textfield name="users[%{#counter}].birthdate" value="{users[counter].birthdate}"/>
   <s:set name="counter" value ="%{#counter + 1}"/>
</s:iterator>

I'll rewrite (without testing) what I think you mean...

<s:iterator value="users">
   <s:textfield name="users.birthdate" value="birthdate"/>
</s:iterator>

The above requires that the following conditions be met... a) That "users" is a list of type "User" and b) that the struts2 conversion framework has been told that users is a list of type "User" which is as simple as adding an annotation like so: http://struts.apache.org/2.3.1.2/docs/keyproperty-annotation.html

With those conditions met... Send as many at one time without the mess of needing to use array notation and you can repopulate the fields.

Now, why would you expect the form values to repopulate after it failed to submit? That is how are you using validation? If for some reason you aren't you have created some action mapping on a value of INPUT or ERROR to direct back to the input page (kind of a waste of effort when the validation framework exists).

梦幻的味道 2025-01-11 11:51:52

请尝试使用下面的代码,它可能会起作用

<s:set name="counter" value="0"/>
<s:iterator value="users" var="user">
  <s:textfield name="users[#counter].birthdate" value="%{users[#counter].birthdate}"/>
  <s:set name="counter" value ="%{#counter + 1}"/>
</s:iterator>

Please try with the below code, it may work

<s:set name="counter" value="0"/>
<s:iterator value="users" var="user">
  <s:textfield name="users[#counter].birthdate" value="%{users[#counter].birthdate}"/>
  <s:set name="counter" value ="%{#counter + 1}"/>
</s:iterator>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文