2014-10-03 32 views
0

我目前正在研究一个简单的应用程序,该应用程序从MySQL数据库获取用户列表并将数据作为JSON格式返回。我正在使用Ext JS Store进行ajax调用。使用Ext JS Ajax调用的Spring MVC 406响应

商店:

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

我有我的类路径正确杰克逊库,有时我得到406错误,尝试在Ext JS中更改接受和内容类型标题,并为listAllAdminUsers方法定义produces类型。当我得到一个404(我尝试了几种不同的东西)时,spring试图将控制器方法解析为视图,并且正在寻找admin/users/list.jsp(不知道为什么?)。我已经用尽了所有的选项,搜索了很多答案,并且我尝试了一切似乎。请帮忙!!

回答

0

所有我不得不改变是把@ResponseBody到收益模型,而不是方法本身。

0

你有两个问题: 1.第一个是关于Accept头。您提到的问题已解决。 2.关于URL映射,在你的要求,你发送一个请求/admin/users/list.htm但你的控制器不仅响应请求/管理/用户/列表所做

试试这个

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' 
     } 
    } 
}); 
+0

感谢您的回答,但我仍然得到404,并且出于某种原因正在寻找admin/users/list.jsp。奇怪的是代码正在进入该方法,但由于某种原因没有返回模型。思考? – invictvs1 2014-10-06 18:08:43

+0

请问您可以发布完整的控制器代码吗? – Pracede 2014-10-06 19:59:11

+0

除了自动装配服务类和验证程序之外,没有其他的东西了。这是非常奇怪的,因为它正在调用控制器(可以看到一个sysout),但它不会返回模型:/ – invictvs1 2014-10-06 20:21:45