Announcement Announcement Module
Collapse
No announcement yet.
Value of nested property 'address' is null Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Value of nested property 'address' is null

    Once i using velocity is fine, but i try to learn using jsp and tags form, and i have some problem..
    I have two class with one to one relationship, i using hibernate as orm. this my view :
    Code:
                    
     Name
    <form:input path="person.name"/>
    <form:errors path="person.name"/>
                        
    Street
    <form:input path="person.address.street"/>
    <form:errors path="person.address.street"/>          
                          
    City                 
    <form:input path="person.address.city"/>                        
    <form:errors path="person.address.city"/>
    <input type="submit">
    if i run getById is ok.. the binding all values are showing. But if i try to post my data to execute other method like save("person") the address is always null, this my controller class :

    Code:
      @RequestMapping(method = RequestMethod.GET)
        private ModelMap displayForm(@RequestParam(value = "id", required = false) Long id) {      
            Person person;
            Address address = null;
            if (id == null) {
                person = new Person();
                address = new Address();
                person.setAdress(address);
            } else {           
                person = personDao.getById(id);
                if (person.getAddress == null) {
                    address = new Adress();
                    mahasiswa.setAddress(address);
                }
            }
            return new ModelMap(person);
        }
    
     @RequestMapping(method = RequestMethod.POST)
    private String proccessForm(@ModelAttribute("person") Person person, BindingResult resultPerson,@ModelAttribute("address") address) {
         person.setAddress(address);
           newPersonValidator().validate(person, resultPerson);    
            if (resultPerson.hasErrors()) {
                return "inputPerson";
            } else {        
               personDao.save(person);
                return "listPerson";
            }
        }
    The above code is work if i using velocity as view ,in this case
    @RequestMapping(method = RequestMethod.POST) can't catch the address values, i don't know where is my wrong, please help me...

  • #2
    Anyone could help me with this?

    Comment


    • #3
      I'm sory, maybe it's wrong post in this topic, but i dont understand how to move it. I have a success to solve my problem, but i still little bit confusing,
      I try to add in my entity class like this :
      Code:
      class Person{
      private Address address=new Address();
      }
      So the address class will be instantiate. And i remove the code(id from Person) in my view :
      Code:
      <form:form commandName="person" method="post">      
      <form:hidden id="id" path="id"/>  -->will be removed
      But i need id from address to make sure the addres saveOrUpdate correctly,
      Code:
      <form:form commandName="person" method="post">      
      <form:hidden id="address.id" path="address.id"/>
      I don't know why this is needed(id from address), but it's working, if i using velocity its i don't need id from Address, but i need id from Person,and it's working. any body could give me some explanation about this ?

      Comment

      Working...
      X