为什么 struts 验证对我不起作用?
我正在尝试使用 Struts 验证来检查用户输入的各个字段。如果有人能够帮助我看到我所缺少的东西,我将非常感激。这就是我所拥有的:
我将 validation.xml
和 TestAction-validation.xml
放入 WEB-INF/classes/
这是 validation .xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator Config 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
<validators>
<validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
<validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
. . .
</validators>
这是TestAction-validation.xml
:
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="testInt">
<field-validator type="int">
<param name="min">0</param>
<param name="max">9</param>
<message>Number not in range</message>
</field-validator>
</field>
<field name="testString">
<field-validator type="stringlength">
<param name="minLength">4</param>
<message>String not long enough.</message>
</field-validator>
</field>
</validators>
我的struts.xml
扩展了struts-default
,我有一个非常简单的操作类TestAction
,它扩展了ActionSupport
并具有字段testInt
和testString
。
根据我的阅读,这应该足以让 Struts 检查输入的值,但它没有发生。我缺少什么?
I'm trying to use Struts validation to check various fields entered by users. If anyone is able to help me see what I lack, I would be extremely grateful. Here's what I have:
I put validation.xml
and TestAction-validation.xml
in WEB-INF/classes/
Here is validation.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator Config 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
<validators>
<validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
<validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
. . .
</validators>
Here is TestAction-validation.xml
:
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="testInt">
<field-validator type="int">
<param name="min">0</param>
<param name="max">9</param>
<message>Number not in range</message>
</field-validator>
</field>
<field name="testString">
<field-validator type="stringlength">
<param name="minLength">4</param>
<message>String not long enough.</message>
</field-validator>
</field>
</validators>
My struts.xml
extends struts-default
, and I have a extremely simple action class TestAction
which extends ActionSupport
and has fields testInt
and testString
.
From what I've read, this should be sufficient for Struts to check the values entered, but it isn't happening. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您有两种选择,基于每个模型或每个操作进行验证。要在 Action 级别进行验证,您只需创建一个名为 {your action}-validation.xml 的文件,并将其放置在与 Action 类相同的包中。要在模型级别进行验证,您将创建一个类似的文件,该文件采用模型对象的名称,然后指示您的操作验证文件根据模型验证文件中的规则进行验证。 (参考)
将validation.xml放在java源文件(默认包)的根目录中,并将TestAction-validation.xml放在TestAction.java文件所在的同一目录中。大多数 IDE 会自动将所有资源复制到将生成类文件的相应目录中。
更新:
http://struts.apache.org/2.x/docs/validation .html
如何找到操作的验证器
You have two choices, validate on a per-model basis or per-action. To validate at the Action level, you would simply create a file that takes the name {your action}-validation.xml and place it in the same package as the Action class. To validate at the model level, you would create a similar file that takes the name of the model object then direct your Action validation file to validate per the rules in the model's validation file. (Reference)
Put validation.xml in root of your java source files (default package) and put TestAction-validation.xml in same directory where your TestAction.java file is located. Most IDE's will automatically copy all resources to respective directory where your class file will be generated.
Update:
http://struts.apache.org/2.x/docs/validation.html
How Validators of an Action are Found
也许是因为 dtd 定义
http://www.opensymphony.com/xwork/ xwork-validator-1.0.2.dtd
不再存在
在 struts2 启动时,我收到此异常,并且我的验证拦截器不再工作
Maybe because the dtd definition
http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd
is no longer there
On struts2 startup I'm getting this exception and my validation interceptors are not working anymore
更换
"
"
使用新的新 DTD
这就是解决方案,它会起作用。
Replace the
"
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
"with new new DTD
This is the solution , it will work.
我脑子里可能有几件事。
1)您是否使用默认拦截器堆栈 - 该堆栈具有验证工作所需的验证拦截器,否则您必须在堆栈中手动指定验证拦截器。
2)TestAction-validation.xml应该位于WEB-INF/classes/[package]下,所以如果操作是com.foo.TestAction,那么TestAction-validation.xml应该位于WEB-INF/classes/com/foo/TestAction-下validation.xml
3) 尝试在验证器 xml 文件的名称中使用您在 TestAction 类中提交的方法的名称。您可以有 TestAction-[method_to_be_validated]-validation.xml
希望有帮助!
There could be a couple of things of the top of my head.
1) Are you using the default interceptor stack - this stack has a validation interceptor which is required for validation to work, otherwise you have to specify the validation interceptor manually in your stack.
2) TestAction-validation.xml should be under WEB-INF/classes/[package] so if the action is com.foo.TestAction then TestAction-validation.xml should be under WEB-INF/classes/com/foo/TestAction-validation.xml
3) Try to use the name of the method to which you are submitting within the TestAction class in the name of the validator xml file. You can have TestAction-[method_to_be_validated]-validation.xml
Hope that helps!