获取一个按钮的端点,该按钮从2个文本字段中获取输入

发布于 2025-01-28 13:00:43 字数 1329 浏览 1 评论 0原文

我有一个基本的HTML页面,上面有2个文本框和一个按钮。我需要在文本框中的条目

响应

<div class="row">
  <div class="col-2">
    <input type="text" class="form-control" id="txtFrom" placeholder="start date(yyyy-mm-dd)"
      required="required">
  </div>
  <div class="col-2">
    <input type="text" class="form-control" id="txtTo" placeholder="end date(yyyy-mm-dd)" required="required">
  </div>
</div>
<div>
  <br>
</div>
<div class="row">
  <div class="row-2">
    <!--                        <input type="button" value="Validate"> -->
    <a th:attr="href='/formular" class="btn btn-primary btn-sml" role="button" aria-disabled="true">Check</a>
  </div>
</div>

然后能够建立相同的控制器(用于获取图表的注释) 我希望它是@getMapping(“/formular?from = {datefrom}&amp; to = {dateTo}”)

code:

 @GetMapping("/formular?from={dateFrom}&to={dateTo}")
 public String formData(@PathVariable String dateFrom, @PathVariable String dateTo, Model model) throws IOException, SQLException {

    logger.info("inside the datefrom dateto");
    return "formular";
 }

但这无济于事。输入日期并单击按钮后,它不会打印日志消息。

有人可以建议吗?

I have a basic html page with 2 textboxes and a button. I need to make the button respond after the entries in the textbox

View

The body of the html looks like this :

<div class="row">
  <div class="col-2">
    <input type="text" class="form-control" id="txtFrom" placeholder="start date(yyyy-mm-dd)"
      required="required">
  </div>
  <div class="col-2">
    <input type="text" class="form-control" id="txtTo" placeholder="end date(yyyy-mm-dd)" required="required">
  </div>
</div>
<div>
  <br>
</div>
<div class="row">
  <div class="row-2">
    <!--                        <input type="button" value="Validate"> -->
    <a th:attr="href='/formular" class="btn btn-primary btn-sml" role="button" aria-disabled="true">Check</a>
  </div>
</div>

I am not able to establish a controller for the same (for the GetMapping annotation)
I expect it to be @GetMapping("/formular?from={dateFrom}&to={dateTo}")

Code:

 @GetMapping("/formular?from={dateFrom}&to={dateTo}")
 public String formData(@PathVariable String dateFrom, @PathVariable String dateTo, Model model) throws IOException, SQLException {

    logger.info("inside the datefrom dateto");
    return "formular";
 }

But that does not help. It does not print the log message after entering the dates and clicking the button.

Could anyone suggest?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇女薄笑 2025-02-04 13:00:43

您缺少name属性(提交表单时,这是URL中放置的内容)。您的表单应该看起来像这样(省略了额外的属性):

<form method="GET" th:action="@{/formular}">
  <input type="text" name="from" />
  <input type="text" name="to" />
  <input type="submit" />
</form>

要在控制器中获取变量,我认为您不应该使用@pathvariables。相反,只有常规@requestParam s。

@GetMapping("/formular")
public String formular(@RequestParam("from") String from, @RequestParam("to") String to) {
  System.out.println(from);
  System.out.println(to);
  return "formular";
}

You're missing the name attribute (which is what is put in the url when you submit the form). Your form should look something like this (extra attributes omitted):

<form method="GET" th:action="@{/formular}">
  <input type="text" name="from" />
  <input type="text" name="to" />
  <input type="submit" />
</form>

And to get the variables in your controllers, I don't think you should be using @PathVariables. Rather, just regular @RequestParams.

@GetMapping("/formular")
public String formular(@RequestParam("from") String from, @RequestParam("to") String to) {
  System.out.println(from);
  System.out.println(to);
  return "formular";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文