【问题标题】:Spring MVC 406 Response using Ext JS Ajax Call使用 Ext JS Ajax 调用的 Spring MVC 406 响应
【发布时间】:2014-10-03 22:38:59
【问题描述】:

我目前正在开发一个简单的应用程序,该应用程序从 MySQL 数据库中获取用户列表并以 JSON 格式返回数据。我正在使用 Ext JS Store 进行 ajax 调用。

商店:

Ext.define('MyApp.store.AdminUsers', {
    extend: 'MyApp.store.BaseStore'
    ,model: 'MyApp.model.AdminUser'
    ,remoteSort: true
    ,pageSize: 20   
    ,proxy: {
        type: 'ajax'
        ,url: contextPath + '/admin/users/list.htm'
        ,actionMethods: 'POST'
        ,simpleSortMode: true
        ,pageParam: undefined
        ,reader: {
            type: 'json'
            ,root: 'adminUsers'
            ,successProperty: 'success'
            ,totalProperty: 'totalCount'
        }
    }
});

Spring MVC 控制器:

@Controller
@RequestMapping("/admin/users/")
public class AdminUserController extends BaseController {
 // items removed for brevity
@RequestMapping(value = "/list")
@ResponseBody 
public Model listAllAdminUsers(Model model, HttpServletRequest request) throws Exception {
    FindInfo findInfo = getFindInfo(request);
    model.addAttribute("totalCount", adminUserService.getCount(findInfo));
    model.addAttribute("adminUsers",    AdminUserConverter.createListData(adminUserService.getList(findInfo)));
        return model;
    }
}

web.xml:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<!-- Spring Security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springConfig/applicationContext.xml</param-value>
</context-param>

<!-- Spring MVC -->
<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springConfig/mvc-dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<session-config>
    <session-timeout>20</session-timeout>
</session-config>

mvc-dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <import resource="spring-controller.xml" />
    <import resource="spring-view.xml" />
    <context:component-scan base-package="com.myapp.web.controller" />

    <mvc:resources mapping="/lib/**" location="/lib/" />
    <mvc:resources mapping="/extjs/**" location="/extjs/" />

    <mvc:annotation-driven />
</beans>

spring-controller.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd">      

    <!-- Configuration to recognize annotation based controller -->
<!--     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter" />
            </list>
        </property>
    </bean>


    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

</beans>

spring-view.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd" >

    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"></bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

我的类路径上有正确的 Jackson 库,有时我收到 406 错误,并尝试更改 Ext JS 中的接受和内容类型标头并为 listAllAdminUsers 方法定义 produces 类型。当我得到 404 时(我尝试了几种不同的方法),spring 正在尝试将控制器方法解析为视图并正在寻找 admin/users/list.jsp (不知道为什么?)。通过搜索大量答案,我已经用尽了所有选项,并且我已经尝试了所有看起来的方法。请帮忙!!

【问题讨论】:

    标签: java xml spring spring-mvc extjs


    【解决方案1】:

    你有两个问题: 1.首先是关于Accept header。您提到的问题已解决。 2.关于url映射,在你的请求中你发送一个请求/admin/users/list.htm但是你的Controller不能只响应对/admin/users/list

    试试这个

    Ext.define('MyApp.store.AdminUsers', {
        extend: 'MyApp.store.BaseStore'
        ,model: 'MyApp.model.AdminUser'
        ,remoteSort: true
        ,pageSize: 20   
        ,proxy: {
            type: 'ajax'
            ,url: contextPath + '/admin/users/list'
            ,actionMethods: 'POST'
            ,simpleSortMode: true
            ,pageParam: undefined
            ,reader: {
                type: 'json'
                ,root: 'adminUsers'
                ,successProperty: 'success'
                ,totalProperty: 'totalCount'
            }
        }
    });
    

    【讨论】:

    • 感谢您的回答,但我仍然收到 404 并且出于某种原因它正在寻找 admin/users/list.jsp。奇怪的是代码正在进入该方法,但由于某种原因没有返回模型。想法?
    • 您能发布您的完整控制器代码吗?
    • 除了自动装配一个服务类和一个验证器,没有什么别的了。这很奇怪,因为它正在调用控制器(可以看到 sysout)但它不会返回模型:/
    • 我还应该说我注意到如果我返回一个字符串,它可以工作并且没有 404。
    【解决方案2】:

    我所要做的只是将@ResponseBody 放在返回模型上,而不是放在方法本身上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      相关资源
      最近更新 更多