简体   繁体   English

Spring MVC Servlet映射和406 HTTP核心

[英]Spring mvc servlet-mapping and 406 http core

I'm working with Spring mvc 4 and glassfish 4 and I had some problems with ajax calls to return object as json, with @ResponseBody. 我正在使用Spring mvc 4和glassfish 4,使用ajax调用时遇到一些问题,即使用@ResponseBody将对象作为json返回。 Then I found a proper solution HTTP Status 406. Spring MVC 4.0, jQuery, JSON that it did not totally work on my project I think because my servlet-mapping. 然后,我找到了一个合适的解决方案HTTP Status406。Spring MVC 4.0,jQuery,JSON无法完全在我的项目上运行,我认为这是因为我的servlet映射。

My servlet mapping was: 我的servlet映射是:

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

But I want also to serve the request like /* to avoid problems with 406 http code so I've tried this: 但我也想像/ *这样处理请求,以避免406 http代码出现问题,因此我尝试了以下方法:

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

But then the dispatcher also maps the *.jsp and breaks At the end I've tried this: 但是,调度程序还映射了* .jsp并中断了它。最后,我尝试了以下操作:

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

But then the dispatcher maps the *.js and *.css like bootstrap, jquery etc. 但是,然后调度程序将* .js和* .css映射为引导程序,jquery等。

And now I ran out of ideas. 现在我没有了主意。

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. 为了解决406 http代码问题,我从ajax指定了内容类型,并从控制器中删除了* .htm。

And for the other side I had to fix the servlet-mapping and add the resource tag to avoid more problems. 另一方面,我必须修复servlet映射并添加资源标签,以避免出现更多问题。

Now my servlet-mapping is like this: 现在我的servlet映射是这样的:

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

And I add this line to the dispatcher-servlet: 然后将此行添加到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/" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM