An attempt to build the jquery + spring3 MVC Architecture

Source: Internet
Author: User

Source: http://www.javaeye.com/topic/651227

 

After reading the above article, I was inspired to try to build the project above.

 

 

The Code is as follows:

Customercontroller. Java

Package COM. jqweb. action; </P> <p> Import Org. springframework. stereotype. controller; <br/> Import Org. springframework. web. BIND. annotation. requestbody; <br/> Import Org. springframework. web. BIND. annotation. requestmapping; <br/> Import Org. springframework. web. BIND. annotation. requestmethod; <br/> Import Org. springframework. web. BIND. annotation. responsebody; </P> <p> Import COM. jqweb. model. customer; </P> <p> @ controller <br/> @ requestmapping ("/customerinfo ") <br/> public class customercontroller {</P> <p> @ requestmapping (value = "/New", method = requestmethod. post) <br/> @ responsebody <br/> Public customer newcustomer (@ requestbody customer) {</P> <p> customer. setname (customer. getname () + "shenbin"); <br/> customer. setaddr (customer. getaddr () + "Kunshan"); </P> <p> return customer; <br/>}< br/>}

Customer. Java

Package COM. jqweb. model; </P> <p> Import Java. io. serializable; </P> <p> Public Class Customer implements serializable {</P> <p> Private Static final long serialversionuid = 6661953752626765129l; </P> <p> private string name; </P> <p> private string ADDR; </P> <p> Public String getname () {<br/> return name; <br/>}</P> <p> Public void setname (string name) {<br/> This. name = Name; <br/>}</P> <p> Public String getaddr () {<br/> return ADDR; <br/>}</P> <p> Public void setaddr (string ADDR) {<br/> This. ADDR = ADDR; <br/>}< br/>}

JqWeb-servlet.xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xmlns: P = "http://www.springframework.org/schema/p" <br/> xmlns: util = "http://www.springframework.org/schema/util" <br/> xmlns: context = "http://www.springframework.org/schema/context" <br/> xsi: schemalocation = "</P> <p> http://www.springframework.org/schema/beans </P> <p> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd </P> <p> http://www.springframework.org/schema/util </P> <p> http://www.springframework.org/schema/util/spring-util-2.5.xsd </P> <p> http://www.springframework.org/schema/context </P> <p> http://www.springframework.org/schema/context/spring-context-2.5.xsd "> </P> <p> <context: component-scan base-package = "com. jqweb. action "/> </P> <p> <Bean class =" org. springframework. web. servlet. MVC. annotation. defaultannotationhandlermapping "/> </P> <p> <Bean class =" org. springframework. web. servlet. MVC. annotation. annotationmethodhandleradapter "> <br/> <property name =" messageconverters "> <br/> <util: list id = "beanlist"> <br/> <ref bean = "mappingjacksonhttpmessageconverter"/> <br/> </util: list> <br/> </property> <br/> </bean> </P> <p> <bean id = "mappingjacksonhttpmessageconverter" <br/> class = "org. springframework. HTTP. converter. JSON. mappingjacksonhttpmessageconverter "/> </P> <p> </beans>

Web. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/javaee <br/> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> </P> <p> <servlet> <br/> <servlet-Name> jqweb </servlet-Name> <br/> <servlet-class> Org. springframework. web. servlet. dispatcherservlet </servlet-class> <br/> <load-on-startup> 1 </load-on-startup> <br/> </servlet> </P> <p> <servlet-mapping> <br/> <servlet-Name> jqweb </servlet-Name> <br/> <URL-pattern> *. DO </url-pattern> <br/> </servlet-mapping> </P> <p> <welcome-file-List> <br/> <welcome-File> index. JSP </welcome-File> <br/> </welcome-file-List> </P> <p> </Web-app>

Index. jsp

<HTML> <br/> <pead> <br/> <SCRIPT type = "text/JavaScript" src = "JS/jquery-1.4.2.js"> </SCRIPT> <br/> <script Type = "text/JavaScript" src = "JS/json2.js"> </SCRIPT> <br/> <SCRIPT type = "text/JavaScript"> <! -- <Br/> function save () {<br/> var elemuserinfo = $ ('# mermerinfo'); <br/> var userinfo = elemuserinfo. serializeobject (); <br/> var jsonuserinfo = JSON. stringify (userinfo); <br/> jquery. ajax ({<br/> type: 'post', <br/> contenttype: 'application/json', <br/> URL: 'mermerinfo/New. do ', <br/> data: jsonuserinfo, <br/> datatype: 'json', <br/> success: function (data) {<br/> $ ('div # responsename '). text (Data. Name); <br/> $ ('div # responseaddr '). text (data. ADDR); <br/>}, <br/> error: function (data) {<br/> alert (data ); <br/>}< br/>}); <br/>}< br/> $. FN. serializeobject = function () {<br/> var o = {}; <br/> var A = This. serializearray (); <br/> $. each (A, function () {<br/> If (O [this. name]) {<br/> If (! O [this. name]. push) {<br/> O [this. name] = [O [this. name]; <br/>}< br/> O [this. name]. push (this. value | ''); <br/>}else {<br/> O [this. name] = This. value | ''; <br/>}< br/>}); <br/> return O; <br/> }; <br/> // --> </SCRIPT> <br/> </pead> <br/> <body> <br/> <Form ID = "customerinfo"> <br/> <input name = "name" type = "text" value = "123"> <br/> <input name = "ADDR" type = "text" value =" 234 "> <br/> </form> <br/> <input type =" button "value =" test "onclick =" Save () "> <br/> <Div id =" responsename "> </div> <br/> <Div id =" responseaddr "> </Div> <br/> </body> <br/> </ptml>

 

Note:

The above version is spring3.0.1.

Jackson JSON processor: http://jackson.codehaus.org/

Json2: http://www.json.org/js.html

 

It was found that the latest Jackson JSON processor version in spring3.0.2 could not work, see: http://jira.springframework.org/browse/SPR-7063? Page = com. Atlassian. Jira. plugin. system. issuetabpanels % 3achangehistory-tabpanel

 

The solution is to wait for the next sub-version of spring3, or use the Jackson JSON processor of a lower version, as shown in.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.