Solve the invalid validation. xml configuration in struts2.

Source: Internet
Author: User

Solve the Problem of invalid validation. xml configuration in struts2. I used XML verification, but I still found that it could not take effect. Later I found the format of the xml header file. I just modified it.

Successful XML

<!DOCTYPE validators PUBLIC        "-//Apache Struts//XWork Validator 1.0.2//EN"        ""><validators>  <field name="username">      <field-validator type="requiredstring">          <message>You must enter a value for bar.</message>      </field-validator>  </field></validators>

Mainly <! Doctype validators public
"-// Apache struts // xwork validator 1.0.2 // en"

The format of this sentence must be correct, otherwise it will not take effect.

The following describes how to use validation. XML for verification:

1. Write valideaction. Java, which is the class for processing business logic.

package com.test.action;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.test.model.Contact;import com.test.model.User;public class ValideAction extends ActionSupport implements ModelDriven<User>{/** *  */private static final long serialVersionUID = 1L;private User user;private String username;private Contact contact;public Contact getContact() {return contact;}public void setContact(Contact contact) { = contact;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}private String password;public String execute(){//System.out.println(user.getContact().getPhone());return SUCCESS;}@Overridepublic User getModel() {// TODO Auto-generated method stubif (null == user) {   return user = new User();  }  return user;}}

2. Compile a verification file, ValideAction-validation.xml

<!DOCTYPE validators PUBLIC        "-//Apache Struts//XWork Validator 1.0.2//EN"        ""><validators>  <field name="username">      <field-validator type="requiredstring">          <message>You must enter a value for bar.</message>      </field-validator>  </field></validators>

3. Corresponding JSP page. Note that I only use a username as an example for testing and judgment. Other fields such as password are not used for testing.

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <% @ taglib uri ="/Struts-tags "prefix =" S "%> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" ""> <HTML> 

4. configure action in struts. xml

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype struts public "-// Apache Software Foundation // DTD struts configuration 2.3 // en" ""> <struts> <! -- Use annotation --> <constant name = "struts. enable. dynamicmethodinvocation "value =" true "/> <constant name =" struts. devmode "value =" true "/> <package name =" default "namespace =" "extends =" struts-Default "> <! -- Correct and wrong jump, respectively. input is the page that will jump after verification fails --> <action name = "data" class = "com. test. action. valideaction "> <result name =" success ">/result. JSP </result> <result name = "input">/login. JSP </result> </Action> </package> <! -- Add packages here --> </struts>

5. The final display of the JSP page is the above file, mainly using <s: fielderror/> to display the error message

6. File directory structure

Output result

Solve the invalid validation. xml configuration in struts2.

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: 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.