Announcement Announcement Module
Collapse
No announcement yet.
form colon option and form colon options selected Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • form colon option and form colon options selected

    I don't understand what they mean in the spring docs for form colon option and form colon options where they say "Sets 'selected' as appropriate based on bound value."

    The example given is
    Code:
    <tr>
        <td>Skills:</td>
        <td><form:select path="skills" items="${skills}"/></td>
    </tr>
    Which turns into the html
    Code:
    <tr>
        <td>Skills:</td>
        <td>
            <select name="skills">
                <option value="Potions">Potions</option>
                <option value="Herbology" selected="true">Herbology</option>
                <option value="Quidditch">Quidditch</option>
            </select>
        </td>
    </tr>
    When I read the sentence "Sets 'selected' as appropriate based on bound value." that doesn't mean anything to me as to why Herbology has selected=true.

    Could someone please expand on it?

    If I'm passing to my view a List of String, can I specify that one of them is selected with the form colon select or form colon option/options tags?

    Thanks

  • #2
    Spring compares the values in the option list to the actual value set on the object. If you have a bean with a property skill set to "Herbology" and pass this list of possible options to from:select, Spring adds the selected attribute correctly.

    HTH,

    Joerg

    Comment


    • #3
      Sorry, I'm still not understanding it. I should also have used their other example:
      Code:
      <tr>
          <td>Country:</td>
          <td>
              <form:select path="country">
                  <form:option value="-" label="--Please Select"/>
                  <form:options items="${countryList}" itemValue="code" itemLabel="name"/>
              </form:select>
          </td>
      </tr>
      If I remember correctly what I read here on the forums previously, in my referenceData I'd need to do a
      Code:
      map.put("countryList", countries);
      where countries is a List of Country objects, and the Country class has getCode() and getName() methods. So you're saying that I also need to have a getCountry() method, which is what specifies what is selected? Is the name of the method, getCountry() in this case (if I'm guessing correctly), determined by the path= part of the form colon select tag?

      Comment


      • #4
        Originally posted by Jörg Heinicke View Post
        Spring compares the values in the option list to the actual value set on the object. If you have a bean with a property skill set to "Herbology" and pass this list of possible options to from:select, Spring adds the selected attribute correctly.

        HTH,

        Joerg
        What do you mean by "the actual value set on the object"? Which object? Which value?

        Comment


        • #5
          Ok, I think I've figured it out. In my opinion the documentation is lacking here; I wouldn't have been so lost if someone had simply said something like "use the formBackingObject() method to set up the object that's bound to the form, and in formBackingObject set the country field to the country that should be pre-selected."

          Comment


          • #6
            Originally posted by lumpynose View Post
            in my referenceData I'd need to do a
            Code:
            map.put("countryList", countries);
            where countries is a List of Country objects, and the Country class has getCode() and getName() methods. So you're saying that I also need to have a getCountry() method, which is what specifies what is selected? Is the name of the method, getCountry() in this case (if I'm guessing correctly), determined by the path= part of the form colon select tag?
            Everything is correct if getCountry() only returns a string which is used in the countries list as well. If you switch to objects instead of plain strings you need a PropertyEditor for converting the objects to strings and back, but you no longer specify itemValue attribute.

            Joerg

            Comment


            • #7
              Let's just ignore that post since I was completely confused when I wrote it.

              Comment


              • #8
                Originally posted by Jörg Heinicke View Post
                Spring compares the values in the option list to the actual value set on the object. If you have a bean with a property skill set to "Herbology" and pass this list of possible options to from:select, Spring adds the selected attribute correctly.

                HTH,

                Joerg
                Could you, please, show it using a code example? I’m still quite confused.

                Comment


                • #9
                  What I receive on my POST-handler is not bound in the expected way which is what I’m confusing about.

                  @ModelAttribute
                  public CountryCmd setup(HttpServletRequest request, Model model)
                  {
                  model.addAttribute("countryList", createCountryList());
                  // set US as default selection
                  return new CountryCmdCmd().setCountry(new Country().setCode("US").setName("United States"));
                  }

                  @RequestMapping(method = RequestMethod.GET)
                  public String start(HttpServletRequest request, Model model)
                  {
                  return "countryView";
                  }

                  @RequestMapping(method = RequestMethod.POST)
                  public String submit(HttpServletRequest request, CountryCmd cmd)
                  {

                  /*
                  * After selecting a different country, i.e. UK and submitting the form
                  * I was expecting cmd.getCountry().getCode().equals("UK") == true
                  * but the country yield in cmd is still US
                  *
                  */
                  return "newView";
                  }

                  private List<Country> createCountryList() { .... }

                  Comment


                  • #10
                    Hi,

                    I have prob in setting the (selected)value for a user defined type in jsp field for a select option. Please see the snippet and let me know where am I going wrong.

                    ##############
                    Country .java
                    ##############

                    public class Country {

                    private String code;
                    private String name;

                    public Country(String code, String name) {
                    this.code = code;
                    this.name = name;

                    }

                    public String getCode() {
                    return code;
                    }

                    public void setCode(String code) {
                    this.code = code;
                    }

                    public String getName() {
                    return name;
                    }

                    public void setName(String name) {
                    this.name = name;
                    }

                    }

                    ##############
                    contact.jsp
                    ##############

                    <f:select path="countryLived" multiple="no">
                    <f:options items="${country}" itemValue="name" itemLabel="code" />
                    </f:select>

                    ##############
                    //Custom property editor.
                    ##############

                    public class CountryNameEditor extends PropertyEditorSupport {

                    public void setAsText(String code) {
                    System.out.println("Set the country to the form property: " + code);
                    if (code.equals("IND")) {
                    setValue(new Country("US", "USA"));
                    } else {
                    setValue(new Country("AUS", "Australia"));
                    }
                    }

                    }

                    ##############
                    // Controller
                    ##############
                    @ModelAttribute(value = "country")
                    public Country[] populateCountry() {
                    Country[] country = { new Country("IND", "INDIA"),
                    new Country("US", "USA") };
                    return country;
                    }

                    @InitBinder()
                    public void initBinder(WebDataBinder dataBinder) {
                    System.out.println("Init Binder...!");
                    dataBinder.registerCustomEditor(
                    com.chaitanya.spring3.form.Country.class, "countryLived",
                    new CountryNameEditor());
                    }

                    @RequestMapping(value = "/submit")
                    ModelAndView formContactSubmit(
                    @ModelAttribute(value = "command") Contact contact) {
                    System.out.println("Contact Submitted...");
                    contact.setFirstName("Mr ".concat(contact.getFirstName()));
                    contact.setLastName(contact.getLastName().toUpperC ase());
                    contact.setPhoneInUse(contact.getPhoneInUse());

                    Country selectedC = contact.getCountryLived();
                    System.out.println(selectedC + " lived. ");

                    contact.setCountryLived(selectedC);

                    ModelAndView modelAndView = new ModelAndView("contact", "command",
                    contact);
                    return modelAndView;

                    }

                    ##############
                    Problem statement:
                    ##############

                    Though Im setting the "contact.setCountryLived(selectedC)" for the user opted value the selection doesn't happen. The other text fields get populated perfectly. In console it shows a toString value for the country bean. Im sure im missing something more..here

                    Comment


                    • #11
                      I could manage to figureout the problem..!

                      #############
                      CountryNameEditor
                      #############

                      public class CountryNameEditor extends PropertyEditorSupport {

                      public void setAsText(String code) {
                      System.out.println("-->Set the country" + code);
                      if (code.equals("US")) {
                      setValue(new Country("US", "USA"));
                      } else {
                      setValue(new Country("AUS", "AUSTRALIA"));
                      }
                      }


                      @Override
                      public String getAsText() {
                      System.out.println("GET the country");
                      Country s = (Country) getValue();
                      if (s == null) {
                      return null;
                      } else {
                      return s.getCode().toString();
                      }
                      }

                      }


                      Comment


                      • #12
                        I could manage to figureout the problem..!

                        #############
                        jsp change:
                        #############

                        <f:select path="countryLived" multiple="no">
                        <f:options items="${country}" itemValue="code" itemLabel="name" />

                        #############
                        controller changes in the method:
                        #############

                        @RequestMapping(value = "/submit")
                        ModelAndView formContactSubmit(
                        @ModelAttribute(value = "command") Contact contact) {
                        System.out.println("Contact Submitted...");
                        contact.setFirstName("Mr ".concat(contact.getFirstName()));
                        contact.setLastName(contact.getLastName().toUpperC ase());
                        contact.setPhoneInUse(contact.getPhoneInUse());

                        Country conLived = contact.getCountryLived();
                        System.out.println(conLived.getCode() +" -> lived..");

                        contact.setCountryLived(conLived);
                        ModelAndView modelAndView = new ModelAndView("contact", "command",
                        contact);
                        return modelAndView;
                        }
                        Last edited by know1stranger; Oct 3rd, 2010, 12:59 PM. Reason: These fixed the problem.. Happy Spring :)

                        Comment

                        Working...
                        X