close

Filter

loading table of contents...

Content Application Developer Manual / Version 2406.1

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 macros to create HTML forms in Freemarker Templates, accessing the form bean in the model.

    This is a simple example for such a form, see Spring documentation for details on how a form view may be used.

    <form action="handlerUri" method="post">
      <label for="email">Email:</label>
        <@spring.bind path="nameOfForm.email"/>
        <@spring.formInput path="nameOfForm.email"/>
    
      <label for="emailRepeat">Repeat Email:</label>
        <@spring.bind path="nameOfForm.emailRepeat"/>
        <@spring.formInput path="nameOfForm.emailRepeat"/>
    
      <input type="submit" value="Subscribe"/>
    </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.2, “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 macro cm.getId 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:

<#assign featureId=cm.getId(feature)/>
<form action="handlerUri" method="post">
  <@spring.bind path="nameOfForm.feature"/>
  <input type="hidden"
         name="${spring.status.expression}"
         value="${featureId}"/>
  <label for="email">Email:</label>
    <@spring.bind path="nameOfForm.email"/>
    <@spring.formInput path="nameOfForm.email"/>

  <label for="emailRepeat">Repeat Email:</label>
    <@spring.bind path="nameOfForm.emailRepeat"/>
    <@spring.formInput path="nameOfForm.emailRepeat"/>

  <input type="submit" value="Subscribe"/>
</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.