軟件:鴻蒙devceo3.1,springboot項目采用IDEA
目的:
1、結合springboot后端與鴻蒙的結合運用。
2、Log日志查看console語句的信息。
3、引入
import http from '@ohos.net.http'。
4、調用springboot后端提供的鏈接發送post
5、TextInput的使用,onChange運算
devceo軟件:
\src\main\ets\pages\FormPage.ets代碼:
import http from '@ohos.net.http'
@Entry
@Component
// @Preview
struct FormPage {@State name: string = ''@State age: string = ''@State email: string = ''submitData() {const httpRequest = http.createHttp() // 創建請求實例httpRequest.request('http://localhost:8080/submitTableData',{method: http.RequestMethod.POST,header: {'Content-Type': 'application/json'},extraData: {name: this.name,age: Number(this.age),email: this.email},readTimeout: 60000}).then((res) => {console.info('請求成功,響應數據:', res.result)}).catch((err) => {console.error('請求失敗:', err)})}build() {Column({ space: 10 }) {TextInput({ placeholder: '請輸入姓名', text: this.name }).onChange(value => this.name = value)TextInput({ placeholder: '請輸入年齡', text: this.age }).onChange(value => this.age = value)TextInput({ placeholder: '請輸入郵箱', text: this.email }).onChange(value => this.email = value)Button('提交數據').onClick(() => this.submitData())}.padding(20)}
}
springboot后端:
新建項目maven的springboot:
\src\main\java\com\example\demo\controller\DataController.java
代碼如下:
package com.example.demo.controller;// DataController.javaimport lombok.Data;
import org.springframework.web.bind.annotation.*;@RestController
public class DataController {@PostMapping("/submitTableData")public String submitData(@RequestBody FormData formData) {System.out.println("收到表單數據:" + formData);return "后端已收到數據:" + formData.getName();}@Datapublic static class FormData {private String name;private int age;private String email;}
}
運行結果: