条件字段的Struts验证器对jsp上的另一个变量有条件 [英] Struts validator of field conditional to another variable on the jsp

查看:31
本文介绍了条件字段的Struts验证器对jsp上的另一个变量有条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用struts验证程序进行jsp验证,我想知道如何根据我的jsp另一个字段的值来定义字段验证.

I'm using the struts validator for a jsp validation and i want to know how can I define a field validation depending on the value of another field of my jsp.

我有一个像这样的元素列表:

I have a list of elements like this one :

<s:select name="varName" label="labelVariable" list="listeVariable" listValue="varValue" listKey="varKey"/>

还有一个这样的文本字段:

And a textfield like this one :

<s:textfield name="varNameText" label="labelText"/>

在我的验证器中,我想根据列表上选择的值来验证字段"varNameText"的大小.

In my validator I want to validate the size of the field 'varNameText' depending on the selected value on the list.

如果我的列表的值是1/2/3,我要验证:

If my list has the values 1/2/3, I want to validate :

  • 如果list = 1,则varNameText长度应为10
  • 如果列表!= 1,则varNameText长度应为12

如何将其保存到xml文件中.

How can I do this into my xml file.

感谢您的回答.

推荐答案

您可以使用

You can use fieldExpression validator

使用OGNL表达式验证字段.

Validates a field using an OGNL expression.

您可以在此处找到fieldExpression验证程序的示例.

You can find an example of fieldExpression validator here.

在对valueStack运行验证之前,您可以创建一个表达式,该表达式使用填充到操作bean的任意数量的字段.

The point that you can create an expression that using any number of fields populated to action bean before you run the validation against the valueStack.

例如

<field-validator type="fieldexpression">
  <param name="expression"><![CDATA[varName==1?(varNameText.length()==10):(varNameText.length()==12)]]></param>
  <message>varNameText length should be ${varName==1?10:12}</message>
</field-validator>

这篇关于条件字段的Struts验证器对jsp上的另一个变量有条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆