2014-10-03 47 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