Formulare mit einem Bean verknüpfen

Command object is the name Spring MVC gives to form-backing beans, this is, to objects that model a form’s fields and provide getter and setter methods that will be used by the framework for establishing and obtaining the values input by the user at the browser side. Thymeleaf requires you to specify the command object by using a th:object attribute in your

tag:

<form action="#" th:action="@{/seedstartermng}" th:object="${seedStarter}" method="post">
  ...
</form>

Formularinput

<input type="text" th:field="*{datePlanted}" />

th:field will also apply the registered Spring Conversion Service, including the DateFormatter we saw before (even if the field expression is not double-bracketed). Thanks to this, the date will be shown correctly formatted.

Values for th:field attributes must be selection expressions ( *{...} ), which makes sense given the fact that they will be evaluated on the form-backing bean and not on the context variables (or model attributes in Spring MVC jargon).

Note that th:field also understands the new types of element introduced by HTML5 like , , etc., effectively adding complete HTML5 support to Spring MVC

Checkbox Fields

<ul>
  <li th:each="feat : ${allFeatures}">
    <input type="checkbox" th:field="*{features}" th:value="${feat}" />
    <label th:for="${#ids.prev('features')}" th:text="#{${'seedstarter.feature.' + feat}}">Heating</label>
  </li>
</ul>

Why do we need this dynamic generation of an id attribute for this field? -> to ensure that each of the checkbox inputs for the same property has a different id value

Note that we’ve added a th:value attribute this time, because the features field is not a boolean like covered was, but instead is an array of values. Let’s see the HTML output generated by this code:

<ul>
  <li>
    <input id="features1" name="features" type="checkbox" value="SEEDSTARTER_SPECIFIC_SUBSTRATE" />
    <input name="_features" type="hidden" value="on" />
    <label for="features1">Seed starter-specific substrate</label>
   </li>
   <li>
    <input id="features2" name="features" type="checkbox" value="FERTILIZER" />
    <input name="_features" type="hidden" value="on" />
    <label for="features2">Fertilizer used</label>
   </li>
   <li>
    <input id="features3" name="features" type="checkbox" value="PH_CORRECTOR" />
    <input name="_features" type="hidden" value="on" />
    <label for="features3">PH Corrector used</label>
  </li>
</ul>

Radio Button Fields

<ul>
  <li th:each="ty : ${allTypes}">
    <input type="radio" th:field="*{type}" th:value="${ty}" />
    <label th:for="${#ids.prev('type')}" th:text="#{${'seedstarter.type.' + ty}}">Wireframe</label>
  </li>
</ul>
<select th:field="*{type}">
  <option th:each="type : ${allTypes}" th:value="${type}" th:text="#{${'seedstarter.type.' + type}}">Wireframe</option>
</select>

Java-Methoden aufrufen

Java Code:

@RequestMapping(value="/seedstartermng", params={"removeRow"})
public String removeRow(final SeedStarter seedStarter, final BindingResult bindingResult,final HttpServletRequest req) {
  final Integer rowId = Integer.valueOf(req.getParameter("removeRow"));
  seedStarter.getRows().remove(rowId.intValue());
  return "seedstartermng";
}

HTML Code:

<button type="submit" name="addRow" th:text="#{seedstarter.row.add}">Add row</button>

results matching ""

    No results matching ""