2013-03-06 45 views
0

我試圖發送一個json列表從控制器到javascript當訪問給定的URL。使用此代碼,當我轉到URL時,我收到了一個406錯誤。我的servlet上下文有<annotation-driven />,我有org.codehaus.jackson maven依賴關係。這是否有任何其他原因給我一個406,或者有更好的方式去發送一個列表到JavaScript?從控制器返回一個列表到javascript導致一個406

控制器功能

@RequestMapping(value = "/{room}/handhygiene.json", method = RequestMethod.GET) 
    public @ResponseBody ArrayList<Integer> getHandHygienePageAsync(
      @PathVariable(value = "room") String roomCode) { 
     ArrayList<Integer> json = new ArrayList<Integer>(); 
     Room room = getRoom(roomCode); 

     json.add(service.getHandHygieneEvents(room)); 
     if (room.isGroupBased()) { 
      json.add(service.getRoomEntryExits(room)); 
     } 
     if (room.isIdBased()) { 
      json.add(service.getPatientContacts(room)); 
     } 
     return json; 
    } 

的Javascript

$.getJSON(currentURL + ".json", 
      function(data){ 
       alert('I can\'t get here though'); 
       var overallRatio; 
       var handWash = data[0]; 

       $("#handwash").html(handWash); 
       if(groupBased == true) 
       { 
        var enex = data[1]; 
        $("#enex").html(enex); 
        overallRatio = "" + Math.round((handWash*100)/enex) + "%"; 
       } 
       else 
       { 
        if(idBased == true) 
        { 
         var contacts = data[2]; 
         $("#contacts").html(contacts); 
         overallRatio = "" + Math.round((handWash*100)/contacts) + "%";      
        } 
       } 

       $("#overall").html(overallRatio); 

      } 
    ); 

下面是請求

GET /groupbased/ICU6/handhygiene.json HTTP/1.1 
Host: localhost:8080 
Connection: keep-alive 
Accept: application/json, text/javascript, */*; q=0.01 
X-Requested-With: XMLHttpRequest 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22 
Referer: http://localhost:8080/groupbased/ICU6/handhygiene 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-US,en;q=0.8 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Cookie: JSESSIONID=063CDE4BF5FB7369B6C48FC2EADFD8E9 

將與什麼這個視圖解析器衝突的頭?

@Bean 
    public InternalResourceViewResolver internalResourceViewResolver() { 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setPrefix("/WEB-INF/views/"); 
     resolver.setSuffix(".jsp"); 
     return resolver; 
    } 
+0

對於錯誤代碼:http://www.checkupdown.com/status/E406.html – 2013-03-06 16:38:21

+0

什麼版本的Spring?您是否使用像jackson-mapper-asl這樣的映射器依賴項?您是否在使用內容協商視圖解析器? – 2013-03-06 16:39:30

回答

0

添加下面的配置到MVC-servet.xml

<bean name="customViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver"> 
    <property name="location" value="/WEB-INF/config/spring/customViews.xml" /> 
    <property name="order" value="0"/> 
</bean> 

路徑/WEB-INF/config/spring/customViews.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 
     "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 
<beans> 
<bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>  
</beans> 

下面創建customViews.xml控制器更改: -

@RequestMapping(value = "/{room}/handhygiene.json", method = RequestMethod.GET) 
public @ResponseBody String getHandHygienePageAsync(
     @PathVariable(value = "room") String roomCode,ModelMap model) { 
    ArrayList<Integer> json = new ArrayList<Integer>(); 
    Room room = getRoom(roomCode); 

    json.add(service.getHandHygieneEvents(room)); 
    if (room.isGroupBased()) { 
     json.add(service.getRoomEntryExits(room)); 
    } 
    if (room.isIdBased()) { 
     json.add(service.getPatientContacts(room)); 
    } 
    model.addAttribute("json",json) 
    return "jsonView"; 
} 

感覺自由t o將其標記爲答案並單擊上行鏈路,如果這解決了您的問題。

相關問題