The reason why the specified validation file in Struts2 does not work is that struts2 does not work.

Source: Internet
Author: User

The reason why the specified validation file in Struts2 does not work is that struts2 does not work.

Reprinted please pay attention to the Source: http://blog.csdn.net/bettarwang/article/details/39801733

We know that if you want to specify a validation file for an Action, you need to set "Action name-validation. the xml "validation file is placed in the same path as the Action, and in struts. specify the value of <result name = "input> in xml, and add <s: fielderror/> to the file corresponding to the input. If the input does not comply with the verification rules, the system does not jump to the corresponding action. Instead, it still redirects back to the input file and displays the corresponding verification prompt.

However, after I specified the validation file, I found that it never worked. Then I finally found the problem. It turned out that I was greedy for the convenience of copying the dtd information of the configuration file from somewhere else, however, the copied dtd information is not the same as the dtd information used by Struts of the current version, resulting in ineffective verification.

The solution is very simple, that is, find your own xwork-core File in lib, for example, I am a xwork-core-2.3.16.3.jar, use the decompression tool to view the dtd file, there are generally multiple, I am viewing the xwork-validator-1.0.3.dtd, the content is as follows:

<?xml version="1.0" encoding="UTF-8"?><!--  XWork Validators DTD.  Used the following DOCTYPE.  <!DOCTYPE validators PUBLIC  "-//Apache Struts//XWork Validator 1.0.3//EN"  "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">--><!ELEMENT validators (field|validator)+><!ELEMENT field (field-validator+)><!ATTLIST fieldname CDATA #REQUIRED><!ELEMENT field-validator (param*, message)><!ATTLIST field-validatortype CDATA #REQUIRED    short-circuit (true|false) "false"><!ELEMENT validator (param*, message)><!ATTLIST validatortype CDATA #REQUIRED    short-circuit (true|false) "false"><!ELEMENT param (#PCDATA)><!ATTLIST param    name CDATA #REQUIRED><!ELEMENT message (#PCDATA|param)*><!ATTLIST message    key CDATA #IMPLIED>
 
Copy the following part to the verification file:
 
<pre name="code" class="html"><!DOCTYPE validators PUBLIC  "-//Apache Struts//XWork Validator 1.0.3//EN"  "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">-->

I tried later and found that the configuration information in the xwork-validator-1.0.2.dtd can also be used, this should be only a version of the problem, but it must be your own xwork-core support version.
 


What should I do if the struts2 validator does not work when verifying regular expressions?

Check whether the file name is correct.
For example:
You need to verify the dosearch method of Useraction. java
The file name to verify should be Useraction-dosearch-validation.xml
 
Problems with the use of the struts2 verification framework

1. The execution principle of the struts verification framework is that when an action is executed, the method in the action will be executed instead of the method in it. After the action is executed, the method in the action will be returned. The validator is divided into field verification and non-field verification. I suggest using field verification, which is more intuitive and understandable. The configuration of field verification is as follows:
Configure the validator for the field of action, such as the username and pwd fields in login. java, then configure the login-validation.xml to verify the two fields:
<Validators>
<Field name = "username"> // field name to be verified (change this field to pwd for password verification)
<Field-validator type = "requiredstring"> // do not leave it empty.
<Message> no null </message> // error message
</Field-validator>
</Field>
Ii. struts2 configuration file
<Struts>
<Package name = "default" extends = "struts-default">
<Action name = "LoginAction" class = "package name. LoginAction>
<Result name = "success">/OK. jsp </result>
<Result name = "error">/error. jsp </result>
</Action>
</Package>
</Struts>
For reference only


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.