Spring,Ajax and jQuery Tutorial Hello World Application

http://www.beingjavaguys.com/2013/07/sending-html-form-data-to-spring.html

In this particular blog we will discuss how to send html form data from Ajax to Spring Controller using jQuery. We will take a simple html form and will get the elements data into ajax function using jQuery $(#id).value() function. Then a call will be made to spring controller along with all form data as request parameters. Then the form data will be stored in java variables using @RequestMapping annotation. At the end a response will be send back to same ajax fucntion in json format.




Sending form data from AJAX to spring controller


We will select the form elements on the basis of their id's using jquery and their values will be stored in js variables. Then a request parameters data string will be made to send to spring controller. See the code below.
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
  2.  pageEncoding="ISO-8859-1"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  7. <title>Insert title here</title>  
  8. <script src="js/jquery-1.10.1.js" type="text/javascript"></script>  
  9.   
  10. </head>  
  11. <body>  
  12.  <center>  
  13.     
  14.    
  15.    
  16.  <b>Being Java Guys | Registration Form </b>   
  17.   
  18.     
  19.   
  20.   <script>  
  21.    function doAjaxPost() {  
  22.       
  23.       
  24.     var name = $('#name').val();  
  25.     var gender = $('#gender').val();  
  26.     var email = $('#email').val();  
  27.     var phone = $('#phone').val();  
  28.     var city = $('#city').val();  
  29.   
  30.     $.ajax({  
  31.      type : "Get",   
  32.      url : "hello.html",   
  33.      data : "name=" + name + "&gender=" + gender + "&email="  
  34.        + email + "&phone=" + phone + "&city=" + city,  
  35.      success : function(response) {  
  36.       alert(response);   
  37.      },  
  38.      error : function(e) {  
  39.       alert('Error: ' + e);   
  40.      }  
  41.     });  
  42.    }  
  43.   </script>  
  44.   <div id="form">  
  45.    <form method="get">  
  46.     <table>  
  47.      <tr>  
  48.       <td>Name :</td>  
  49.       <td><input type="text" id="name" /></td>  
  50.      </tr>  
  51.      <tr>  
  52.       <td>Gender :</td>  
  53.       <td><input type="radio" id="gender" value="male" /> Male   
  54.   
  55.        <input type="radio" id="gender" value="female" /> Female</td>  
  56.      </tr>  
  57.      <tr>  
  58.       <td>Email :</td>  
  59.       <td><input type="text" id="email" /></td>  
  60.      </tr>  
  61.      <tr>  
  62.       <td>Phone :</td>  
  63.       <td><input type="text" id="phone" /></td>  
  64.      </tr>  
  65.      <tr>  
  66.       <td>City :</td>  
  67.       <td><select id="city"><option value="noida">Noida</option>  
  68.         <option value="delhi">Delhi</option>  
  69.         <option value="gurgaon">Gurgaon</option>  
  70.         <option value="others">Others</option>  
  71.       </select></td>  
  72.      </tr>  
  73.      <tr>  
  74.       <td> </td>  
  75.       <td><input type="button" value="Save" onclick="doAjaxPost();" />  
  76.       </td>  
  77.      </tr>  
  78.     </table>  
  79.    </form>  
  80.   </div>  
  81.  </center>  
  82. </body>  
  83. </html>  


Getting ajax data in spring controller and sending response back to ajax


Now we will use @RequestMapping annotation along with required value attributes to get the data send from ajax as request parameters and store that directly in java variables. At the end we have used @ResponseBody annotation to send back the response to ajax function in json format. See the code below: 
  1. package com.beingjavaguys.controller;  
  2. /** 
  3.  * @author Nagesh Chauhan 
  4.  */  
  5. import org.springframework.stereotype.Controller;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RequestParam;  
  8. import org.springframework.web.bind.annotation.ResponseBody;  
  9.   
  10. @Controller  
  11. public class Home {  
  12.   
  13.  @RequestMapping("/hello")  
  14.  public @ResponseBody  
  15.  String hello(@RequestParam(value = "name") String name,  
  16.    @RequestParam(value = "gender") String gender,  
  17.    @RequestParam(value = "email") String email,  
  18.    @RequestParam(value = "phone") String phone,  
  19.    @RequestParam(value = "city") String city) {  
  20.   System.out.println(name);  
  21.   System.out.println(gender);  
  22.   System.out.println(email);  
  23.   System.out.println(phone);  
  24.   System.out.println(city);  
  25.   
  26.   String str = "{\"user\": { \"name\": \"" + name + "\",\"gender\": \""  
  27.     + gender + "\",\"email\": \"" + email + "\",\"phone\": \""  
  28.     + phone + "\",\"city\": \"" + city + "\"}}";  
  29.   return str;  
  30.   
  31.  }  
  32. }  
If everything goes right ajax function will call the spring mvc controller along with data to sent, we will see sended data values printed on console and a response string from controller to ajax as alert.

Simple Html Form

Response back from controller


In this particular blog we came across how to use a ajax function to send data to spring controller and getting back the response in json format. In upcoming blogs we will see more about spring mvc and other opensource technologies.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值