Announcement Announcement Module
Collapse
No announcement yet.
setting an object as value of object attribute of a backing bean in a form Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • setting an object as value of object attribute of a backing bean in a form

    Hi, am new to the spring MVC.
    in my application, am using Hibernate.
    I have a form for submitting student 's information
    The student information will have a department as attribute(of Department data type).

    in this form, the department will be selected from a select option. the code for the form is shown below
    commandName is "student"

    Code:
    <form:form action="saveStudent.htm" commandName="student">
        <table widht="100" cellspacing="0" cellpadding="0">
            <tr>
                <th style="font-size: 12px" colspan="2" align="center" >New student : fill the details</th>            
            </tr> 
            
            <tr>
                <th align="right">name:</th>
                <td><form:input path="name" /></td>
            </tr>
    
            <tr>
                <th align="right">Department:</th>
                <td><form:select items="${depList}" itemLabel="department_Name" itemValue="" path="department" /></td>
            </tr>
            <tr >
                <th colspan="2" align="center"><input type="submit" value="Register" /></th>
            </tr>
        </table>
    </form:form>
    this is the Student class:
    Code:
    @Entity
    @Table(name="Student")
    public class Student implements Serializable {
        @Id
        @GeneratedValue(strategy= GenerationType.AUTO)
        @Column(name="ID")
        private long id;
        @Column(name="name")
        private String name;
        @ManyToOne
        private Department department; 
    }
    this is the Department class:

    Code:
    @Entity
    @Table(name="Department")
    public class Department implements Serializable {
        @Id
        @GeneratedValue(strategy= GenerationType.AUTO)
        @Column(name="ID")
        private long id;
        @Column(name="d_Name")
        private String department_Name;
        @Column(name="d_code")
        private String dept_code;
        @Column(name="d_head")
        private String dept_head;
    }
    so a list containing many department objects will be passed to the view. when I select a department out of this, I need this selected department to be set as department of the of the student object.

    please ask me if further information is needed for answering this.

  • #2
    Hi amFroz,

    this is a common problem and there is a simple solution to it:
    In the student class you need to declare the department id explicit for reference from 'itemValue'.
    Like this:
    Code:
    @Entity
    @Table(name="Student")
    public class Student implements Serializable {
        @Id
        @GeneratedValue(strategy= GenerationType.AUTO)
        @Column(name="ID")
        private long id;
        @Column(name="name")
        private String name;
        @ManyToOne
        @JoinColumn(name="department_id", insertable=false, updatable=false, nullable=false)
        private Department department; 
        @Column(name="department_id", nullable=false)
        private long departmentId;
    
    }
    This way you'll bind the department 2 times to the same column (department_id) making sure that just the department(s) id is enabled to insert or update information. Your drop down box will look like this:
    Code:
            <tr>
                <th align="right">Department:</th>
                <td><form:select items="${depList}" itemLabel="department_Name" itemValue="id" path="departmentId" /></td>
            </tr>
    provided of course your ${deplList} holds Department objects.

    Note that your question is fully covered in the this demo (use of unit and vatrate dropdowns) with the item view and data model object on github. (Sorry for all the german articles names in the SheptOrder demo [ those are from one of germans biggest grocery discounters])

    Happy coding amFroz !
    Last edited by shept; Nov 30th, 2011, 05:37 PM.

    Comment


    • #3
      Hi Andreas,
      Thanks for your sincere response.

      I will write the code which I have followed prior to your reply. and tell me which one will be better.

      I have added property dummy_dept_id for student class, and made this attribute as Transient.
      in the form, the itemValue of the department is set to this student.dummy_dept_id;
      in the server side, the controller will handover the student object to the StudentService class for making a proper student object.
      the StudentService class will have a method makeUpStudent(Student std) that will set the department field of the recieved student object with the deprtament having id equal t that of std.dummy_dept_id.
      this student object is return back to the controller and the controller will forward it to the studentDao for saving it.
      please check the code below. you will get a clear cut idea;


      Student Class:
      Code:
      @Entity
      @Table(name="Student")
      public class Student implements Serializable {
          @Id
          @GeneratedValue(strategy= GenerationType.AUTO)
          @Column(name="ID")
          private long id;
          @Column(name="name")
          private String name;
          @ManyToOne
          private Department department;
      
          @Transient
          Private long dummy_dept_id;
      }

      Student Controller:

      Code:
      package xcom.controller;
      
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.servlet.ModelAndView;
      
      
      @Controller
      public class StudentController{
          
      private StudentService studentService;
          
          @RequestMapping("save.htm")
          public ModelAndView index(Student std) {
          Student s = std;
          s = studentService.makeUpStudent(s);
          studentService.save(s);
          return new ModelAndView("studentForm");
          }
      }


      Student Service Class:
      Code:
      public class StudentService{
          
          HibernateTemplate hibernateTemplate;
      
          public void setHibernateTemplate(SessionFactory sessionFactory) {
              this.hibernateTemplate = new HibernateTemplate(sessionFactory);
          }
          
          public Student makeUpStudent(Student std) {
               DepartmentDao dd = new DepartmentDao();
               Department dept = dd.findDepartmentById(std.getDummyDeptId);
               std.setDepartment(dept);
               return std;
          }
      }

      Department Dao:

      Code:
      import java.util.List;
      import org.hibernate.SessionFactory;
      import org.springframework.orm.hibernate3.HibernateTemplate;
      
      public class DepartmentDao{
          
          HibernateTemplate hibernateTemplate;
      
          public void setHibernateTemplate(SessionFactory sessionFactory) {
              this.hibernateTemplate = new HibernateTemplate(sessionFactory);
          }
          
          public Department findDepartmentById(long id) {
              return hibernateTemplate.get(Department.class, id);
          }
      }


      Student Dao
      Code:
      import java.util.List;
      import org.hibernate.SessionFactory;
      import org.springframework.orm.hibernate3.HibernateTemplate;
      
      public class StudentDao{
          
          HibernateTemplate hibernateTemplate;
      
          public void setHibernateTemplate(SessionFactory sessionFactory) {
              this.hibernateTemplate = new HibernateTemplate(sessionFactory);
          }
          
          public Student save(Student std) {
              return hibernateTemplate.save(std);
          }
      }
      Tell me which one should I follow and is there any other option other than this two we discussed about? I mean in the sense, setting directly form the form

      Also, I will look through the your reply for my shept related doubt. Hope the example attached to that reply is using Tiles too.

      Thanks

      Comment

      Working...
      X