简体   繁体   中英

how can I take text that user write in input form thymeleaf

I have this input form:

<div class="searchBox">
            <form data-th-object="${findHotel}" method="get" data-th-action="@{/search}">
                <input class="searchBoxTextEditor" type="text" placeholder="Enter name of the hotel" data-th-field="*{name}">
                <input type="submit" value="find">
            </form>
</div>

This form for models:


public class FindHotel {
    private String name;

    public FindHotel(String name) {
        this.name = name;
    }

    public FindHotel() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

This class controller:

@GetMapping("/search")
    public String getSearchRedirect(FindHotel findHotel){
        return "redirect:/search/" + findHotel.getName();
    }

@GetMapping("/search/{text}")
public String getSearchResultPage(@PathVariable(name = "text")String text,Model model){
        List<Hotel> hotels = hotelService.findHotelByNameLike(text);
        model.addAttribute("hotels",hotels);
        return "hotels";
}

How can I change this action-url for take result without redirect @GetMapping . I need take result and put it in my @GetMapping method for take info from DB. How can I change it?

<form data-th-object="${findHotel}" method="get" data-th-action="@{/search}">
  <input class="searchBoxTextEditor" type="text" placeholder="Enter name of the hotel" data-th-field="*{name}">
  <input type="submit" value="find">
</form>

if you do need to go to search/{text} but do not want redirection you can do it by javascript for example:

HTML:

<input id="hotelName" class="searchBoxTextEditor" type="text" placeholder="Enter name of the hotel" data-th-value="${findHotel.name}">
<button id="findHotel">find</button>

JS:

document.getElementById("findHotel").onclick = function() {
  window.location.href ="/search" + document.getElementById("hotelName").value;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM