如何获取用户在输入表单中编写的文本

hvvq6cgz  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(257)

我有这个输入表:

<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>

此表格适用于模型:

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;
    }
}

此类控制器:

@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";
}

如何在不重定向的情况下更改take result的此操作url @GetMapping . 我需要把结果放进我的电脑里 @GetMapping 从数据库获取信息的方法。我怎样才能改变它?

<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>
bwitn5fc

bwitn5fc1#

如果您确实需要转到search/{text}但不想重定向,您可以通过javascript来实现,例如:
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;
};

相关问题