Why is struts validation not working for me?
Asked Answered
D

4

8

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?

Dolhenty answered 19/2, 2010 at 19:26 Comment(1)
Can you post your action mapping/struts.xml? Turning on dev mode (struts.devMode) if it isn't, might give you more clues as to what's happening.Auricula
C
6

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

Carraway answered 25/2, 2010 at 1:57 Comment(1)
Thanks much. One more thing I was missing was including tags like <s:fielderror> in my JSP.Dolhenty
R
4

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)
Rosemaryrosemond answered 19/6, 2013 at 17:25 Comment(1)
At least one link provided by the accepted answer contain the new DOCTYPE you should be using for your validation metadata files: <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">Kid
E
4

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.

Erb answered 27/4, 2014 at 17:20 Comment(1)
OK, but I don't think I'm going to check. I had this problem over four years ago, and I don't use Struts so much anymore.Dolhenty
A
3

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!

Anabantid answered 22/2, 2010 at 2:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.