Results 1 to 6 of 6

Thread: problem with SelectTag of spring form tag library?

  1. #1
    Join Date
    Apr 2008
    Posts
    3

    Default problem with SelectTag of spring form tag library?

    Hello,
    I am having a problem with displaying the current value of a command object property while using spring SelectTag. My scenerio is as follows:

    My command object is an instant of Student.class

    public class Student {
    String name;
    int studentNo;
    State state;

    public String getName() {
    return name;
    }

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

    public int getStudentNo() {
    return studentNo;
    }

    public void setStudentNo(int studentNo) {
    this.studentNo = studentNo;
    }

    public State getState() {
    return state;
    }

    public void setState(State state) {
    this.state = state;
    }
    }

    Configuration of State.java is as follows

    public class State {
    int id;
    String name;

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

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

    I am using a jsp file named student.jsp to crate and update student. The jsp file is backed by a command object of type Student.class and controlled by a spring SimpleFormController. I am also passing a list of State (named stateList) in the model, from which the user will select an state and assign it to the student. Part of my jsp page is as follows:

    <%@ taglib uri="/spring-form.tld" prefix="form" %>
    <html>
    <head>
    .......
    .......
    </head>
    <body>
    <form:form method="post">
    ......
    ......
    <form:select path="state">
    <form: option value="" label="please select" />
    <form: options items="${stateList}" itemLabel="name" itemValue="id" />
    </form:select>
    ......
    ......
    </form>
    </body>

    I have a customPropertyEditor to bind the state parameter from request to the state property of the Student object. This data binding is working perfectly well and there is no problem in creating new student. However, problem occurs when I want to update an existing student by assigning him a new sate. When I view the student.jsp page for the existing student the select tag corresponding to the state property shows the output "please select". It never shows the name of the current state of the student. Then I changed the selectTag as follows

    <form:select path="state" itemLabel="name" itemValue="id">
    <form: option value="" label="please select" />
    <form: options items="${stateList}" itemLabel="name" itemValue="id"/>
    </form:select>

    This didn't work either. Note that the problem is only about viewing the current value of the state property.
    Is it a bug of selectTag or am I missing something?

    ps: I added a space between "from:" and "option" to mean optionTag since I was having trouble with smilies.

  2. #2
    Join Date
    Jan 2007
    Posts
    5

    Default

    replace this:
    <form:select path="state">
    <form: option value="" label="please select" />
    <form: options items="${stateList}" itemLabel="name" itemValue="id" />
    </form:select>


    with this:
    <form:select path="state" items="${stateList}" />

    Cheers,
    P

  3. #3
    Join Date
    Apr 2008
    Posts
    3

    Default

    Amelin
    Thank s for your reply. However, your solution is not working either. My problem
    was not with binding data in the state property of Student object, rather the problem
    was correct value was not being displayed after the binding occurs. Suppose, there
    are two states in the statelist.
    1. Florida with id = 1, and
    2. Alabama with id =2.
    Suppose I have selected Alabama in the select tag and press some submit button in the jsp page that result in data binding in server side. Then the state property of student will be be set to Alabama. However, when I display the page to the user again. The select tag will not reflect that value. Your solution has other problems too. How can I specify an empty choice when using
    <form:select path="state" items="${stateList}" />?

  4. #4
    Join Date
    Aug 2008
    Posts
    20

    Default

    Hi,

    try to correct <form:select path="state"> to <form:select path="state.id">

    this should fix your problem.

  5. #5
    Join Date
    Apr 2008
    Posts
    18

    Default

    Hey guys

    Could you post your controller to see what id, state, stateList are related to?
    i trying to figure out how to use the form tag library, but no one talks about the controller side of the problem.

    Can you help me on this one?

    thanx

  6. #6
    Join Date
    Oct 2008
    Location
    Delhi, India
    Posts
    163

    Default

    I have the exact same problem as the OP. During the edit the form select tag does not bind to the current selection. Any ideas?

Posting Permissions

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