求助spring mvc 用ajax请求报406 Not Acceptable

xiangzi7758258 2016-03-01 09:14:23
spring mvc 用ajax请求报406 Not Acceptable 求大神帮忙 qq:978497958 可以远程 qq上聊
...全文
336 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
keygod1 2016-03-02
  • 打赏
  • 举报
回复
方法上的@RequestMapping("resultMap"),,你加了.json,,那么地址就是resultMap.json.json了把?
xiangzi7758258 2016-03-02
  • 打赏
  • 举报
回复
引用 4 楼 hjkhongjinkai 的回复:
java code @responsebody 怎么在public后面 不是应该在方法上面吗?
放到上面还是一样
hjkhongjinkai 2016-03-02
  • 打赏
  • 举报
回复
java code @responsebody 怎么在public后面 不是应该在方法上面吗?
hjkhongjinkai 2016-03-02
  • 打赏
  • 举报
回复
好复杂的样子!!!!!!!!!!!!!!!!!!!
xiangzi7758258 2016-03-02
  • 打赏
  • 举报
回复
配置文件 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>Map</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

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

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

	<!-- 配置Spring的用于解决懒加载问题的过滤器 只能解决同一个请求的懒加载问题 -->
	<filter>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>

	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.json</url-pattern>
	</servlet-mapping>
</web-app>
springmvc-servlet.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
	<context:component-scan base-package="com.yisa.map.web"></context:component-scan><!--springMVC扫描的包 --> 	
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean>
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
        <property name="messageConverters">   
            <list>   
                <bean class = "org.springframework.http.converter.StringHttpMessageConverter">   
                    <property name = "supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>   
                        </list>   
                    </property>   
                </bean>   
            </list>   
        </property>  
    </bean>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
        <property name="messageConverters"> 
            <list>  
                <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 
                    <property name="supportedMediaTypes"> 
                        <list> 
                            <value>text/html; charset=utf-8</value> 
                        </list> 
                    </property> 
                </bean>
            </list>  
        </property> 
    </bean>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">   
        <property name="interceptors">  
            <list>     
            </list>  
        </property>  
    </bean>  

</beans>
applicationContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="com.yisa.map"></context:component-scan>
	<!-- hibernate -->
	 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/map?useUnicode=true" />
		<property name="username" value="root" />
		<property name="password" value="mgx" />
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="annotatedClasses">
			<list>
				<value>com.yisa.map.domain.Location</value>
				<value>com.yisa.map.domain.img.Maptile</value>
				<value>com.yisa.map.domain.img.Room</value>
				<value>com.yisa.map.domain.point.Pp</value>
				<value>com.yisa.map.domain.nav.Nav</value>
				<value>com.yisa.map.domain.LocationVo</value>
			</list>
		</property>
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>
	
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- hibernate模板 -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<tx:annotation-driven transaction-manager="transactionManager" />

</beans>
是榴莲啊 2016-03-02
  • 打赏
  • 举报
回复
这个url有点奇怪,car前面不应该有“/”吗,,“/car/...”
xiangzi7758258 2016-03-02
  • 打赏
  • 举报
回复
引用 6 楼 keygod1 的回复:
方法上的@RequestMapping("resultMap"),,你加了.json,,那么地址就是resultMap.json.json了把?
再加上.json就404
xiangzi7758258 2016-03-01
  • 打赏
  • 举报
回复
<script>
        $(function () {
            $("p").click(function () {
            	$.ajax({
                    url:'car/resultMap.json',
                    type:'post',
                    dataType:'json',
                    success:function(data){
                        alert(data.success);
                        alert(data.message);
                    },
                    error:function(){
                        alert('System is wrong.'); 
                    }
                });
			});
        });
    </script>
<body>
<p>111111</p>
</body>

@RequestMapping("/car")
public class CarCharacterController{
@RequestMapping("/resultMap.json")
    public @ResponseBody Map<String, Object> getAjaxDataByResponseBody() {
        System.out.println("通过注解@ResponseBody返回JSON数据");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", true);
        map.put("message", "Successfully returning the data.");
        return map;
    }
}

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧