【问题标题】:How to send parameter from Angular9 html form to Springboot2 controller如何将参数从 Angular 9 html 表单发送到 Spring Boot 2 控制器
【发布时间】:2020-08-23 19:10:25
【问题描述】:

我是 Angular 9 的新手,想知道如何将参数从 HTML 表单收集到 SpringBoot 控制器。我需要传递3个变量,即fromDatetoDateinterval

HTML 表单示例:

<div class="container">
  <h1>Chart input</h1>
  <form class="form-inline">
    <div class="form-group mx-sm-3 mb-2">
      <label for="fromDate" class="sr-only">From Date</label>
      <input type="fromDate" class="form-control" id="fromDate" placeholder="From Date">
      <label for="toDate" class="sr-only">To Date</label>
      <input type="toDate" class="form-control" id="toDate" placeholder="To Date">
      <label for="intervalSelected">Interval Selected</label>
      <select class="form-control" id="intervalSelected" name="interval">
        <option value="1 min">1 min</option>
        <option value="5 mins">5 mins</option>
        <option value="15 mins">15 mins</option>
        <option value="30 mins">30 mins</option>
        <option value="1 day">1 day</option>
      </select>
    </div>
    <!--<input class="btn btn-primary" type="submit" value="submit"  aria-pressed="true">-->
    <button type="submit" class="btn btn-primary mb-2">Plot Chart</button>
  </form>
</div>

component.ts:

import { Component, NgZone, OnInit } from "@angular/core";
import * as am4core from "@amcharts/amcharts4/core";
import * as am4charts from "@amcharts/amcharts4/charts";
import am4themes_animated from "@amcharts/amcharts4/themes/animated";
import { Router, ActivatedRoute } from '@angular/router';
import * as $ from 'jquery';
import 'jqueryui';

am4core.useTheme(am4themes_animated);

@Component({
  selector: 'app-plot-chart',
  templateUrl: './plot-chart.component.html',
  styleUrls: ['./plot-chart.component.css']
})
export class PlotChartComponent implements OnInit{

service.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from "@angular/common/http";
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class EmployeeService {

  private baseUrl = 'http://localhost:8080/api/';

  constructor(private http: HttpClient) { }

  getEmployee(id: number): Observable<any> {
    return this.http.get(`${this.baseUrl}/employees/${id}`);
  }

Spring 控制器:(内容不太相关,除了接受 3 个请求参数)

    @RequestMapping(value = "candlestick", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody JsonArray getAllPlot(@RequestParam("fromDate") String fromDate, 
                                              @RequestParam("toDate") String toDate, 
                                              @RequestParam("interval") Integer interval) {
        Test app = new Test();

        Contract con = new Contract();
        con.symbol("EUR");
        con.exchange("IDEALPRO");
        con.secType("CASH");
        con.currency("USD");

        app.run(con, period, interval, sleepTime);

        JsonArray json = app.getOhlcJson();

        return json;
    }

【问题讨论】:

  • 能否添加getAllPlot函数的代码?

标签: html angular spring-boot


【解决方案1】:

reqParams URL 类似于http://localhost:8080/employees?id=?。所以我认为你必须改变 service.ts 之类的

getEmployee(id: number): Observable<any> {
return this.http.get(`${this.baseUrl}/employees?id=${id}`);
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-10-13
  • 2021-11-19
  • 2012-08-01
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 2019-02-24
  • 2019-07-22
相关资源
最近更新 更多