【问题标题】:Spring MVC form prepopulate or get value from readonly input tagSpring MVC 表单预填充或从只读输入标记获取值
【发布时间】:2016-01-26 14:41:03
【问题描述】:

我有一个包含所有字段的表单,其中少一个,由用户填写。这是通过表单传递的类

public class CarForm {

    private String id;
    private Integer initialKm;
    private String carChassis;
    private String note;
    private String carType;
    private Integer fleet;

只有一个字段,fleet,我想在传递表单之前设置,或者更好的是,从 HTML 设置它。例如:

<div class="form-group">
   <label>Fleet application</label> <input type="text"
      class="form-control" th:field="*{fleet}"    
      th:placeholder="${fleetApplication}" readonly="readonly">
</div>

所以我想显示${fleetApplication.application} 并将此值设置为fleet,或者,如果可能,设置另一个值${fleetApplication.idFleet}。 是否有可能是这些解决方案之一?谢谢

更新:正如@Faraj Farook 所建议的,我这样解决了:

<div class="form-group">
    <label>Fleet application</label>
    <!-- Show fleet application -->
    <input class="form-control" type="text" th:value="${fleetApplication.application}" readonly="readonly" />
  <!-- Save into fleet the value of idFleet -->
   <input type="hidden" name="fleet" th:value="${fleetApplication.idFleet}" />
</div>

【问题讨论】:

    标签: html forms spring-mvc thymeleaf


    【解决方案1】:

    在服务器端设置

    控制器

    String action(Model model){
       CarForm carForm =  new CarForm();
       carForm.setFleet(<some value>);
       model.addAttribute("obj", carForm);
       return view.html;
    }
    

    view.html

    <div class="form-group">
        <label>Fleet application</label> 
        <input type="text" th:field="*{fleet}" readonly/>
    </div>
    

    在客户端设置

    view.html

    <div class="form-group">
        <label>Fleet application</label> 
        <input type="text" readonly th:value="${someValueVariable}"/>
        <input type="hidden" name="fleet" th:value="${someValueVariable}"/>
    </div>
    

    【讨论】:

    • 与客户端它不显示变量的值和舰队的返回值为空。如果我使用 th:text 它会显示值,但开箱即用并返回 null 我已经尝试过的服务器端方法,它为车队值返回 null
    • 使用 th:value 而不是 th:placeholder.. 顺便说一句,我认为这是因为它会被 th:field 中的值覆盖。如上所述使用它。我稍微改变了答案
    猜你喜欢
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多