为什么 struts 验证对我不起作用?

发布于 2024-08-22 14:14:30 字数 1749 浏览 7 评论 0原文

我正在尝试使用 Struts 验证来检查用户输入的各个字段。如果有人能够帮助我看到我所缺少的东西,我将非常感激。这就是我所拥有的:

我将 validation.xmlTestAction-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并具有字段testInttestString

根据我的阅读,这应该足以让 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 技术交流群。

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

发布评论

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

评论(4

情感失落者 2024-08-29 14:14:30

您有两种选择,基于每个模型或每个操作进行验证。要在 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

把梦留给海 2024-08-29 14:14:30

也许是因为 dtd 定义

http://www.opensymphony.com/xwork/ xwork-validator-1.0.2.dtd

不再存在

在 struts2 启动时,我收到此异常,并且我的验证拦截器不再工作

Line: 1436 - sun/net/www/protocol/http/HttpURLConnection.java:1436:-1
at com.opensymphony.xwork2.util.DomHelper.parse(DomHelper.java:115)
at com.opensymphony.xwork2.validator.DefaultValidatorFileParser.parseValidatorDefinitions(DefaultValidatorFileParser.java:118)
at com.opensymphony.xwork2.validator.DefaultValidatorFactory.retrieveValidatorConfiguration(DefaultValidatorFactory.java:195)
at com.opensymphony.xwork2.validator.DefaultValidatorFactory.parseValidators(DefaultValidatorFactory.java:184)
at com.opensymphony.xwork2.validator.DefaultValidatorFactory.<init>(DefaultValidatorFactory.java:55)

Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://opensymphony.com/xwork/xwork-validator-config-1.0.dtd
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source)
at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)

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

Line: 1436 - sun/net/www/protocol/http/HttpURLConnection.java:1436:-1
at com.opensymphony.xwork2.util.DomHelper.parse(DomHelper.java:115)
at com.opensymphony.xwork2.validator.DefaultValidatorFileParser.parseValidatorDefinitions(DefaultValidatorFileParser.java:118)
at com.opensymphony.xwork2.validator.DefaultValidatorFactory.retrieveValidatorConfiguration(DefaultValidatorFactory.java:195)
at com.opensymphony.xwork2.validator.DefaultValidatorFactory.parseValidators(DefaultValidatorFactory.java:184)
at com.opensymphony.xwork2.validator.DefaultValidatorFactory.<init>(DefaultValidatorFactory.java:55)

Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://opensymphony.com/xwork/xwork-validator-config-1.0.dtd
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source)
at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
南薇 2024-08-29 14:14:30

更换
""
使用新的新 DTD

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.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

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">

This is the solution , it will work.

扛起拖把扫天下 2024-08-29 14:14:30

我脑子里可能有几件事。

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文