Announcement Announcement Module
Collapse
No announcement yet.
form:select tag [chose one option as selected] Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • form:select tag [chose one option as selected]

    Hi! I am doing a project for my university and I have been a while gloogling for a solution about how to select one of the options in a dropdown list, but I was not fortunate.
    - I am using Hibernate and Spring MVC.
    - I have an entity "patient" which has a field "country" of type String.

    In one of the pages I have to edit the values of the patient and I need that the dropdown list is initialized with the correct value. I know how to retrieve the data and all that,
    Which I do not know is how to translate:

    Code:
    <select>
       ........
      <option value="Greece" selected="selected">Greece</option>
      .........
    </select>
    What I have now and is working is:

    Code:
    <form:form commandName="commandPatient" method="POST" action="editPatient">
    .................
    <form:select path="country">
       <form:options items='${countries}'/>    
    </form:select>
    .................
    </form:form>
    So my question is: how do I select the value I want for the country?
    I also have declared in my controller a variable with the desired value I want:

    Code:
        protected Map referenceData(HttpServletRequest request) throws Exception {
        	Map<Object, Object> dataMap = new HashMap<Object, Object>();
        	dataMap.put("country", "Greece");
            return dataMap;
        }
    I know there is a solution using
    Code:
    <spring:bind path="...">
           <select name="${status.expression}">
               <c:forEach items="${countries}" var="c">
    ........................
             </c:forEach>
           </select>
    </spring>
    as indicated in: http://www.jroller.com/raible/entry/...ith_spring_mvc
    but I prefer to use the form:select tag
    I just wished there was a "selected" tag
    "Thank you for your help"
    Last edited by raulsan; Nov 23rd, 2009, 07:00 PM.

  • #2
    on your form backing class, you can set the country

    eg.

    public Class Form {
    private String country;

    //setter and getter methods for each member variables
    }


    when you create form backing object, you can set the value of country .. spring tags will automatically map the correct selected value from the drop down list.

    surajz

    Comment


    • #3
      [solved]

      Haha!
      Thank you surajz!

      Ok, thread [solved].

      If someone reads the post, this is what I did:

      I have a newPatient.jsp with a form with many fields, one of them is a list of countries:

      Code:
      <form:form commandName="commandPatient" method="POST" action="editPatient">
      ...................... 
         <form:select path="country">
            <form:options items='${countries}'/>
         </form:select>
      .............
      </form:form>
      newPatient.jsp has a controler called EditPatientControler.java where I override the necessary methods to get my purpose:

      Code:
      public class EditPatientControler extends SimpleFormController {
          public EditPatientControler() {
          }
      
          @Override
          protected Object formBackingObject(HttpServletRequest request) throws Exception {
          	Patient p = new Patient();
          	p.setCountry("Greece");
          	return p;
          }
      
          @Override
          protected Map referenceData(HttpServletRequest request) throws Exception {
          	Map<Object, Object> dataMap = new HashMap<Object, Object>();
          	dataMap.put("countries", Arrays.asList("Afghanistan","Aringland Islands","Albania","Algeria", .............................));
              return dataMap;
          }
      
      
          @Override
          protected void doSubmitAction(Object command) throws Exception {
              try {
                  InitialContext ic = new InitialContext();
                  final QueueConnectionFactory factory =
                            (QueueConnectionFactory) ic.lookup("jms/PatientFactory");
                  final QueueConnection connection = factory.createQueueConnection();
                  final QueueSession session = connection.createQueueSession(false,
                          QueueSession.AUTO_ACKNOWLEDGE);
                  Patient p = (Patient) command;
                  final Queue savePatientQueue = (Queue) ic.lookup("jms/SavePatient");
                  MessageProducer messageProducer = session.createProducer(savePatientQueue);
                  ObjectMessage message = session.createObjectMessage();
                  message.setObject(p);
                  messageProducer.send(message);
                  messageProducer.close();
                  connection.close();
              } catch (JMSException ex) {
                  ex.printStackTrace();
              }
          }
      }
      As you can see, I did what surajz advised me inside the method "formBackingObject(....){}"
      returning a patient with the field country set to "Greece". I don't really want Greece there, but it is just an example.
      I understand now that in the .jsp
      Code:
      <form:select path="country">
      retrieves the value of the value "country".
      I have to keep working on my project now
      "Thank you again"

      Comment

      Working...
      X