Accessing UI elements and corresponding controller mapping

Rakshan

Mitglied
Hallo,
I have a UI, with 2 text boxes and a button.
1652450309105.png

The html code is basic as shown:
HTML:
    <div class="col-2">
                        <input type="text" class="form-control" id="txtFrom"
                            placeholder="start date(yyyy-mm-dd)" th:value="${dateFrom}">
                    </div>
                    <div class="col-2">
                        <input type="text" class="form-control" id="txtTo"
                            placeholder="end date(yyyy-mm-dd)" th:value="${dateTo}">
                    </div>
                </div>
                <div>
                    <br>
                </div>
                <div class="row">
                    <div class="row-2">
<!--                         <input type="button" value="Validate"> -->
                        <a th:attr="href='/formularForm?from='+${dateFrom}+'&to='+${dateTo}"
                            class="btn btn-primary btn-sml" role="button" aria-disabled="true"  >Check</a>
                    </div>
                </div>
The corresponding controller has the following code :
Java:
@GetMapping("/formularform?from={dateFrom}&to={dateTo}")
  public String validateData(@PathVariable String dateFrom, @PathVariable String dateTo, Model model) throws IOException, SQLException
  {
      
    logger.info("inside the datefrom dateto");
    return "formularform";
  }

Even if I enter the dates and press the button, on the addressbar I see that the values are null. Please tell me what I am doing wrong?
 

KonradN

Super-Moderator
Mitarbeiter
First of all: This is a german speaking forum. So you might want to switch to german or look for an english speaking forum instead.

You didn't understand how the software works. Your Controller build up a model and gives it to the the thymeleaf html generator. That generator takes the model and your template and then builds the html which will be sent to the client.

So with the following template code:
<a th:attr="href='/formularForm?from='+${dateFrom}+'&to='+${dateTo}"
thymeleaf will create an "a" html node with the href attribute that is build from the model. So the values dateFrom and dateTo will be taken from the model that the controller gave to the thymeleaf engine! This is not done on the client side by the webbrowser! (Just check the source inside your client!)

So if you want to build the URL on the client, then use javascript to set it up (not recommend in my eyes) or simply use form submission:
 

Neue Themen


Oben