Thymeleaf - 多选形式

时间:2017-05-13 12:45:51

标签: java html twitter-bootstrap thymeleaf bootstrap-select

我使用Java,Thymeleafm,Bootstrap和bootstrap-select。 我的回购是https://github.com/realsony/thymeleaf

我的控制器:

  @RequestMapping(value = "/start", method = RequestMethod.GET)
//@ResponseBody //can be deleted due to thymeleaf
public String start() {
    return "start";
}

@RequestMapping(value = "/start", method = RequestMethod.POST)
public String startPost(HttpServletRequest request, Model model) {
    String productA = request.getParameter("Products");
    System.out.println("product: " + productA);
    return "start";
}

那是我的HTML:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head th:replace="template :: head">
</head>
<body>
<div th:replace="template :: body"></div>
<div class="container">
    <form method="post" action="start.html">
        <h2>Chose what you want to trade</h2>
        <label>
            <select th:name="Products" class="selectpicker" multiple="multiple">
                <option th:value="Glas">Glas</option>
                <option th:value="Table">Table</option>
                <option th:value="Fork">Fork</option>
            </select>
        </label>
        <button th:type="submit" class="btn btn-lg btn-info">Send</button>
    </form>
</div>
</body>
</html>

我的问题

实际上用户可以在这里选择1-4种产品。 一旦他点击一个产品,它应该保存在控制器中。 但它没有用。在片刻,参考“产品”只提供一个选择而不是更多。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

首先在Controller中编写两个方法。

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save (HttpServletRequest request, Model model) {
//save code goes here
}

@RequestMapping(value = "/delete", method = RequestMethod.POST)
public String delete(HttpServletRequest request, Model model) {
//delete code goes here
}

然后选择发送ajax请求到/ save 并取消选择发送ajax请求到/删除