2011-09-14 61 views
0

我使用spring mvc 3.0來構建一個web應用程序。spring mvc如何用javascript或ajax調用窗體命令對象調用

用戶可以通過編寫自己的ID或發送空白表單來獲取客戶,並且我可以通過使用按鈕來關閉用戶可以在客戶之間穿行的所有客戶。問題是如何通過Ajax或JavaScript做到這一點,而無需回傳。

我的客戶對象添加到的ModelAttribute,在我的JSP文件中使用:

<form:form modelAttribute="Customer" method="POST"> 
      <form:input path="name"/> 

@RequestMapping(value = "/customer", method = RequestMethod.GET) 
    public String handleCustomer(Model md,HttpSession session) { 

     Customer customer= (Customer) session.getAttribute("customer"); 

     if(customer== null) 
     { 
      customer= new Customer(); 
     } 
     md.addAttribute("Customer ",customer); 
     return "customer"; 
    } 

這裏是如何改變這種模型屬性,而不回發的問題。現在根據這個get方法,當我設置Customer對象時,所有字段都被設置,因爲我使用「path」來綁定字段。我試圖通過ajax回調進行更改,但它不起作用。我不想讓所有的領域和jQuery一個接一個地分配。

這裏是我的JS功能:

$("#Customer").submit(function() { 
//    var customer= $(this).serializeObject(); 
//    $.postJSON("Customer", customer, function(data) { 
//     
//    }); 

       $.getJSON("customer/query.htm",{ id: $('#id').val() }, function(result) {       

        $('#testdiv').val(result); 

      }); 
       return false;    
      }); 

我曾經嘗試都的getJSON和postJSON功能。你可以鏈接某種書,教程或文檔,它會有所幫助。

回答

0

這是我的問題的答案我找不到一種方法來刷新我的表單與JSON響應。

但用ajax webrequest。

這是ajax Utils類。

import org.springframework.web.context.request.WebRequest; 

public class AjaxUtils { 

    public static boolean isAjaxRequest(WebRequest webRequest) { 
     String requestedWith = webRequest.getHeader("X-Requested-With"); 
     return requestedWith != null ? "XMLHttpRequest".equals(requestedWith) : false; 
    } 

    public static boolean isAjaxUploadRequest(WebRequest webRequest) { 
     return webRequest.getParameter("ajaxUpload") != null; 
    } 

    private AjaxUtils() {} 
} 

控制器中,我們添加

@RequestMapping(value = "/customer.htm", method = RequestMethod.GET) 
    public String handleCustomer(Model md, HttpSession session,WebRequest webRequest) { 

      Customer customer= ( Customer) session.getAttribute("customer"); 

     if (customer== null) { 
      customer= new Customer(); 
     } 
     md.addAttribute("Customer", customer); 
     md.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(webRequest)); 
     return "customer"; 
    } 

    @RequestMapping(value = "/customer.htm", method = RequestMethod.POST) 
     public String processSubmit( Customer customer, BindingResult result, WebRequest webRequest, HttpSession session, Model model) { 
      customer= dataService.getCustomer(customer.getId()); 

      session.setAttribute("customer", customer); 
      if (AjaxUtils.isAjaxRequest(webRequest)) { 
       // prepare model for rendering success message in this request 
       model.addAttribute("ajaxRequest", true); 
          model.addAttribute("Customer",customer); 
       return null; 
      } 


      return "customer"; 
     } 

功能,所以在JSP或視圖方面,我們發佈頁面:

$("#Customer").submit(function() { 
        $.post($(this).attr("action"), $(this).serialize(), function(html) { 
         $("#customerdiv").replaceWith(html); 

        }); 
        return false; 
       }); 

要使用AJAX utils的類的幫助下,我們設置的總結請求完整回發的類型或我們的後處理程序函數中的ajaxrequest,我們進行了更改,此更改直接影響頁面和表單。無需處理響應或使用jquery逐個設置所有字段希望這有助於某人。

2

使用諸如.getJSON之類的標準jQuery AJAX調用來提交您的查詢。

在Spring處理程序:

  1. 添加@ModelAttribute的處理方法簽名,以便Spring將名稱 - 值對從AJAX調用表單支持命令對象映射。
  2. 將處理程序方法的返回類型更改爲@ResponseBody Map<String, ? extends Object>,然後將對象添加到地圖而不是Model
  3. Jackson JAR添加到您的應用程序,以便Spring將響應對象自動奇蹟序列化爲JSON。

jQuery調用提交AJAX請求,Spring處理它,自動將名稱 - 值對綁定到命令對象,然後將JSON返回給jQuery方法。

@RequestMapping(value = "/customer", method = RequestMethod.GET) 
public @ResponseBody List<Customer> handleCustomer(
     @ModelAttribute("Customer") Customer form, 
     HttpSession session) { 
    List<Customer> customers = new ArrayList<Customer>(); 
    if(form.getId() > 0) { 
     // call your data access service to get that one customer 
     Customer c = myCustomerService.getCustomer(form.getId()); 
     customers.add(c); 
    } else { 
     // call your data access service to return all customers 
     customers = myCustomerService.getAllCustomers(); 
    } 
    return customers; 
} 
+0

首先感謝您的回答。糾正我,如果我錯了,但json將返回一個序列化的對象,我必須逐個設置所有字段。所以我的表單命令對象不是最新的,並且在提交時會出現問題。總結我想要什麼,我需要一種方式,如更新面板在asp.net它將部分更新我的網頁形式在我的情況。 – maniacneron

+0

Spring將自動將請求中的名稱 - 值對映射到通過'@ RequestBody'配置的Command對象。 JSON響應對此沒有影響。 – atrain

+0

如果我理解正確哇:)我是新的春天再次感謝您的答案,如果你給我一個例子它會非常appriciated.Thank你再次 – maniacneron