0

当访问给定的 URL 时,我试图将 json 列表从控制器发送到 javascript。使用此代码,当我转到 URL 时收到 406 错误。我的 servlet 上下文有<annotation-driven />并且我有org.codehaus.jacksonMaven 依赖项。有没有其他原因给我一个 406,或者有没有更好的方法来发送一个列表到 javascript?

控制器功能

@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;
    }
4

1 回答 1

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 下创建 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>

控制器更改:-

@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";
}

如果这解决了您的问题,请随意将其标记为答案并单击上行链接。

于 2013-03-07T04:06:19.360 回答