在上篇的基础上
准备工作 :
修改 pom.xml
<project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance http://maven.apache.org/xsd/maven-4.0.0.xsd "> <modelVersion> 4.0.0 </modelversion> <gruppe> com.github.carter659 </GroupId> <artifactId> spring03 </artifactid> <Version> 0.0.1/0.1/0.1/0.1/0.1/0.1/0.1/0.1/0.1/0.1/0.1/0.1/0.1/0.1/0.1/0.1/0.1/0.1/0.1/0.0.1-1-Snapshot> <name>spring03</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </abhängig> <depeopcy> <GroupId> org.springFramework.boot </GroupId> <artifactId> Spring-Boot-Devtools </artifactid> <optional> true </optional> </abhängig> </abhängig> <builds <artifactid> Spring-Boot-Maven-Plugin </artifactId> </plugin> </plugins> </build> </project>
修改 app.java
Paket com.github.carter659.spring03; import org.springframework.boot.springApplication; import org.springframework.boot.autoconFigure.springbootApplication; @springbootApplication -Class -Applicta -Application. args); }}
新建 "order.java" 类文件 :
Paket com.github.carter659.spring03; import Java.util.date; öffentliche Klasse Order {public String Nr.; öffentliches Datum; öffentliche int Menge;}说明一下 : 这里我直接使用 öffentlich 字段了 , Get/Set 方法就不写了。
新建控制器 "MainController":
Paket com.github.carter659.spring03; import Java.Time.zoneid; Import Java.util.hashMap; Import Java.util.map; org.springframework.web.bind.annotation.postmapping; import org.springframework.web.bind.annotation.requestbody import org.spingframework "Index"; } @GetMapping ("/jQuery") public String jQuery () {return "jQuery"; } @GetMapping ("/Angularjs") public String angularjs () {return "AngularJS"; } @Postmapping ("/postdata") public @ResponseBody map <String, Objekt> Postdata (String Nr. System.out.println ("Menge:" + Menge); System.out.println ("Datum:" + Datum); Karte <String, Objekt> map = new HashMap <> (); map.put ("msg", "ok"); map.put ("Menge", Menge); map.put ("no", no); map.put ("Datum", Datum); Rückgabekarte; } @Postmapping ("/postjson") public @ResponseBody map <String, Objekt> Postjson (@RequestBody Order) {System.out.println ("Order No:" + order.no); System.out.println ("Order -Menge:" + order.quantity); System.out.println ("Bestelldatum:" + order.date.toinstant (). Atzone (zoneId.SystemDefault ()). Tolocaldate ()); Karte <String, Objekt> map = new HashMap <> (); map.put ("msg", "ok"); map.put ("value", order); Rückgabekarte; }}新建 jQuery.html 文件:
<! DocType html> <html xmlns: th = "http://www.thymeleaf.org"> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <titels> jquery </title> </title> </title> </title> src = "// cdn.bootcs.com/jquery/1.11.3/jquery.min.js"></script><Script type =" text/javaScript ">/*<! [cdata [*/function postdata () {var data = ' + $ ('#no '). '& date =' + $ ('#Date'). Val (); $ .ajax ({type: 'post', url: '/postdata', Daten: Daten, Erfolg: Funktion (r) {console.log (r);}, error: function () {alert ('error!')}}); } function postjson () {var data = {no: $ ('#no'). val (), Menge: $ ('#quantity'). val (), Datum: $ ('#Datum'). Val ()}; $ .ajax ({type: 'post', contentType: 'application/json', url: '/postjson', Daten: json.stringify (Daten), Erfolg: Funktion (r) {console.log (r);}, fehler: function () {alert ('error!')}}); } /*]]>*/</script></head><body> no: <input id="no" value="No.1234567890" /> <br /> quantity: <input id="quantity" value="100" /> <br /> date: <input id="date" value="2016-12-20" /> <br /> <input value="postData" type="button" onclick = "postdata ()" /> <br /> <Eingabewert = "postjson" type = "button" onclick = "postjson ()" /> < /body> < /html>新建 "AngularJs.html" 文件 :
<! DocType html> <html xmlns: th = "http://www.thymeleaf.org"> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <titels> Angularjs </title> </title> </title> </title> </title> </title> src = "// cdn.bootcs.com/angular.js/1.5.6/angular.min.js"></script><Script type =" text/javaScript "> var app = angular.module ('App', []); App.Controller ('MainController', Funktion ($ rootscope, $ scope, $ http) {$ scope.data = {no: 'Nr. 1234567890', Menge: 100, 'Date': '2016-12-20'}; Daten: $ scope.data}). Erfolg (Funktion (r) {$ scope.responseBody = r;}); ng-model="data.quantity" /> <br /> date: <input id="date" ng-model="data.date" /> <br /> <input value="postJson" type="button" ng-click="postJson()" /> <br /> <br /> <div>{{responseBody}}</div></body></html>项目结构如下图 :
一、结合 jQuery
运行 app.java 后进去 "http: // localhost: 8080/jQuery" 页面
点击 "Postdata" 按钮 :
JQuery 成功的调用了 Spring MVC 的后台方法 "public @ResponseBody Map <String, Objekt> Postdata (String Nr., Int Quantity, String Datum)"
这里 , „Datum“ 参数我使用的是 String 类型 , 而并不是 Datum 类型。因为大多数情况是使用对象形式来接收 ajax 客户端的值 , 所以我这里偷懒了 , 就直接使用 String 类型。如果想使用 Datum 类型 则需要使用 则需要使用 @initbinder 注解 , 后面的篇幅中会讲到 , 在这里就不再赘述。 在这里就不再赘述。 在这里就不再赘述。 在这里就不再赘述。 类型 则需要使用 则需要使用 @initbinder
另外 另外 使用 使用 "Thymeleaf" 模板引擎在编写 js 时 , "&" 关键字要特别注意 , "Thymeleaf" 模板引擎使用的是 xml 语法。因此 , 在 <script> 标签的开始 标签的开始 "/*<! [CDATA [*/...../*]>*/"
例如 :
<script type = "text/javaScript">/*<! [cdata [*// JavaScript Code .../*]]>*/</script>
否则 , 运行 "Thymeleaf" 模板引擎时就会出现错误 "org.xml.sax.saxparseException: ..."
点击 "postjson" 按钮 :
JQuery 则成功调用了后台 "public @ResponseBody Map <String, Objekt> Postjson (@RequestBody Order)" 方法 , ,
并且参数 "Bestellung" 中的属性或字段也能被自动赋值 , 而 Datum 类一样会被赋值。
注意的是 : 在使用 JQuery 的 $ .ajax 方法时 , , contentType 参数需要使用 "application/json" , 而后台 Spring MVC 的 "postjson" 方法中的 "order" 参数也需要使用 @RequestBody 注解。
二、结合 Angularjs
进入 进入 后进去 http: // localhost: 8080/angularjs ”页面
点击 "postjson" 按钮 :
使用 AngularJS 后 , 依然能调用 依然能调用 "public @Responbody map <String, Objekt> Postjson (@RequestBody Order)" 方法。 方法。
代码 : https: //github.com/carter659/spring-boot-03.git
以上就是本文的全部内容 , 希望对大家的学习有所帮助 , 也希望大家多多支持武林网。 也希望大家多多支持武林网。