组建Jquery+Spring3 MVC架构的尝试

来源:互联网 发布:数据分析算法 python 编辑:程序博客网 时间:2024/06/07 19:02

资料来源:http://www.javaeye.com/topic/651227

看完上面的文章深受启发,于是先尝试着把上文中的工程搭建起来.

代码如下:

CustomerController.java

package com.jqweb.action;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;import com.jqweb.model.Customer;@Controller@RequestMapping("/customerInfo")public class CustomerController {@RequestMapping(value = "/new", method = RequestMethod.POST)@ResponseBodypublic Customer newCustomer(@RequestBody Customer customer) {customer.setName(customer.getName() + "shenbin");customer.setAddr(customer.getAddr() + "kunshan");return customer;}}

Customer.java

package com.jqweb.model;import java.io.Serializable;public class Customer implements Serializable {private static final long serialVersionUID = 6661953752626765129L;private String name;private String addr;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddr() {return addr;}public void setAddr(String addr) {this.addr = addr;}}

jqWeb-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:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:component-scan base-package="com.jqweb.action"/><bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters"><util:list id="beanList"><ref bean="mappingJacksonHttpMessageConverter" /></util:list></property></bean><bean id="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /></beans>

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>jqWeb</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>jqWeb</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

index.jsp

<html><head><script type="text/javascript" src="js/jquery-1.4.2.js"></script><script type="text/javascript" src="js/json2.js"></script><script type="text/javascript"><!--function save() {var elemUserinfo = $('#customerInfo');var userinfo = elemUserinfo.serializeObject();  var jsonuserinfo = JSON.stringify(userinfo);jQuery.ajax({type : 'POST',contentType : 'application/json',url : 'customerInfo/new.do',data : jsonuserinfo,dataType : 'json',success : function(data){$('div#responseName').text(data.name);  $('div#responseAddr').text(data.addr);  },error : function(data){alert(data);}});  }$.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;  };// --></script></head><body><form id="customerInfo"><input name="name" type="text" value="123"><input name="addr" type="text" value="234"></form><br><input type="button" value="test" onclick="save()"><br><div id="responseName"></div><div id="responseAddr"></div></body></html>


注:

以上使用的是Spring3.0.1版本。

Jackson JSON Processor下载地址:http://jackson.codehaus.org/

json2下载地址:http://www.json.org/js.html

下来发现在Spring3.0.2版本下最新的Jackson JSON Processor版本不能正常工作,参见:http://jira.springframework.org/browse/SPR-7063?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel

 解决等办法是等待Spring3的下一个子版本,或者如上图所示使用低版本的Jackson JSON Processor

 

原创粉丝点击