在Spring Thymeleaf中获取值时出现问题。

huangapple 未分类评论54阅读模式
英文:

Problem getting value in Spring Thymeleaf

问题

这是我的HTML代码:

            <td> 
                <form class="form-inline" action="/infopedidos" th:action="@{/infopedidos}" th:object="${newRider}" method="post">

                    <select> 
                        <option th:each="riders: ${TodosLosRiders}" th:value="${riders.getRider_id()}" th:text="${riders.getRemail()}">
                        </option>                       
                    </select>

                    <button type="submit" value="Submit" class="btn btn-primary">Asignar rider</button>

                </form>
            </td>

这是我的控制器代码:

@PostMapping("/infopedidos")
public String UsuariosIntervaloSubmit(Model model, @ModelAttribute riders newRider, @RequestParam(value="${riders.getRider_id()}") String param) throws ParseException {

我遇到的错误是:

Required String parameter 'riders.getRider_id()' is not present
org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'riders.getRider_id()' is not present

你能帮助我吗?谢谢!

英文:

I am facing a problem trying to get from a post the values:

Here is my HTML

            &lt;td&gt; 
                &lt;form class=&quot;form-inline&quot; action=&quot;/infopedidos&quot; th:action=&quot;@{/infopedidos}&quot; th:object=&quot;${newRider}&quot; method=&quot;post&quot;&gt;

                    &lt;select&gt; 
                        &lt;option th:each=&quot;riders: ${TodosLosRiders}&quot; th:value=&quot;${riders.getRider_id()}&quot; th:text=&quot;${riders.getRemail()}&quot;&gt;
                        &lt;/option&gt;                       
                    &lt;/select&gt;

                    &lt;button type=&quot;submit&quot; value=&quot;Submit&quot; class=&quot;btn btn-primary&quot;&gt;Asignar rider&lt;/button&gt;

                &lt;/form&gt;
            &lt;/td&gt;

And I am trying to get in a post the value "riders.getRider_id()"

Here is my controller:

@PostMapping(&quot;/infopedidos&quot;)
        public String UsuariosIntervaloSubmit(Model model, @ModelAttribute riders newRider, @RequestParam(value=&quot;$riders.getRider_id()}&quot;) String param) throws ParseException {
        

And the error that I have is the next:

Required String parameter &#39;riders.getRider_id()&#39; is not present
org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter &#39;riders.getRider_id()&#39; is not present

Can you help me guys? Thanks!

答案1

得分: 0

我建议你阅读这个链接:https://www.baeldung.com/thymeleaf-in-spring-mvc

你的表单目前只发送了你的 'newRider' 对象作为 @ModelAttribute。但是你的选择标签没有指向对象中的任何字段。这意味着你没有在表单输入中更改对象,只是发送它。如果你想编辑你的 'newRider' 对象并通过 POST 发送它,你应该在你的选择标签中包含 'th:field' 属性。

<select th:field="*{<somefield>}">

你的控制器:

@PostMapping("/infopedidos")
public String UsuariosIntervaloSubmit(Model model, @ModelAttribute riders newRider ) throws ParseException {

但是如果你想将选择选项的数据作为请求参数发送,你必须在选择标签中添加 name。

<select th:name="<paramName>">

你的控制器:

@PostMapping("/infopedidos")
public String UsuariosIntervaloSubmit(Model model, @ModelAttribute riders newRider, @RequestParam(value="<paramName>") String param) throws ParseException {

表单:

<form th:action="@{/mapping}" method="post">
    <select class="form-control" name="<paramName>">
        <option th:each="obj : ${objList}" th:value="${obj.field}" th:text="${obj.field2}"></option>
    </select>
</form>

控制器:

@PostMapping
public String func(@RequestParam("<paramName>") String param, Model model) {
    ** 这里是你的代码 **
}
英文:

I suggest you reading this : https://www.baeldung.com/thymeleaf-in-spring-mvc

Your form is currently sending only your 'newRider' object as a @ModelAttribute. But your select tag is not pointing to any field in your object. That means you are not changing the object in your form inputs. Only sending it. You should include 'th:field' attr to your select tag if you want to edit your 'newRider' object and send it via post.

&lt;select th:field=&quot;*{&lt;somefield&gt;}&quot;&gt; 

Your Controller:

 @PostMapping(&quot;/infopedidos&quot;)
    public String UsuariosIntervaloSubmit(Model model, @ModelAttribute riders newRider ) throws ParseException {

But if you want to send data of your select option as a request parameter, you have to add name to your select tag.

&lt;select th:name=&quot;&lt;paramName&gt;&quot;&gt; 

Your controller:

@PostMapping(&quot;/infopedidos&quot;)
    public String UsuariosIntervaloSubmit(Model model, @ModelAttribute riders newRider, @RequestParam(value=&quot;&lt;paramName&gt;&quot;) String param) throws ParseException {

form:

&lt;form th:action=&quot;@{/&lt;mapping&gt;}&quot; method=&quot;post&quot;&gt;
            &lt;select class=&quot;form-control&quot; name=&quot;&lt;paramName&gt;&quot;&gt;
                &lt;option th:each=&quot;obj : ${objList}&quot;
                        th:value=&quot;${obj.field}&quot; th:text=&quot;${obj.field2}&quot;&gt;&lt;/option&gt;
            &lt;/select&gt;

Controller:

@PostMapping
public String func(@RequestParam(&quot;&lt;paramName&gt;&quot;) String param, Model model) {
     ** Your Code Here **
}

huangapple
  • 本文由 发表于 2020年4月8日 18:26:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/61098485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定