Struts front-end Verification

Source: Internet
Author: User

For more information about the use of <HTML: javascript> In struts1.x, see the demo.

The first is the Web. xml file.

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <web-app xmlns = "http://java.sun.com/xml/ns/javaee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" version = "2.5" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <br/> <servlet-Name> action </servlet-Name> <br/> <servlet-class> org. apache. struts. action. actionservlet </servlet-class> <br/> <init-param> <br/> <param-Name> config </param-Name> <br/> <param-Value >/WEB-INF/struts-config.xml </param-value> <br/> </init-param> <br/> <param-Name> debug </param-Name> <br/> <param-value> 3 </param-value> <br/> </init-param> <br/> <init-Param> <br/> <param-Name> detail </param-Name> <br/> <param-value> 3 </param-value> <br/> </init -param> <br/> <load-on-startup> 0 </load-on-startup> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> action </servlet-Name> <br/> <URL-pattern> *. DO </url-pattern> <br/> </servlet-mapping> </P> <p> <welcome-file-List> <br/> <welcome-File> enter. JSP </welcome-File> <br/> </welcome-file-List> <br/> </Web-app>

Then, the <HTML: javascript> tag is used to verify the form. Enter. jsp

<% @ Page Language = "Java" pageencoding = "UTF-8" %> <br/> <% @ taglib uri = "http://struts.apache.org/tags-html" prefix = "html" %> </P> <p> <! -- Focus is generally called focus or positioning. You need to give it an action name. This in onsubmit specifies that the current form is verified --> <br/> <HTML: Form Action = "/Enter" Focus = "enter" onsubmit = "Return validateenterform (this) "> <br/> uname: <HTML: Text property =" uname "/> <br/> Email: <HTML: text property = "email"/> <br/> <HTML: Submit/> <HTML: cancel/> <br/> </ptml: form> </P> <p> <! -- Generate dynamic JavaScript scripts. Specify a specific formbean name in formname --> <br/> <HTML: javascript formname = "enterform"/> </P> <p> <! -- <Br/> ======================================== ========================================================== ====================================< br/> [JavaScript] <br/> struts front-end verification, usage: <br/> Step 1: locate action through focus <br/> Step 2: generate a dynamic Javascript script <br/> Step 3: When the form is submitted, use onsubmit = "" to specify the called function, that is, the function generated in step 2 <br/> ==================================== ========================================================== ==========================================< br/> if we do not know the third step should if you call a function, what should we do? <br/> WE CAN When you finish step 2, release the project, call the enter. jsp page, right-click the page to view the source file <br/>, and you will find that enter. jsp is opened slowly. A Javascript script is automatically generated in the source file, that is, the result after the second step is executed <br/> and a verification function named validateenterform appears in the Javascript script. This is the function written in onsubmit. <br/> ==================================== ========================================================== ===============================< br/> If the actionform used belongs to validatorform subclass, at this time, the verification is completely verified by the server <br/> if you want to verify on the front-end, a script is automatically generated on the front-end, the form that needs to be used with the front-end page is a dynamic verification <br/> so the actionform used at this time must be a dynavalidatorform class, and it is not displayed in the form of JavaBean <br/> that is, dynavalidatorform does not exist at this time. It is generated dynamically when the program is running, and is described in the struts-config.xml <br/> ======================== ========================================================== =======================================================< Br/> [Supplement] <br/> individual tests: if not. verify the configuration of the form field on the page in XML <br/> tested by a person: the following error is reported when the application is running <br/> tested by a person: javax. servlet. JSP. jspexception: no form found under 'enterform' in locale 'zh _ cn' <br/> I understand that struts can dynamically create JavaScript scripts corresponding to front-end forms, this script is used to verify the validity of front-end form fields <br/> I understand: And we only need to verify the validity of fields in the validation. some description data is provided for this form field in XML, and then it can be verified directly <br/> I understand: But please note that if you want Automatically generate scripts, the form corresponding to the front-end page must be a dynavalidatorform <br/> ==================== ========================================================== =====================================================< br/> 【 welcome to shoot bricks] <br/> if we have a front-end verification on struts1.x, there is something wrong with it. You are welcome to give me some comments. Thank you !! <Br/> ========================================== ========================================================== ======================================< br/> -->

Then the entersuccess. jsp page after the verification is successful

<% @ Page pageencoding = "UTF-8" %> <br/> <% @ page import = "org. apache. struts. validator. dynavalidatorform "%> <br/> <% <br/> dynavalidatorform VFF = (dynavalidatorform) session. getattribute ("enterform"); <br/>%> <br/> <font color = "blue"> <% = VFF. get ("uname") %> </font>, logon successful

Then there is the struts1.x core configuration file struts-config.xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype Struts-config public "-// Apache Software Foundation // DTD struts configuration 1.2 // en" <br/> "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <br/> <Struts-config> <br/> <form-beans> <br/> <form-bean name = "enterform" type = "org. apache. struts. validator. dynavalidatorform "> <br/> <! -- Specify two attributes of the dynamic verifiable form --> <br/> <form-property name = "email" type = "Java. lang. string "/> <br/> <form-property name =" uname "type =" Java. lang. string "/> <br/> <! -- This form has an advantage: it does not generate an object immediately injected into the memory when the program is running --> <br/> <! -- It is generated dynamically when we use it. Therefore, its overhead is smaller. However, the running speed is slower --> <br/> </form-bean> <br/> </form-beans> </P> <p> <action-mappings> <br/> <action Path = "/Enter" <br/> attribute = "enterform" <br/> name = "enterform" <br/> input = "/Enter. JSP "<br/> type =" com. jadyer. action. enteraction "> <br/> <forward name =" entersuccess "Path ="/entersuccess. JSP "Redirect =" true "/> <br/> </Action-mappings> </P> <p> <message-resources parameter = "com. jadyer. util. applica Tionresources "/> </P> <p> <! -- In this case, you need to put the validator-rules.xml and validation. xml file under the webroot // WEB-INF // directory --> <br/> <! -- Validator. XML is compiled by ourselves, and the validator-rules.xml is provided by struts1.x --> <br/> <! -- In struts1.2, validator-rules.xml is located in the // webroot // WEB-INF root directory --> <br/> <! -- In struts1.3 the validator-rules.xml is located in the org. Apache. Struts. validator package in the struts-core-1.3.8.jar --> <br/> <! -- Then save the validator-rules.xml as validation. XML, and copy the 12th lines of the file <plug-in> to the struts-config.xml --> <br/> <plug-in classname = "org. apache. struts. validator. validatorplugin "> <br/> <set-Property =" pathnames "value ="/WEB-INF/validator-rules.xml,/WEB-INF/validation. XML "/> <br/> </plug-in> <br/> </Struts-config>

Next is our custom validation. xml file.

<! Doctype form-validation Public <br/> "-// Apache Software Foundation // DTD commons validator rules configuration 1.1.3 // en" <br/> "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd"> <br/> <form-validation> <br/> <FormSet> <br/> <! -- Note: You must provide enterform verification. Otherwise, enter. <HTML: javascript> verification specified in JSP, still invalid --> <br/> <form name = "enterform"> <br/> <field property = "uname" depends = "required, mask "> <br/> <arg0 key =" input username "resource =" false "/> <br/> <var-Name> mask </var-Name> <br/> <var-value> [A-Za-Z] </var-value> <br/> </var> <br/> </field> <br/> <field property = "email" depends = "required, email "> <br/> <arg0 key =" lab. email "/> <br/> </field> <br/> </form> <br/> </FormSet> <br/> </form-validation>

Then the action processing class used

Package COM. jadyer. action; </P> <p> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; <br/> Import Org. apache. struts. action. action; <br/> Import Org. apache. struts. action. actionform; <br/> Import Org. apache. struts. action. actionforward; <br/> Import Org. apache. struts. action. actionmapping; <br/> Import Org. apache. struts. validator. dynavalidatorform; </P> <p> public class enteraction extends action {<br/> Public actionforward execute (actionmapping mapping, actionform form, <br/> httpservletrequest request, httpservletresponse response) {<br/> dynavalidatorform enterform = (dynavalidatorform) form; </P> <p> // obtain and output the value in dynavalidatorform <br/> // here, the uname corresponds to enter. <HTML: Text property = "uname"/> <br/> system. out. println ("uname:" + enterform. get ("uname"); <br/> system. out. println ("Email:" + enterform. get ("email"); </P> <p> Return Mapping. findforward ("entersuccess"); <br/>}< br/>}

Finally, the international resource file applicationresources. properties is used.

Lab. name = Name <br/> lab. email = email </P> <p> errors. required = {0} is required <br/> errors. invalid = {0} is invalid <br/> errors. email = {0} is an invalid e-mail address </P> <p> # the following content of the applicationresources_zh_cn.properties file </P> <p> lab. name =/u59d3/u540d <br/> lab. email =/u7535/u5b50/u90ae/u4ef6 </P> <p> errors. required = {0}/u4e0d/u80fd/u4e3a/u7a7a <br/> errors. invalid = {0}/u8f93/u5165/u7684/u5185/u5bb9/u4e0d/u5408/u7406 <br/> errors. email = {0}/u4e0d/u662f/u5408/u6cd5/u7684email/u5730/u5740

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.