SPRINGMVC parameters Verify the use of Bindingresult result __SPRINGMVC

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_6829be5c0101alxh.html a very detailed explanation and worthy of further discussion

http://bbs.csdn.net/topics/360254599 Simple and clear



1. Import dependent Packages First Bean-validator.jar

2. Write some relevant validation information on the entity class: You can search for more verification methods, which is only part of the

can refer to: Click on open Link http://blog.csdn.net/caihaijiang/article/details/7463514

Private String username;
	private String password;
	Private String nickname;
	private String Email;
	
	Public user () {
	}

	@NotEmpty (message= "User name cannot be empty") public
	String GetUserName () {return
		username;
	}

	public void Setusername (String username) {
		this.username = username;
	}

	@Size (min=1,max=10,message= "Password length should be between 1 and 10") Public
	String GetPassword () {return
		password;
	}

	public void SetPassword (String password) {
		this.password = password;
	}

	Public String Getnickname () {return
		nickname;
	}

	public void Setnickname (String nickname) {
		this.nickname = nickname;
	}

	@Email (message= "Malformed mailbox") Public
	String Getemail () {return
		email;
	}

	public void Setemail (String email) {
		This.email = email;
	}
3. Background controller processing

@RequestMapping ("save") public
    String Save (@Valid User user,bindingresult result) {
        //
        if ( Result.haserrors ()) {
            list<objecterror> ls=result.getallerrors ();
            for (int i = 0; i < ls.size (); i++) {
                System.out.println ("Error:" +ls.get (i));
            }
        return "AddUser";
    }
An in-depth analysis of Bindingresult: If you use the interface to return information, you can directly get the error message in Bindingresult

if (Br.haserrors ()) {
			//If there is an error jump directly to the Add view
			fielderror fielderror= br.getfielderror ();
			System.out.println (Fielderror.getdefaultmessage ());
			
			list<fielderror> list = Br.getfielderrors ();
			for (Fielderror fielderror2:list) {
				System.out.println (fielderror2.getdefaultmessage ());
			}
			List<objecterror> ls=br.getallerrors ();
            for (int i = 0; i < ls.size (); i++) {
                System.out.println ("Error:" +ls.get (i). Getdefaultmessage ());
			return "User/add";
		}



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.