Announcement Announcement Module
Collapse
No announcement yet.
Will getAsText() in PropertyEditor bind to multiple HTML fields? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Will getAsText() in PropertyEditor bind to multiple HTML fields?

    Hi guys!

    I am not sure if we solve below problem in the way I did.. so please correct me on my implementation if this is not the spring way of solving the issue:

    I have a Student Detail form which has got two <input type="text"> fields for student name and class type.

    The command object for this form is "DetailForm" with "Student" class as an attribute. Below are the classes:

    class DetailForm{

    Student student;
    //Other member variables goes here;
    }

    class Student{
    private String name;
    private String classType;

    //Getter and setters for name and classType
    }

    The name of the two fields (i.e. name and classType) in the form has been set to "student" so that I can get a single value passed to the setAsText() method of my Property Editor seperated by comma (i.e. name,classType)

    Below if the chunk of form in JSP:

    <spring:bind path="detailform.student.name">
    <input type="text" name="student" value="<c:out value="${status.value}"/>">
    </spring:bind>

    <spring:bind path="detailform.student.classType">
    <input type="text" name="student" value="<c:out value="${status.value}"/>">
    </spring:bind>


    To transform the incoming student name and classType value from the Detail Form to an instance of Student I have created a Property Editor as shown below:

    public class StudentEditor extends PropertyEditorSupport
    {

    // Constructors

    public StudentEditor ()
    {
    super();
    }

    // Methods

    /**
    * Converts string representation of student to Student object
    */
    public void setAsText(String value) throws IllegalArgumentException
    {

    Student student=new Student();
    StringTokenizer tk=new StringTokenizer(value,",");
    name=tk.nextToken();
    class=tk.nextToken();

    student.setName(name);
    student.setClassType(class);

    setValue(student);
    }

    /**
    * Converts Student to Stringified version.
    */
    public String getAsText()
    {
    //What to be done here if the return value should populated in two different fields ??

    //Shall we return a command seperated value for name and classtype i.e."name,classType" as passed by framework in setAsText() ??
    }

    }

    My Question:

    What should I do if I want to get the return value from getAsText() to populate the two HTML fields i.e. name and class type on JSP? Should we return "name,classType" as the return value? Will the framework map it to the same in the above <spring:bind> JSP chunk??

    Let me know if anyone has solved similar kind of problem in past...

    Thx guys!
    A

  • #2
    You don't need a PropertyEditor in this case at all.

    Spring MVC will simply bind the two seperate string values in the domain object (name and classType) to the two fields on the form, and then back from the form to the domain object on a submit. That all comes for free... since they're only string values and Spring knows how to bind them (i.e. String to a String is a pretty straight-forward converstion! ).

    Note though, you have an error in your jsp... it should look like:

    Code:
    <spring:bind path="detailform.student.name">
    <input type="text" name="<c:out value="${status.expression}" />"
      value="<c:out value="${status.value}"/>">
    </spring:bind>
    
    <spring:bind path="detailform.student.classType">
    <input type="text" name="<c:out value="${status.expression}" />"
      value="<c:out value="${status.value}"/>">
    </spring:bind>
    Note, ${status.expression} gives you the proper name so that spring can bind the value appropriately. It's based on the path in the bind tag. The key idea here again is that you're not binding to the student object, but to its properties. Name gets bound to Student.name, not to Student.

    Your paths might also be messed up... I don't know what your commandClass is. If it's a Student, then it should be "student.name" and "student.classType", assuming your command name is "student" (it's "command" by default).

    If there's a one-to-one mapping between form inputs and domain object properties, and they're all simple types, you generally don't need to use properties editors. If you were binding a java.util.Date, or some custom type, then you would of course.

    Another point to make is that in most cases, you bind a single domain object property to a single form element. There are exceptions, but it's rare.

    Cheers,
    Christian

    Comment


    • #3
      Thanks Nelson....I agree with your suggestion. I realised that my example was simple enough than what is required in my actual scenario.Let me add add to the Student example.

      Let's say I don't we don't have Student name and class type field in the HTML Detail Form. Instead I have a drop down which contains student id's. And I have a StudentFactory class which returns an instance of Student if we pass in a student id and NULL if the student id does not exist.

      Below is skeleton structure of the Factory class:

      StudentFactory {

      private DataStore dataStore;

      public Student getStudentInstance(String studentId){
      Student toReturn=dataStore.lookup(studentId);
      return toReturn;
      }

      public void setDataStore(DataStore dataStore){
      this.dataStore=dataStore;
      }
      }

      Now the requirement is that when a user selects a particular student id from the drop down in the HTML form and do a sub mit. the corresponding id should be fetched using the factory and the returned Student instance should be set in the student attribute of DetailForm.

      We could use <spring:bind> as mentioned in your response and we could have manipulated the attributes of the Student attribute in the DetailForm.

      That's why i have used the PropertyEditor where in the setAsText() method do a lookup from factory and do a setValue() for the student class.

      I believe that now the example is more specific!

      Now consider a situation where I have four drop down present on the Detail Form and a Factory class use the four attributes on the Form and do a lookup to return a Student class which I want to set in the DetailForm command object. What should be done in this scenario?

      Comment

      Working...
      X