在上篇的基础上
准备工作:
修改 pom.xml
<Project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: schemalocation = "http:/MANCMMMMMMMMMMMMMMMMMMMMMMMMMMBM http://maven.apache.org/xsd/maven-4.0.0.xsd "> <moderVersion> 4.0.0 </modelversion> <moderversion> <moderversion> <name> spring03 </name> <url> http://maven.apache.org </url> <carent> <roupId> org.springframework.boot </groupId> <ArtIfactId> สปริง-สตาร์สตาร์เทอร์ </artifactid> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </premerties> <pernceies> </perctency> <predency> <roupId> org.springframework.boot </groupid> <ratifactid> Spring-Boot-Devtools </artifactid> <plorial> True </onftiveal> </การพึ่งพา> </การพึ่งพา <ArtIfactId> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugins> </uffer
修改 app.java
แพ็คเกจ com.github.carter659.spring03; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springBootapplication; -
新建“ order.java” 类文件:
แพ็คเกจ com.github.carter659.spring03; นำเข้า java.util.date; คำสั่งระดับสาธารณะ {สตริงสาธารณะหมายเลข; วันที่สาธารณะ ปริมาณ int สาธารณะ;}说明一下: 这里我直接使用สาธารณะ字段了, get/set 方法就不写了。
新建控制器“ MainController”:
แพ็คเกจ com.github.carter659.spring03; นำเข้า java.time.zoneid; นำเข้า java.util.hashmap; นำเข้า java.util.map นำเข้า org.springframework.stereotype.controller; org.springframework.web.bind.annotation.postmapping; นำเข้า org.springframework.web.bind.annotation.Requestbody; นำเข้า org.springframework.web.bind.annotation.responsebody; @controllerpublic } @getMapping ("/jQuery") สตริงสาธารณะ jQuery () {return "jQuery"; } @getMapping ("/angularjs") สตริงสาธารณะ angularjs () {return "angularjs"; } @PostMapping ("/postdata") แผนที่สาธารณะ @ResponseBody แผนที่ <สตริงวัตถุ> postdata (สตริงหมายเลข, ปริมาณ int, วันที่สตริง) {system.out.println ("ไม่:" + ไม่); System.out.println ("ปริมาณ:" + ปริมาณ); System.out.println ("วันที่:" + วันที่); แผนที่ <string, Object> map = new hashmap <> (); map.put ("msg", "ตกลง"); map.put ("ปริมาณ", ปริมาณ); map.put ("ไม่", ไม่); map.put ("วันที่", วันที่); แผนที่กลับ; } @PostMapping ("/postjson") แผนที่สาธารณะ @ResponseBody แผนที่ <สตริงวัตถุ> postjson (@requestbody สั่งซื้อลำดับ) {system.out.println ("หมายเลขสั่งซื้อ:" + order.no); System.out.println ("ปริมาณการสั่งซื้อ:" + order.quantity); System.out.println ("วันที่สั่งซื้อ:" + order.date.toinstant (). atzone (zoneid.systemdefault ()). tolocaldate ()); แผนที่ <string, Object> map = new hashmap <> (); map.put ("msg", "ตกลง"); map.put ("value", order); แผนที่กลับ; -新建 jQuery.html 文件:
<! doctype html> <html xmlns: th = "http://www.thymeleaf.org"> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> src = "// cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script> <script type =" ข้อความ/javascript ">/*<! '& date =' + $ ('#วันที่'). val (); $ .ajax ({type: 'post', url: '/postdata', ข้อมูล: ข้อมูล, ความสำเร็จ: ฟังก์ชั่น (r) {console.log (r);}, ข้อผิดพลาด: ฟังก์ชั่น () {แจ้งเตือน ('ข้อผิดพลาด!')}}); } function postjson () {var data = {ไม่: $ ('#no'). val (), ปริมาณ: $ ('#ปริมาณ'). val (), วันที่: $ ('#วันที่'). val ()}; $ .ajax ({ประเภท: 'โพสต์', contentType: 'แอปพลิเคชัน/json', url: '/postjson', ข้อมูล: json.stringify (ข้อมูล), ความสำเร็จ: ฟังก์ชั่น (r) {console.log (r);}, ข้อผิดพลาด: ฟังก์ชั่น () } /*]]>* /</script> </head> <body> ไม่: <อินพุต id = "ไม่" value = "No.1234567890" /> <br /> ปริมาณ: <อินพุต id = "ปริมาณ" ค่า = "100" /> <br /> วันที่: <อินพุต ID = "วันที่" onclick = "postdata ()" /> <br /> <ค่าอินพุต = "postjson" type = "ปุ่ม" 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"/> src = "// cdn.bootcss.com/angular.js/1.5.6/angular.min.js"></script> type =" text/javascript "> var app = angular.module ('app', []); app.controller ('MainController', ฟังก์ชั่น ($ rootscope, $ scope, $ http) {$ scope.data = {ไม่: 'No.1234567890', ปริมาณ: 100, 'วันที่': '2016-12-20'}; 'โพสต์', ข้อมูล: $ scope.data}). ความสำเร็จ (ฟังก์ชั่น (r) {$ scope.responsebody = r;}); ng-model = "data.quantity" /> <br /> วันที่: <อินพุต id = "วันที่" ng-model = "data.date" /> <br /> <ค่าอินพุต = "postjson" type = "ปุ่ม" ng-click = "postjson ()" /> <br /> <br />项目结构如下图:
一、结合 jQuery
运行 app.java 后进去“ http: // localhost: 8080/jQuery” 页面
点击“ postdata” 按钮:
jQuery 成功的调用了 Spring MVC 的后台方法“ Public @ResponseBody Map <String, Object> PostData (หมายเลขสตริง, ปริมาณ int, วันที่สตริง)”
这里,“ วันที่” 参数我使用的是สตริง类型, 而并不是วันที่类型。因为大多数情况是使用对象形式来接收 ajax 客户端的值, 所以我这里偷懒了, 就直接使用สตริง类型。如果想使用วันที่类型, 则需要使用 @initbinder 注解, 后面的篇幅中会讲到,, 在这里就不再赘述。在这里就不再赘述。在这里就不再赘述。在这里就不再赘述。在这里就不再赘述。在这里就不再赘述。在这里就不再赘述。在这里就不再赘述。,,,,,,
另外, 使用“ thymeleaf” 模板引擎在编写 js 时,“ &” 关键字要特别注意, 因为“ thymeleaf” 模板引擎使用的是 xml 语法。因此, 在在 <Script> 标签的开始 ― 结束的位置要加“/*<! [cdata [*/...../*]>*/”
例如:
<script type = "text/javascript">/*<!
否则, 运行“ thymeleaf” 模板引擎时就会出现错误“ org.xml.sax.saxparseexception: ... ”
点击“ postjson” 按钮:
jQuery 则成功调用了后台“ Public @ResponseBody Map <String, Object> Postjson (@requestbody order order สั่งซื้อ)” 方法,,
并且参数“ คำสั่งซื้อ” 中的属性或字段也能被自动赋值, 而วันที่类一样会被赋值。
注意的是: 在使用 jQuery 的 $ .ajax 方法时 contentType 参数需要使用“ แอปพลิเคชัน/json”, 而后台ฤดูใบไม้ผลิ MVC 的“ postjson” 方法中的“ คำสั่งซื้อ” 参数也需要使用 @requestbody 注解。
二、结合 Angularjs
进入“ 后进去 http: // localhost: 8080/angularjs” 页面
点击“ postjson” 按钮:
使用 Angularjs 后, 依然能调用“ Public @ResponseBody Map <String, Object> Postjson (@requestbody order สั่งซื้อ)” 方法。方法。
代码: https: //github.com/carter659/spring-boot-03.git
以上就是本文的全部内容, 希望对大家的学习有所帮助, 也希望大家多多支持武林网。