spring mvc 和ajax异步交互实例

1.jsp页面,记得引入json包

  1. <%@ page language="java" contentType="text/html; charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <script type="text/javascript" src="js/jquery-2.1.3.js"></script>  
  7. <script type="text/javascript" src="js/jquery-2.1.3.min.js"></script>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=utf8">  
  9. <title>Insert title here</title>  
  10. <script type="text/javascript">  
  11.     function ajaxTest(){
  12. var array = $('#dg').datagrid("getSelections");
    if(array.length>0){
    //定义数组,通过下边的用来存储选中记录的Id
    var ids = new Array();
    for (i = 0; i < array.length; i++) {
    ids[i] = array[i].supId;
    //alert(ids[i]);
    }  
  13.         $.ajax({ 
  14. //向后台传送删除的那条数据的id 
  15.         data:{supId:ids}  
  16.         type:"GET",  
  17.         dataType: 'json',  
  18.         url:"user/login.action",  
  19.         error:function(data){  
  20.             alert(data);  
  21.         },  
  22.         success:function(data){  
  23.             alert(data); 
  24.  页面回显
  25.             $("#result").html(data) ;  
  26.         }  
  27.         });  
  28.     }  
  29. </script>  
  30. </head>  
  31. <body>  
  32.     <input type="text" name="name" id="name"/>  
  33.     <input type="submit" value="" οnclick="ajaxTest();"/>  
  34.     <div id="result"></div>  
  35. </body>  
  36. </html> 
  37. 2.后台页面
  38. package com.gxa.bj.action;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;

    import com.gxa.bj.model.Account;
    import com.gxa.bj.model.Page;
    import com.gxa.bj.model.SupplierInfo;
    import com.gxa.bj.service.SupplierInfoService;
    import com.gxa.bj.util.JSONHelper;
    @Controller
    public class SupplierInfoAction {
    private SupplierInfoService supplierInfoService;


    public SupplierInfoService getSupplierInfoService() {
    return supplierInfoService;
    }


    public void setSupplierInfoService(SupplierInfoService supplierInfoService) {
    this.supplierInfoService = supplierInfoService;
@RequestMapping(value="/delete.action")
public  void delete(String supId,HttpServletRequest request,HttpServletResponse resp){

resp.setCharacterEncoding("utf-8");
PrintWriter pw=null;
try {
pw = resp.getWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(supplierInfoService.removeItem(supId)>0){
pw.write("删除成功");
}else{
pw.write("失败失败");
}
}
  1. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值