Announcement Announcement Module
Collapse
No announcement yet.
Emp obj containing null address obj , how to handle in jsp Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Emp obj containing null address obj , how to handle in jsp

    <spring:bind path="employee.address.id"> when address is null , it throws an exception , what are others doing when child objects of parent object come back as null ?

    thanks

  • #2
    Initialize it in your POJO:

    Code:
    public class Employee ..&#123;
    
     private Address address = new Address&#40;&#41;;

    Comment


    • #3
      thanks for your reply , this is what I am currently doing , my only concern is that

      #1) creating unnecessary objects
      #2) in order to test if an object is truly null you must also inspect the empty address object to see if all fields of the address object are null.

      Thanks again

      Comment


      • #4
        You could create a customised constructor as well!

        Comment


        • #5
          true , this works fine when you navigate to say a form and the controller form backing object returns a new object (with child objects created as defaults in the constructor of the pojo) . However , when you use that same form to edit data and the object is retrieved from the database say with hibernate .. you still end up with the same problem .. a null object reference unless of course the object is declared as a hibernate "component" in which case you will get a blank object back or the use the constructor to create the empty object.

          Comment


          • #6
            I've solved this issues with two options:
            1- put a default value for the field in the database table, and
            2 - put that one default value in the class property too.

            Code:
            public class Employee ..&#123;
            
             private Address address = new Address&#40;&#41;;
            and
            Code:
            public class Address ..&#123;
            
             private Long id = new Long&#40;-1&#41;;

            Comment


            • #7
              thanks I see what your talking about and I appreciate your feedback, since our current domain model is somewhat frozen .. it's hard for me to incorporate the the defaults in the constructor. Is there some potentially less intrusive solution .. like when child objects that are null to suppress the bind error in the jsp. This way the domain would not have to be modified and when say an employee has no address the reference would be null .. as would expected to be and it would still bind to the jsp ?

              thanks again

              Comment


              • #8
                You could build a custom property editor for you POJO or improvise in your jsp view:

                Code:
                	<c&#58;choose>
                	    <c&#58;when test="$&#123;employee.address.id == null&#125;">
                <input type="text" size="45" name="id" value="<c&#58;out value="$&#123;employee.address.id&#125;"/>" />
                	    </c&#58;when>
                	    <c&#58;otherwise>   
                                <spring&#58;bind path="employee.address.id">
                                     ...
                		</c&#58;otherwise>	        
                    </c&#58;choose>

                Comment


                • #9
                  lol , I knew that was the next solution coming .. looks good , but there are so many cases which this could happen ... I would need to duplicate all my input tags for this conditional logic ie .. if address is not null don't bind and use this input item , else bind and use this input item

                  Comment


                  • #10
                    So, what are you waiting for to build a custom property editor?

                    This is one topic that I've not touched yet!
                    Gilberto

                    Comment


                    • #11
                      good point , if anyone knows anything about property editors maybe u can answer this . The address in broken into 3 fields on the form .. I thought that with property editor your could only bind one field ? Three fields make up the address object ... can you use a property editor for this. (property editor folks ?)

                      Comment

                      Working...
                      X