Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-6840

form:checkbox tag creates invalid HTML id when bound to indexed property

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 3.0 GA
    • Fix Version/s: 3.0.1
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      When the tag form:checkbox is bound to an indexed property, it creates an HTML id which contains the characters [ and ]

      • These characters are not allowed in an id.
      • The form:label tag that is bound tot the same indexed property doesn't contain [ and ] in the for attribute
        and therefore is not correctly coupled to the corresponding checkbox.

      A checkbox generated in the example:
      <input id="countries[Belgium]1" name="countries[Belgium]" type="checkbox" value="true" checked="checked"/>

      The corresponding label:
      <label for="countriesBelgium">Belgium</label>

      The class that acts as command object:
      --------------------------------------
      package org.example.entities;

      import java.util.Collections;
      import java.util.LinkedHashMap;
      import java.util.Map;

      public class Continent {
      // key = name of country
      // Boolean= bordered by the sea
      private Map<String, Boolean> countries = new LinkedHashMap<String, Boolean>();

      public void setCountries(Map<String, Boolean> countries)

      { this.countries = countries; }

      public void addCountry(String name, boolean borderedByTheSea)

      { countries.put(name, borderedByTheSea); }

      public Map<String, Boolean> getCountries()

      { return Collections.unmodifiableMap(countries); }

      }

      The Controller class:
      ---------------------
      package org.example.web;

      import org.example.entities.Continent;
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.web.bind.annotation.ModelAttribute;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;

      @Controller
      public class ContinentController {
      @RequestMapping(value = "/continent.htm", method = RequestMethod.GET)
      public String continentForm(Model model)

      { Continent continent=new Continent(); continent.addCountry("Belgium", true); continent.addCountry("Switzerland", false); model.addAttribute(continent); return "continent.jsp"; }

      @RequestMapping(value = "/person.htm", method = RequestMethod.POST)
      public void personForm(@ModelAttribute Continent continent) {
      }
      }

      continent.jsp
      -------------
      <?xml version="1.0" encoding="UTF-8"?>
      <%@page contentType="text/html" pageEncoding="UTF-8" session="false"%>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
      <%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">
      <head>
      <title>Person example</title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      </head>
      <body>
      <form:form commandName="continent" method="get">
      <c:forEach items="${continent.countries}" var="entry">
      <div>
      <form:checkbox path="countries[${entry.key}]" />
      <form:label path="countries[${entry.key}]">${entry.key}</form:label>
      </div>
      </c:forEach>
      <div><input type="submit" /></div>
      </form:form>
      </body>
      </html>

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                juergen.hoeller Juergen Hoeller
                Reporter:
                desmethans Hans Desmet
                Last updater:
                Trevor Marshall
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Days since last comment:
                  8 years, 15 weeks, 1 day ago