0

我正在使用 Spring mvc 4 和 glassfish 4,我在使用 @ResponseBody 将对象返回为 json 的 ajax 调用时遇到了一些问题。然后我找到了一个合适的解决方案HTTP Status 406. Spring MVC 4.0, jQuery, JSON,我认为它在我的项目上并不完全适用,因为我的 servlet 映射。

我的 servlet 映射是:

   <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

但我也想提供像 /* 这样的请求,以避免 406 http 代码出现问题,所以我尝试了这个:

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

但是随后调度程序也映射了 *.jsp 并中断了最后我尝试了这个:

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

但随后调度程序映射 *.js 和 *.css,如 bootstrap、jquery 等。

现在我的想法已经用完了。

4

1 回答 1

0

At the end I fix it. For one side to fix the 406 http code problem I specify the content type from ajax and I remove the *.htm from the controller.

And for the other side I had to fix the servlet-mapping and add the resource tag to avoid more problems.

Now my servlet-mapping is like this:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

And I add this line to the dispatcher-servlet:

<bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

<mvc:resources mapping="/resources/**" location="/resources/" />
于 2014-10-28T23:02:07.360 回答