使用Ext JS Ajax调用Spring MVC 406响应

时间:2014-10-03 22:39:00

标签: java xml spring spring-mvc extjs

我目前正在开发一个简单的应用程序,它从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:

@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-调度-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>

弹簧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>

弹簧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中更改accept和content-type标头,并为listAllAdminUsers方法定义produces类型。当我得到一个404(我尝试了几个不同的东西)时,spring正在尝试将控制器方法解析为视图并且正在寻找admin / users / list.jsp(不知道为什么?)。通过搜索无数的答案,我已经用尽所有的选项,我已经尝试了所有的一切。请帮忙!!

2 个答案:

答案 0 :(得分:0)

你有两个问题: 1.第一个是关于Accept标头。你提到的问题你解决了。 2.关于网址映射,在您的请求中发送请求 /admin/users/list.htm ,但您的控制器不仅可以响应对 / 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'
        }
    }
});

答案 1 :(得分:0)

我必须改变的是将@ResponseBody放到返回模型上,而不是放在方法本身上。