Spring MVC Ajax Request

Source: Internet
Author: User

Add in Jar Package

Jackson-annotations-2.5.0.jar

Jackson-core-2.5.0.jar

Jackson-databind-2.5.0.jar

Springmvx.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd HT        Tp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.1.xsd ">    <!--Spring MVC Annotation Driver -    <Mvc:annotation-driven/>    <!--Scanning Device -    <Context:component-scanBase-package= "com" />    <!--Configuring the View resolver -    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        <!--Prefixes and suffixes -        < Propertyname= "prefix"value="/"></ Property>        < Propertyname= "suffix"value= ". jsp"></ Property>    </Bean>    <!--read/write string from request and response -    <BeanID= "Stringhttpmessage"class= "Org.springframework.http.converter.StringHttpMessageConverter">        < Propertyname= "Supportedmediatypes">            <List>                <value>Text/plain;charset=utf-8</value>            </List>        </ Property>    </Bean>    <!--used to convert an object to JSON -    <BeanID= "Jsonconverter"class= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></Bean>    <Beanclass= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">        < Propertyname= "Messageconverters">            <List>                <refBean= "Stringhttpmessage" />                <refBean= "Jsonconverter" />            </List>        </ Property>    </Bean></Beans>

Handler

 PackageCom.stone.controller;ImportJava.io.PrintWriter;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.ResponseBody;ImportCom.stone.bean.UserInfo;/** */@Controller Public classUsercontroller {@RequestMapping ("/ajaxuser.do")     Public voidusernumber (String num, httpservletresponse response) {System.out.println (num); Try {            //ResponseResponse.setcontenttype ("text/html"); Response.setcharacterencoding ("Utf-8"); PrintWriter out=Response.getwriter (); if("Stone". Equals (num)) {Out.println ("Sorry, please re-enter the account!" "); } Else{out.println ("Congratulations, you can use the account!" ");            } out.flush ();        Out.close (); } Catch(Exception e) {e.printstacktrace (); }} @RequestMapping ("/ajaxuser2.do")     Public@ResponseBody UserInfo ajaxuser (Integer userId) {System.out.println ("-------Bianhao:" +userId); UserInfo User=NewUserInfo ();        User.setuserid (USERID); User.setusername ("Zhang San"); User.setusersex (Male); returnuser; }}

Js

functionBtn01click () {varnum = $ (' #userNumber '). Val (); if(num = = ") {alert (' Please enter your account! ‘); } Else{$.post (' Ajaxuser.do ', {num:num},function(data) {alert (data);    }); }}functionBtn02click () {varnum = $ (' #userId '). Val (); if(num = = ") {alert (' Please enter your account! ‘); } Else{$.post (' Ajaxuser2.do ', {userid:num},function(data) {alert (Data.userid+ '----' +data.username+ '----' +data.usersex); }, "JSON"); }}

Java Bean

 PackageCom.stone.bean;Importjava.util.Arrays;Importjava.util.Date;ImportOrg.springframework.format.annotation.DateTimeFormat; Public classUserInfo {PrivateInteger userId; PrivateString UserName; PrivateString Usersex; Privatestring[] USERXQ; @DateTimeFormat (Pattern= "Yyyy-mm-dd HH:mm:ss")    PrivateDate Usertime; @Override PublicString toString () {return"UserInfo [userid=" + UserId + ", username=" +UserName+ ", usersex=" + Usersex + ", userxq=" + arrays.tostring (USERXQ) + ", usertime=" + Usertime + "]"; }     PublicDate Getusertime () {returnUsertime; }     Public voidsetusertime (Date usertime) { This. Usertime =Usertime; }     Publicstring[] Getuserxq () {returnUSERXQ; }     Public voidsetuserxq (string[] userxq) { This. USERXQ =USERXQ; }     PublicInteger getUserId () {returnuserId; }     Public voidSetuserid (Integer userId) { This. UserId =userId; }     PublicString GetUserName () {returnUserName; }     Public voidsetusername (String userName) { This. UserName =UserName; }     PublicString Getusersex () {returnUsersex; }     Public voidsetusersex (String usersex) { This. Usersex =Usersex; }}

Spring MVC Ajax Request

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.