从eleaf下拉列表中获取所选值

ygya80vv  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(283)

我有一个下拉列表,客户端必须选择一个值,然后它将在mysql db中持久化好的,我这样做了,但我希望当客户端选择一个值时,我得到该值,然后我执行if语句示例:
如果选择的值为:cin,则compte.setcomptenumber(25364138);就像那样
这是我的控制器:

@RequestMapping(value="/addClient")
public String addClient (Model model) {
    Client client= new Client();
    Compte compte = new Compte();
    Agence agence = new Agence();
    //Carte carte = new Carte();
    model.addAttribute("client",client);
    List<String> typepieceList = Arrays.asList("CIN","Passeport");

    return "client";

}

我的html:

<div class="p-col-12">
<label class="p1">Choisir Type de la pièce d'identité
</label> <br> <select th:field="*{typepieceClient}"
                                    class="form-control" required="required">
                                    <option value=""></option>
                                    <option th:each="p: ${typepieceList}" th:value="${p}"
                                        th:text="${p}" class="p1"></option>
                                </select>
                            </div>
8nuwlpux

8nuwlpux1#

rest控制器应该接受从窗体传递的值。
您可以使用@pathvariable注解或
如果要将整个表单提交给rest控制器,则必须将其配置为使用@modeldattribute
前任:

@RequestMapping(value="/addClient")
public String addClient(@ModelAttribute Model model)
hyrbngr7

hyrbngr72#

这里要做的是,定义了一个端点/addclient,当您从浏览器调用此端点时:
您正在创建一个名为client的模型[model.addattribute(“client”,client);]
使用此模型调用html文件并返回到浏览器。
这只是解决方案的一半。为了能够接受下拉列表中的值,您需要另一个端点来提交此表单。
对于这个新端点,您需要使用@requestmapping(value=“/addclient”)public string addclient(@modeldattribute model)等声明从html接受模型
然后从html开始,您应该能够使用form标记并将表单提交到此端点。
我建议你检查一下这篇文章https://spring.io/guides/gs/handling-form-submission/

相关问题