Spring boot 如何将thymeleaf中的drown选择值传递给spring引导控制器

Spring boot 如何将thymeleaf中的drown选择值传递给spring引导控制器,spring-boot,thymeleaf,Spring Boot,Thymeleaf,请帮助我处理任何工作代码。我从一周后就开始尝试,没有得到解决方案。为html选择元素指定名称属性,您可以从HttpServletRequest对象访问控制器中下拉列表的选定值,如下所示 <form role="form" id="sendAddress" th:action=@{/sendAddress} method="post"> <select class="form-control" name="nameOfCity">

请帮助我处理任何工作代码。我从一周后就开始尝试,没有得到解决方案。

为html选择元素指定名称属性,您可以从HttpServletRequest对象访问控制器中下拉列表的选定值,如下所示

  <form role="form" id="sendAddress" th:action=@{/sendAddress} method="post">
        <select class="form-control" name="nameOfCity">
            <option value="">Select City</option>
            <option value="HYD">Hyderabad</option>
            <option value="MUM">Mumbai</option>
            <option value="DEL">Delhi</option>
        </select>
    </form>



    @RequestMapping(value={"/sendAddress"},method = RequestMethod.POST)
    public String messageCenterHome(Model model,HttpSession session,HttpServletRequest request) {

        String selectedCity= request.getParameter("nameOfCity")
        //return view 
    }

选择城市
海得拉巴
孟买
德里
@RequestMapping(值={“/sendAddress”},方法=RequestMethod.POST)
公共字符串messageCenterHome(模型模型、HttpSession会话、HttpServletRequest请求){
字符串selectedCity=request.getParameter(“城市名称”)
//返回视图
}

为html select元素指定名称属性,您可以从HttpServletRequest对象访问控制器中下拉列表的选定值,如下所示

  <form role="form" id="sendAddress" th:action=@{/sendAddress} method="post">
        <select class="form-control" name="nameOfCity">
            <option value="">Select City</option>
            <option value="HYD">Hyderabad</option>
            <option value="MUM">Mumbai</option>
            <option value="DEL">Delhi</option>
        </select>
    </form>



    @RequestMapping(value={"/sendAddress"},method = RequestMethod.POST)
    public String messageCenterHome(Model model,HttpSession session,HttpServletRequest request) {

        String selectedCity= request.getParameter("nameOfCity")
        //return view 
    }

选择城市
海得拉巴
孟买
德里
@RequestMapping(值={“/sendAddress”},方法=RequestMethod.POST)
公共字符串messageCenterHome(模型模型、HttpSession会话、HttpServletRequest请求){
字符串selectedCity=request.getParameter(“城市名称”)
//返回视图
}

Java MVC框架封装了Servlet API,并使用该框架更简单的API向我们提供了通用功能

对于从请求中检索参数值这样常见的任务,Spring MVC和Boot使用
@RequestParam
注释来检索从带有选项的选择标记中选择的参数值。因此的代码根本不需要使用
HttpServletRequest
来获取参数;相反,它将只注释请求调用的控制器方法的参数:

HTML表单

<form role="form" id="sendAddress" th:action=@{/sendAddress} method="post">
    <select class="form-control" name="*nameOfCity*">
        <option value="">Select City</option>
        <option value="HYD">Hyderabad</option>
        <option value="MUM">Mumbai</option>
        <option value="DEL">Delhi</option>
    </select>
</form>
@RequestMapping(value={"/sendAddress"}, method = RequestMethod.POST)
public String messageCenterHome(**@RequestParam** String *nameOfCity*) {
    // value of nameOfCity is now value of "nameOfCity" paramter,
    // that is, the value of the option tag selected
    // return view 
}

如果您还没有尝试过,请试一试,因为我刚刚看到了这一点,而且这个问题发布已经有几个月了。

Java MVC框架封装了Servlet API,并使用该框架更简单的API向我们提供了通用功能

<form th:action="@{/search}" th:object="${searchRequest}" method="post">    
    <select th:field="*{parkType}">
        <option value="1">Test1</option>
        <option value="2">Test2</option>
    </select>           
</form>

@RequestMapping(value = "/search", method = RequestMethod.POST)
public String searchParks(@ModelAttribute(value = "searchRequest") SearchRequest searchRequest, Model model) {
   
    searchRequest.getParkType(); // to get option value

    return "listings";
}

@lombok.Data
public class SearchRequest {
   private String name;
   private String parkType;
}
对于从请求中检索参数值这样常见的任务,Spring MVC和Boot使用
@RequestParam
注释来检索从带有选项的选择标记中选择的参数值。因此的代码根本不需要使用
HttpServletRequest
来获取参数;相反,它将只注释请求调用的控制器方法的参数:

HTML表单

<form role="form" id="sendAddress" th:action=@{/sendAddress} method="post">
    <select class="form-control" name="*nameOfCity*">
        <option value="">Select City</option>
        <option value="HYD">Hyderabad</option>
        <option value="MUM">Mumbai</option>
        <option value="DEL">Delhi</option>
    </select>
</form>
@RequestMapping(value={"/sendAddress"}, method = RequestMethod.POST)
public String messageCenterHome(**@RequestParam** String *nameOfCity*) {
    // value of nameOfCity is now value of "nameOfCity" paramter,
    // that is, the value of the option tag selected
    // return view 
}
如果您还没有尝试过,请尝试一下,因为我刚刚看到了这一点,问题发布已经有几个月了。


<form th:action="@{/search}" th:object="${searchRequest}" method="post">    
    <select th:field="*{parkType}">
        <option value="1">Test1</option>
        <option value="2">Test2</option>
    </select>           
</form>

@RequestMapping(value = "/search", method = RequestMethod.POST)
public String searchParks(@ModelAttribute(value = "searchRequest") SearchRequest searchRequest, Model model) {
   
    searchRequest.getParkType(); // to get option value

    return "listings";
}

@lombok.Data
public class SearchRequest {
   private String name;
   private String parkType;
}
测试1 测试2 @RequestMapping(value=“/search”,method=RequestMethod.POST) 公共字符串searchParks(@modeldattribute(value=“searchRequest”)searchRequest searchRequest,Model Model){ searchRequest.getParkType();//获取选项值 返回“列表”; } @龙目数据 公共类搜索请求{ 私有字符串名称; 私有字符串类型; }

测试1
测试2
@RequestMapping(value=“/search”,method=RequestMethod.POST)
公共字符串searchParks(@modeldattribute(value=“searchRequest”)searchRequest searchRequest,Model Model){
searchRequest.getParkType();//获取选项值
返回“列表”;
}
@龙目数据
公共类搜索请求{
私有字符串名称;
私有字符串类型;
}

如果您不向我们展示任何不起作用的代码,我们应该如何帮助您?如果您不向我们展示任何不起作用的代码,我们应该如何帮助您?我想,这不是正确的ThimeLeaf解决方案,但它对我来说是有效的。谢谢。我想,这不是一个合适的ThimeLeaf解决方案,但它对我很有效。谢谢