Results 1 to 8 of 8

Thread: A form which appears on every page

  1. #1
    Join Date
    Dec 2007
    Posts
    6

    Question A form which appears on every page

    Hi,

    I'm converting an existing site from Cocoon 2.1 to Spring 2.5 MVC.

    I have a form select element which appears at the top of every page to let a user change their location. How do I handle this form with Spring MVC?

    At the moment I'm writing it without using Spring's form methods, but if there's a way to do this with them I'd try that.

    I have a LocationList bean which I've inserted into every view, so I can iterate over it. Spring's EL syntax won't let me call methods, so the code I've got which adds the "selected" attribute doesn't work.

    HTML Code:
    <c:set var="currentLocation"><authz:authentication operation="locationKey"/></c:set>
    
    <select name="locationKey" onchange="form.submit();">
      <c:forEach var="location" items="${locationList.rootLocations}">
        <c:choose>
          <c:when test="${currentLocation == location}">
            <option value="${location.key}" selected="selected">${location.name}</option>
          </c:when>
          <c:otherwise>
            <option value="${location.key}">${location.name}</option>
          </c:otherwise>
        </c:choose>
      </c:forEach>
    </select>
    The above code works fine and renders a drop-down list of locations, but it doesn't select the current location because of course you can't compare strings with ==. I can't call methods in JSTL so I can't use equals().

    So what I want to know is:
    • Should I be using Spring's form tags?
    • If so, how? Bearing in mind this form appears on every page.
    • Can I replace the expression language with something that can handle calling functions?

  2. #2
    Join Date
    Jan 2006
    Posts
    28

    Default

    Your test is incorrect. You'd better use:
    Code:
    <c:when test="${currentLocation == location.key}">
            <option value="${location.key}" selected="selected">${location.name}</option>
          </c:when>
    Asserting your key property is a string. JSTL handles correctly the == for strings or number or a lot of basic types.

  3. #3
    Join Date
    Dec 2007
    Posts
    6

    Smile

    Quote Originally Posted by akram View Post
    Your test is incorrect. You'd better use:
    Gah! You're totally right. I missed that. Thanks very much! My location picker works perfectly now.

    Asserting your key property is a string. JSTL handles correctly the == for strings or number or a lot of basic types.
    I didn't realise that, quite useful to know. Thanks again.

  4. #4
    Join Date
    Dec 2006
    Posts
    311

    Default

    using the formtags would clean the code up:

    Code:
    <form:select path="searchServicerId">
         <form:option value="-1" label="Please choose...."/> 
         <form:options items="${yourItems}" itemLabel="name"  itemValue="value"/>
    </form:select>
    also you can put this in its own jsp then include it in each page so when you make changes you only change it in one place.

  5. #5
    Join Date
    Dec 2007
    Posts
    6

    Default

    Quote Originally Posted by TerpInMD View Post
    using the formtags would clean the code up:

    Code:
    <form:select path="searchServicerId">
         <form:option value="-1" label="Please choose...."/> 
         <form:options items="${yourItems}" itemLabel="name"  itemValue="value"/>
    </form:select>
    I was hoping it could be done with form:select, but what do I need to set up in order for this to work? What have you had to set up to make "searchServicerId" valid in that example? And where would I pass in the current value of the property so it would know which option to flag as selected?

    Thanks

  6. #6
    Join Date
    Dec 2006
    Posts
    311

    Default

    Ironacally I just posted this earlier:

    Code:
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    .....
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    the searchId (in your case 'location.key') is just a property in the Command class that backs the form. You could create it in a base class then extend it for each command class you need. Is this making sense?

  7. #7
    Join Date
    Dec 2007
    Posts
    6

    Default

    Quote Originally Posted by TerpInMD View Post

    the searchId (in your case 'location.key') is just a property in the Command class that backs the form. You could create it in a base class then extend it for each command class you need. Is this making sense?
    I already knew about the form tld

    It's kind of making sense. Maybe my brain is still in "rails" mode.

    This form is in the header of my general layout (I'm using the tiles2 components). So far I'm working on one Controller file, called SiteController. This controller is going to get all non-specific URLs like /home.do for example. I'm using the @Controller annotation, and @RequestMapping("/home.do") on my home action. I haven't investigated any of the controller types which specialise in form handling yet.

    If I understand you correctly, I need to create something like "BaseController" and extend that for SiteController and all my other future controller classes. BaseController will have a method like getLocationKey(), and that will be accessed by the form:select tag as long as I give it the correct attributes. Did I get that right?

  8. #8
    Join Date
    Dec 2006
    Posts
    311

    Default

    Your on the right track I think. Take a look at SimpleFormController specifically the referenceData method. That is where you will populate your option list.

    Also in your case you may extend a BaseController and BaseCommand object. The base controller will have your referenceData call to populate your location list. The base command will have your locationId that you grab once the form is submitted.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •