close

Filter

loading table of contents...

Content Application Developer Manual / Version 2110

Table Of Contents

4.3.6.1 Form rendering

In order to render a form with Spring Forms, several things must be done:

  1. A simple model Java bean (POJO) with properties for each form field is used as a back end and to represent the form.

    This is a simple example for such a backing bean:

    public class MyForm {
    
      private String email;
      private String emailRepeat;
    
      public String getEmail() {
        return email;
      }
    
      public void setEmail(String email) {
        this.email = email;
      }
    
      public String getEmailRepeat() {
        return emailRepeat;
      }
    
      public void setEmailRepeat(String emailRepeat) {
        this.emailRepeat = emailRepeat;
      }
    }
  2. The form backing bean must be added to the model that is rendered.

    To add the form backing bean to the model, add a method to the handler class, annotated with @ModelAttribute

    @ModelAttribute("nameOfForm")
    public MyForm createMyForm() {
      return new MyForm();
    }
  3. To render the front end, Spring provides a tag library to create HTML forms in JSPs, accessing the form bean in the model.

    This is a simple example for such a form, see Spring form tag library documentation for details on how the form taglib may be used.

    <%@ taglib prefix="form"
               uri="http://www.springframework.org/tags/form" %>
    
    <form:form method="POST"
               action="handlerUri"
               commandName="nameOfForm">
    
      <%-- render form fields --%>
      <form:input path="email"/>
      <form:input path="emailRepeat"/>
    
      <input type="submit" value="Subscribe"/>
    
    </form:form>

Using IDs for Encoding Objects in Form Fields

Under some circumstances, you will need to write down a string representation of the identity of a bean, for example "the content bean for content 22". This is typically necessary in intermediary XML documents or when you want to refer to a bean in an HTML hidden input field.

For this purpose, the CoreMedia CAE contains a generic ID facility that allows you to convert selected bean types to a string and back. The ID API basically consists of two methods #getId and #parseId in the class com.coremedia.id.IdProvider. Note that this is not an object serialization. This facility is only useful to capture an id of a stateless object that represents an external business entity, as outlined in Section 4.1.1.1, “Patterns For Content Beans”. The default implementation comes with id support for content beans and blob properties. Other bean types can be supported by writing a new implementation of com.coremedia.id.IdScheme and plugging it into the id resolver using a Customizer.

In order to encode an object id into a form field in a template, as well as to decode it back on a form submission, the CoreMedia CAE comes with a custom tag <cm:id> as well as an implementation of the java.beans.PropertyEditor interface that you can use in Spring to parse form fields back into bean references.

The following example shows how to encode the id of a bean feature into an HTML form:

<%@ taglib prefix="form"
           uri="http://www.springframework.org/tags/form" %>

<cm:id self="${feature}" var="id"/>
<form:form method="POST"
           action="handlerUri"
           commandName="nameOfForm">
  <%-- render form fields --%>
  <input name="feature" value="${id}" type="hidden" />
  <form:input path="email"/>
  <form:input path="emailRepeat"/>

  <input type="submit" value="Subscribe"/>
</form:form>

In this example, a regular <input> field was used to render the id. Because of this, the id will not be bound to the backing bean, but the value can be retrieved by the controller using the command request.getParameter("feature")

Search Results

Table Of Contents
warning

Your Internet Explorer is no longer supported.

Please use Mozilla Firefox, Google Chrome, or Microsoft Edge.