Ajax validation in the SPRINGMVC framework

Source: Internet
Author: User

Of course, you need to configure the environment before you use SPRINGMVC, which is not the case, directly on the code.

Before using SPRINGMVC, I used AJAX validation to use a jar package that parses the JSON: I get the data through Ajax to the background servlet, and then the Jsonobject object to pass the data as the foreground. Like this:

New Jsonobject (); Request.setcharacterencoding ("Utf-8"); Response.setcharacterencoding ("Utf-8" ) ); Response.setcontenttype ("Text/html;charset=utf-8"= response.getwriter (); J.put ("msg", " The user name has been registered "); Out.write (j.tostring ());

It is very convenient to use. But after learning the framework, especially after the use of SPRINGMVC, it is more convenient to use. The following is the use of the process, in fact, and the previous use is the same.

This is the entity class: The Set/get code is not posted.

1  Public class User {2     Private int uid; 3     Private String uname; 4     Private String password;

JSP code: Note Importing jquery packages, where we use jquery for Ajax validation

<%@ page language= "Java"Import= "java.util.*" pageencoding= "UTF-8"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >$ (function () {$ ("#password"). blur (function () {$.ajax ({URL:"User/ajax", data:{uname:$ ("#uname"). Val (), password:$ ("#password"). Val ()}, type:"POST", Success:function (data) {alert (data);            }                });        });    }); </script> User name<input id= "uname" type= "text" name= "uname" >Password<input id= "password" type= "password" name= "password" > </body>

Background code: Here is the Springmvc annotation method, you need to specify the Ajax authentication method above the @responsebody.return content is the data you want to work on the foreground page.

 PackageCom.mi.controller;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.ResponseBody;ImportOrg.springframework.web.servlet.mvc.multiaction.MultiActionController;ImportCom.mi.entity.User, @Controller @requestmapping ("User") Public classMycontrollerextendsmultiactioncontroller{@RequestMapping (value= "Ajax", produces={"TEXT/HTML;CHARSET=UTF-8;"}) @ResponseBody PublicObject Ajax (user user) {System.out.println (User.getuname ());        System.out.println (User.getpassword ());        SYSTEM.OUT.PRINTLN (user); returnUser.getpassword (); }}

This is probably the first time to learn a little bit, to make a small summary, and then to update the new knowledge.

Ajax validation in the SPRINGMVC framework

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.