spirng mvc3 jqgrid 无法正确返回数据到表格

WEB.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	  <servlet>
  	<servlet-name>RFADispatcher</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath*:springcfg/applicationContext-mvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>RFADispatcher</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-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>
  	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


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

	<import resource="applicationContext.xml"/>

	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
	 	
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">  
			<list>  
		  	<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->  
		  	<bean class="org.springframework.http.converter.StringHttpMessageConverter">
		  		<property name="supportedMediaTypes">
		  			<list>
		  				<value>text/html;UTF-8</value>
		  			</list>
		  		</property>
		  	</bean>
			</list>  
		</property>
	</bean>
	 	
	<bean name="mappingJacksonHttpMessageConverter" 
		class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
		
	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
	 	
	<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />
     
	<!-- enable component scanning (beware that this does not enable mapper scanning!) -->    
	<context:component-scan base-package="com.das.casit.rfa.controller.speed">
  	<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect" />  
	</context:component-scan>

	<mvc:annotation-driven/>
	
	<!-- enable autowire -->
	<context:annotation-config />
    
	<!-- enable transaction demarcation with annotations -->
	<tx:annotation-driven />
	
	<!-- 跳转视图配置 -->
	
	<bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property> 
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	<!-- 
		设置全局异常处理方式。!注意,如果使用XML配置了异常处理方式,
		那么使用Annotation处理异常的方式将不再被使用。 
	-->
	
	<bean name="exceptionResolver" 
	      class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<!-- 这里的errorPage同样应用viewResolver的前缀、后缀匹配 -->
				<prop key="java.lang.Exception">errorPage</prop>
			</props>
		</property>
	</bean>
	
</beans>


application.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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">

	<import resource="applicationContext-bean.xml"/>
	
	<!-- ============================== 数据库配置 ==================================== -->
	<!-- 数据源配置 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
		</property>
		<property name="url">
			<value>jdbc:sqlserver://localhost:1433;DatabaseName=ihToSql;SelectMethod=cursor</value>
		</property>
		<property name="username">
			<value>hydas</value>
		</property>
		<property name="password">
			<value>!Hcf@2009</value>
		</property>
	</bean>
	
	<!-- ================================= 事务控制相关 ============================================= -->
   	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
    	<property name="dataSource" ref="dataSource"></property>
 	</bean> 
	
	<!-- ================================ MyBatis SqlSession配置 ========================================= -->
	<!-- 使用SqlSessionFactoryBean工厂产生SqlSession对象,方便后期注入Dao -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:myBatiscfg/Configuration.xml"></property>
	</bean>
	
</beans>


UserModel.java
package com.das.casit.rfa.controller.speed;

/**
 * @author Administrator
 * 
 */
public class UserModel {
	private String id;
	private String username;
	private int age;

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}

	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}
}


DemoController.java
/**
 * 
 */
package com.das.casit.rfa.controller.speed;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/speed")
public class DemoController {
	private Logger logger = LoggerFactory.getLogger(DemoController.class);

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUserList() {
		logger.info("�б�");
		List<UserModel> list = new ArrayList<UserModel>();
		UserModel um = new UserModel();
		um.setId("1");
		um.setUsername("sss");
		um.setAge(222);
		list.add(um);
		Map<String, Object> modelMap = new HashMap<String, Object>(3);
		modelMap.put("total", "1");
		modelMap.put("data", list);
		modelMap.put("success", "true");
		return modelMap;
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> addUser(@RequestBody UserModel model) {

		Map<String, String> map = new HashMap<String, String>(1);
		map.put("success", "true");
		return map;
	}
}



index.jsp
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Script-Type" content="text/html; charset=UTF-8" />
<title>Spring MVC</title>
		<script src="../jsLib/jqGrid/js/jquery-1.5.2.min.js" type="text/javascript"></script>
		<script src="../jsLib/jqGrid/js/i18n/grid.locale-cn.js" type="text/javascript"></script>
		<script src="../jsLib/jqGrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>	
		<script type="text/javascript" src="index.js"></script>
</head>
<body>
<div id="info"></div>
<form action="add" method="post" id="form">
编号:<input type="text" name="id"/>
姓名:<input type="text" name="username"/>
年龄:<input type="text" name="age"/>

<input type="button" value="提交" id="submit"/>
</form>
</body>
</html>


index.js
$.fn.serializeObject = function() {
	var o = {};
	var a = this.serializeArray();
	$.each(a, function() {
		if (o[this.name]) {
			if (!o[this.name].push) {
				o[this.name] = [ o[this.name] ];
			}
			o[this.name].push(this.value || '');
		} else {
			o[this.name] = this.value || '';
		}
	});
	return o;
};

$(document).ready(
		function() {
			alert("here");
			jQuery.ajax( {
				type : 'GET',
				contentType : 'application/json',
				url : 'user/list',
				dataType : 'json',
				success : function(data) {
					if (data && data.success == "true") {
						$('#info').html("共" + data.total + "条数据。<br/>");
						$.each(data.data, function(i, item) {
							$('#info').append(
									"编号:" + item.id + ",姓名:" + item.username
											+ ",年龄:" + item.age);
						});
					}
				},
				error : function() {
					alert("error")
				}
			});
			$("#submit").click(function() {
				var jsonuserinfo = $.toJSON($('#form').serializeObject());
				alert(jsonuserinfo);
				jQuery.ajax( {
					type : 'POST',
					contentType : 'application/json',
					url : 'user/add',
					data : jsonuserinfo,
					dataType : 'json',
					success : function(data) {
						alert("新增成功!");
					},
					error : function(data) {
						alert("error")
					}
				});
			});
		});


运行结果如下图所示,




使用Chrome调试时,提示信息如下:

Resource interpreted as Document but transferred with MIME type application/json.

还没有找到解决办法,嘿嘿

你可能感兴趣的:(java,jquery,Web,mvc,json)