Java 使用extjsajax调用的springmvc406响应
我目前正在开发一个简单的应用程序,它从MySQL数据库获取用户列表,并以JSON格式返回数据。我正在使用ExtJS存储进行ajax调用 商店:Java 使用extjsajax调用的springmvc406响应,java,xml,spring,spring-mvc,extjs,Java,Xml,Spring,Spring Mvc,Extjs,我目前正在开发一个简单的应用程序,它从MySQL数据库获取用户列表,并以JSON格式返回数据。我正在使用ExtJS存储进行ajax调用 商店: Ext.define('MyApp.store.AdminUsers', { extend: 'MyApp.store.BaseStore' ,model: 'MyApp.model.AdminUser' ,remoteSort: true ,pageSize: 20 ,proxy: { ty
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>
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
上下文配置位置
类路径:springConfig/applicationContext.xml
mvc调度器
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
类路径:springConfig/mvc-dispatcher-servlet.xml
1.
mvc调度器
*.htm
index.jsp
20
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错误,并尝试在extjs中更改accept和content-type头,并为listalAdminusers方法定义products
type。当我得到一个404时(我尝试了几种不同的方法),spring试图将控制器方法解析为view,并寻找admin/users/list.jsp(不确定为什么?)。我已经用尽了所有的选择,搜索了无数的答案,我已经尝试了一切。请帮忙 您有两个问题:
1.第一个是关于Accept头的。正如你所说,你解决了这个问题。
2.是关于url映射的,在您的请求中,您发送一个请求/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'
}
}
});
我所要做的更改是将
@ResponseBody
放在返回模型上,而不是放在方法本身上。谢谢你的回答,但我仍然得到404,出于某种原因,它正在寻找admin/users/list.jsp。奇怪的是,代码进入了该方法,但由于某种原因没有返回模型。想法?你能发布完整的控制器代码吗?除了自动连接服务类和验证器之外,没有什么其他的了。这是非常奇怪的,因为它正在进行控制器调用(可以看到一个sysout),但它不会返回模型:/我还应该说,我注意到,如果我返回一个字符串,它会工作,没有404。