Spring MVC表格预填充或从只读输入标签获取值

时间:2015-10-27 09:08:05

标签: html forms spring-mvc thymeleaf

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

传递的类
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>

1 个答案:

答案 0 :(得分:2)

在服务器端设置

控制器

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>