在上篇的基础上
准备工作 :
修改 Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.github.carter659</groupId> <artifactId>spring03</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <mame> Spring03 </name> <url> http://maven.apache.org </url> <parent> <groupd> org.springframework.boot </GroupId> <strifactid> Spring-boot-Starter-parent </artifactid> <serse> 1.4.2.release </version> <project.build.sourceencoding> utf-8 </project.build.sourceEncoding> <java.version> 1.8 </java.version> </свойства> <Depective> <Dependency> <groupId> org.springframework.boot </GroupD> <ratifactId> spring-boot-sTarTer-tthym-tthym-tthym-tthym.thym-tathileafeeafeeafeeafeeab <dependency> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-devtools </artifactid> <putainate> true </optunal> </depertive> </depertive> <buld> <blicins> <bricin> <groupid> org.spramework.boot </GroupId> <Artifactid> Spring-boot-maven-plugin </artifactid> </placin> </plugins> </build> </project>
修改 app.java
пакет com.github.carter659.spring03; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplicationpublic Class App args); }}
新建 «order.java» 类文件 :
пакет com.github.carter659.spring03; import java.util.date; public class order {public String no; дата общественности; Public int Количество;}: : 这里我直接使用 public 字段了 , Get/set 方法就不写了。
新建控制器 «Maincontroller» :
пакет com.github.carter659.spring03; import java.time.zoneid; import java.util.hashmap; import java.util.map; import org.springframework.stereotype.controller; import org.spramework.web.bindation.Annotation.getmapping; org.springframework.web.bind.annotation.postmapping; импорт org.springframework.web.bind.annotation.requestbody; import org.springframework.web.bind.annotation.Responsebody; @ControllerPublic Class MainController {@gigatePaping ("/" @ControllerPublic Class {@gigatePaping ("/" @ControllerPublic Class {@getepparing ("/") @ControllerPublic indexexexter (@gigatePAPPAPPAPPAPPING () } @GetMapping ("/jQuery") public String jQuery () {return "jQuery"; } @GetMapping ("/angularjs") public String angularjs () {return "angularjs"; } @Postmapping ("/postdata") public @responsebody map <string, object> postdata (string no, int количество, строка дата) {system.out.println ("no:" + no); System.out.println ("Количество:" + Количество); System.out.println ("Date:" + Date); Map <string, object> map = new hashmap <> (); map.put ("msg", "ok"); map.put («количество», количество); map.put ("нет", нет); map.put ("date", date); карта возврата; } @Postmapping ("/postjson") public @responsebody map <string, объект> postjson (@requestbody order order) {System.out.println ("Заказ №:" + order.no); System.out.println («Количество заказа:» + order.quantity); System.out.println ("Дата заказа:" + order.date.toinstant (). Atzone (zoneid.systemdefault ()). Tolocaldate ()); Map <string, object> map = new hashmap <> (); map.put ("msg", "ok"); 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 =" text/javascript ">/*<! [cdata [*/function postdata () {var data = 'no =' + $ (###no ').). '& date =' + $ ('#date'). val (); $ .ajax ({type: 'post', url: '/postdata', data: data, success: function (r) {console.log (r);}, ошибка: function () {alert ('error!')}}); } function postjson () {var data = {no: $ ('#no'). val (), количество: $ ('#количество'). val (), дата: $ ('#date'). val ()}; $ .ajax ({type: 'post', contentType: 'application/json', url: '/postjson', data: json.stringify (data), success: function (r) {console.log (r);}, ошибка: function () {alert ('error!')}}); } /*]]>* /< /script> < /head> <body> no: <input id = "no" value = "№1234567890" /> <br /> Количество: <input id = "Количество" Value = "100" /> <br /> Дата: <input id = "value =" 2016-20 " /> <br /> <input value =" value = "value" = "2016-20" /> <br /> <input value = "valud =" value = "value". " onclick = "postdata ()" /> <br /> <input value = "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><script type =" text/javascript "> var app = angular.module ('app', []); app.controller ('maincontroller', function ($ rootscope, $ racope, $ http) {$ scope.data = {no: '№ 1234567890', количество: 100, 'date': '2016-12-20'}; $ scope.postjson = function () {$ http ({url ':': ':' uspope. Data: $ scope.data}). Успех (function (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, 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)» 方法 ,
并且参数 «Заказ» 中的属性或字段也能被自动赋值 , дата 类一样会被赋值。
: Ether
二、结合 Angularjs
进入 «后进去 http: // localhost: 8080/angularjs» 页面
点击 «Postjson» 按钮 :
使用 angularjs 后 依然能调用 依然能调用 «public @ResponseBody Map <String, Object> postjson (@requestbody order order)» 方法。 方法。
代码 : https: //github.com/carter659/spring-boot-03.git
以上就是本文的全部内容 , 希望对大家的学习有所帮助 也希望大家多多支持武林网。