Announcement Announcement Module
No announcement yet.
Selected Listbox Item question Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Selected Listbox Item question

    I have a listbox that populates upon the user entering the JSP via the referenceData method. I would like to automatically select some items based on a post from a preceding page.

    If I want to do this, would I do it in the referenceData method or formBackingObject? And what else should change? Code example please.

    protected Map referenceData(HttpServletRequest request) throws Exception {
    	Map referenceData = new HashMap();
                 //... populate the docs list ....
    	referenceData.put("docs", docs);
    	return referenceData;
    <form:select path="documents" size="20" cssStyle="width: 100%;" multiple="true" onchange="loop()">
    	<form:options items="${docs}" itemValue="docid" itemLabel="docname"  />

  • #2
    I think you'll either have to use JavaScript to select the appropriate options, or you'll need to write your own loop to create the select/option list instead of using the Spring JSP tag so that you can control which options are selected.


    • #3
      If you want to populate a form with details of a backing object first time you enter the page, you can do this in formBackingObject.

      protected Object formBackingObject(HttpServletRequest request) throws Exception {
      	ArrayList initialDocs = new ArrayList();
      	initialDocs .add(doc1);
      	initialDocs .add(doc2);
      	MyCommand cmd = (MyCommand)super.formBackingObject(request);
      	cmd.setDocuments(initialDocs );
      	return cmd;
      Last edited by fuzziman; Jan 6th, 2008, 05:58 AM. Reason: incorrect info, delete irrelevant section


      • #4
        The way I think of it is that for a form there are 2 objects:

        The object returned by referenceData, which is from the form's perspective essentially a read-only object. The referenceData object is what you fill in with stuff that goes in a drop down list, for example. The form "reads" what's in it and displays it.

        The other object is the one returned by formBackingObject; this is the object that the spring documentation refers to as "the bound object" for the spring form tags, for example. In one sense you can think of this object as write-only, where it's filled in by the form's POST request, but it's write-only in the scenario where you're not pre filling in any form fields or pre selecting anything in a drop down list. When you click the submit button the form "writes" into the object the contents of the form fields.

        But when you want to pre fill or pre select any form fields then this object is both read and written; it's read to fill in or select the form fields, then it's written when you click the submit button. You need to fill it in with the stuff that's pre selected or pre filled in the formBackingObject method.

        I'm not sure if this is entirely correct, but that's how I think of it.