在上篇的基础上
:
修改 pom.xml
<project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: schemalation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd "> <ModelVersion> 4.0.0 </ ModelVersion> <ProupId> com.github.carter659 </proupId> <Artifactid> Spring03 </ artifactid> <version> 0.0.1-snapshot <name> printemps03 </name> <url> http://maven.apache.org </url> <parent> <proupId> org.springframework.boot </proupId> <Artifactid> printemps-boot-starter-Parent </ artifactid> <version> 1.4.2.release </Dease> </parent> <pregerties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </properties> <dépendances> <dependency> <proupId> org.springframework.boot </proupId> <artifactid> spring-boot-starter-thymelea </proupId> </Dependency> <Dedency> <ProupId> org.springFramework.boot </proncId> <ArtifActid> printemps-boot-devtools </ artifactid> <ochotional> true </opultal> </dEpendency> </Detences> <uild> <plugins> <Glugin> <proupId> org.springFramework. <ArtefactId> Spring-Boot-Maven-Plugin </ ArfactId> </Glugin> </Glugins> </Duild> </randing>
修改 app.java
Package com.github.carter659.spring03; import org.springframework.boot.springApplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplicationpublic class app. args); }}
新建 «Order.java» : :
package com.github.carter659.spring03; import java.util.date; commandes de classe publique {chaîne publique no; Date publique Date; Quantité publique publique;}说明一下 : 这里我直接使用 public 字段了 , get / set 方法就不写了。
新建控制器 «contrôleur principal»:
package com.github.carter659.spring03; import java.time.zoneid; import java.util.hashmap; import java.util.map; import org.springframework.sterreotype.controller; import org.springframework.web.bind.annotation.getmapping; import; org.springframework.web.bind.annotation.postmapping; import org.springframework.web.bind.annotation.requestbody; import org.springframework.web.bind.annotation.responsebody; @ControllerPublic Class index de la classe de la classe de la classe (" } @GetMapping ("/ jQuery") String public jQuery () {return "jQuery"; } @Getmapping ("/ angularjs") String public angularjs () {return "angularjs"; } @Postmapping ("/ postdata") public @ResponseBody map <string, objet> postdata (chaîne non, quantité int, date de chaîne) {System.out.println ("no:" + no); System.out.println ("Quantité:" + Quantité); System.out.println ("Date:" + Date); Map <string, object> map = new Hashmap <> (); map.put ("msg", "ok"); map.put ("quantité", quantité); map.put ("non", non); map.put ("date", date); carte de retour; } @Postmapping ("/ postjson") public @ResponseBody map <String, objet> postjson (@Requestbody Order Order) {System.out.println ("Order No:" + Order.No); System.out.println ("Quantité d'ordre:" + ordonnance.quantity); System.out.println ("Date de commande:" + Order.Date.Toinstant (). Atzone (ZoneId.SystemDefault ()). Tolocaldate ()); Map <string, object> map = new Hashmap <> (); map.put ("msg", "ok"); map.put ("valeur", ordre); carte de retour; }}新建 jQuery.html 文件:
<! Doctype html> <html xmlns: th = "http://www.thymeleaf.org"> <adrey> <meta http-equiv = "contenu-ype" contenu = "text / html; charset = utf-8" /> <title> jequery </ title> <script src = "// cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script><script type =" Text / JavaScript "> / * <! [cdata [* / fonction postdata () {var data = 'no =' + $ ('# no'). '& date =' + $ ('# date'). Val (); $ .ajax ({type: 'post', url: '/ postdata', data: data, succès: function (r) {console.log (r);}, error: function () {alert ('error!')}}); } fonction postjson () {var data = {no: $ ('# no'). val (), quantité: $ ('# quantité'). val (), date: $ ('# date'). val ()}; $ .ajax ({type: 'post', contentType: 'application / json', url: '/ postjson', data: json.stringify (data), succès: function (r) {console.log (r);}, error: function () {alert ('error!')}}); } / *]]> * / </ script> </ head> <body> no: <input id = "no" value = "n ° 1234567890" /> <br /> quantité: <entrée id = "Quantity" value = "100" /> <br /> date: <entrée id = "date" value = "2016-12-20" /> <br /> <put value = " onClick = "postdata ()" /> <br /> <input value = "postjson" type = "bouton" onclick = "postjson ()" /> </ body> </html>新建 «Angularjs.html» : :
<! Doctype html> <html xmlns: th = "http://www.thymeleaf.org"> <adread> <meta http-equiv = "contenu-type" contenu = "text / html; charset = utf-8" /> <tetle> anyle src = "// cdn.bootcss.com/angular.js/1.5.6/angular.min.js"></script><script type =" Text / JavaScript "> var app = angular.module ('app', []); app.Controller ('MainController', fonction ($ rootscope, $ scope, $ http) {$ scope.data = {no: 'n ° 1234567890', quantité: 100, 'Date': '2016-12-20'}; $ scope.postjson = funder ', {$ http ({url:' / postjson = '), {$ http ({url:' / postjson = '), {$ http ({url:' / postjson = '), {$ http ({url:' / postjson ', {$ http ({url:' / postjson ', {$ http ({url:' Données: $ scope.data}). Success (fonction (r) {$ scope.ResponseBody = r;});}}); </cript> </ad> <Body ng-app = "app" ng-contrôleur = "Data.No" ng-model = "data.quantity" /> <br /> date: <input id = "date" ng-model = "data.date" /> <br /> <input value = "postjson" type = "Button" ng-Click = "postjson ()" /> <r /> <br /> <v> {{réponse}} </ div> </ body> </ html>:
一、结合 jQuery
运行 app.java 后进去 后进去 «http: // localhost: 8080 / jQuery» 页面
点击 «Postdata» 按钮:
jQuery 成功的调用了 Spring MVC 的后台方法 “public @ResponseBody Map <String, Object> PostData (chaîne no, quantité int, date de chaîne)"
这里 , «Date» 参数我使用的是 String 类型 , 而并不是 Date 类型。因为大多数情况是使用对象形式来接收 Ajax 客户端的值 , 所以我这里偷懒了 , 就直接使用 String 类型。如果想使用 Date 类型 , 则需要使用 @initbinder 注解 , 后面的篇幅中会讲到 , 在这里就不再赘述。
另外 , 使用 «thymeleaf» 模板引擎在编写 js 时 , “&» 关键字要特别注意 , 因为 因为 “thymeleaf» 模板引擎使用的是 xml 语法。因此 , 在 <cript> 标签的开始 ― 结束的位置要加 结束的位置要加 结束的位置要加 / * <! [Cdata [* / ..... / *]]> * / ”
:
<script type = "text / javascript"> / * <! [cdata [* / // code javascript ... / *]]> * / </ script>
否则 , 运行 «thymeleaf» 模板引擎时就会出现错误 «org.xml.sax.saxparseException: ...»
点击 «Postjson» : :
jQuery 则成功调用了后台 `` Public @ResponseBody Map <String, objet> postjson (@Requestbody Order Order) ”方法 ,
并且参数 «Ordre» 中的属性或字段也能被自动赋值 , 而 Date 类一样会被赋值。
注意的是 : 在使用 jQuery 的 $ .ajax 方法时 , contentType 参数需要使用 "application / json" , 而后台 printemps mvc 的 "postjson" 方法中的 "Order" 参数也需要使用 @requestbody 注解。
二、结合 Angularjs
进入 «后进去 http: // localhost: 8080 / angularjs» 页面
点击 «Postjson» : :
使用 Angularjs 后 , 依然能调用 “public @ResponseBody Map <String, objet> postjson (@Requestbody Order Order)” 方法。
代码 : https: //github.com/carter659/spring-boot-03.git
以上就是本文的全部内容 , 希望对大家的学习有所帮助 , 也希望大家多多支持武林网。