Q:

Adding two objects that belong to two different models with similar attribute names in one form using Thymeleaf and Spring MVC

I have a Student class with fields (firstName, lastName, mobile, and email among others) and a Guardian class with some fields haveing the same name (firstName, lastName, and email among others).
In the add student view, the user is supposed to enter the student's details and their guardian's as so for the student's:
...    
<div class="col s6">

                        <div th:object="${student}" class="form-details row">Enter team member information
                        </div>

                        <div hidden="false">
                            <label>Project</label>
                            <input th:field="*{project}" th:value="${project}"/>
                        </div>

                        <label>First Name</label>
                        <input type="text" th:field="*{firstName}"/><br/><br/>

                        <label>Last Name</label>
                        <input type="text" th:field="*{lastName}"/><br/><br/>

                        <label>Email Address</label>
                        <input type="text" name="email" th:field="*{email}"/><br/><br/>

                        <label>Mobile Number</label>
                        <input type="text" name="mobile" th:field="*{mobile}"/><br/><br/>
...

and for the guardian's:
<div th:object="${guardian}" class="form-details">Enter guardian's information
                            </div>

                            <label>First Name</label>
                            <input type="text" th:field="*{firstName}"/><br/><br/>

                            <label>Last Name</label>
                            <input type="text" th:field="*{lastName}"/><br/><br/>

                            <label>Mobile Number</label>
                            <input type="text" name="mobile" th:field="*{mobile}"/><br/><br/>

The controller is adding both objects properly to the view:
@RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addStudent(@RequestParam(value = "projectId") Integer projectId,
                                   HttpServletRequest request) {

        String lang = (String) request.getSession().getAttribute("lang");
        ModelAndView modelAndView = new ModelAndView();
        if(lang.equals("en")) {
            modelAndView.setViewName("student-form");
        }
        else {
            modelAndView.setViewName("arabic/student-form-ar");
        }

        Student student = new Student();
        student.setProject(projectService.findOneById(projectId));

        modelAndView.addObject("student", student);
        modelAndView.addObject("guardian", new Guardian());
        modelAndView.addObject("project", projectService.findOneById(projectId));

        return modelAndView;
    }

Then saving both objects:
@RequestMapping(value = "/save", method = RequestMethod.POST, params = {"next"})
    public ModelAndView saveStudent(Student student, Guardian guardian,
                                    Project project) {

        // first student
        if(studentService.findAllByProject(project).isEmpty()) // the first student
            student.setLeader(true);

        else // not the first student
            student.setLeader(false);

        guardianService.save(guardian);
        student.setGuardian(guardian);

        studentService.save(student);

        ModelAndView modelAndView = new ModelAndView("redirect:/supervisor/add?projectId="+project.getId().toString());

        return modelAndView;

    }

The values are getting concatenated and added for each object; for example:
If I enter this for student:
First Name: Student FN
Student First Name Image
And this for guardian:
First Name: Guardian FN
Guardian First Name Image
I get the following record in the database for both student and guardian tables:
Student FN,Guardian FN
Is there any way to make Thymeleaf distinguish between the two? Apart from changing the models obviously.

A:

I believe it is possible that you are not refering to the data in thymeleaf properly. Looks like when you use th:field=*{} you are doing it outside of the div where you define your th:object=${}. Try to do this inside the same div. Another thing I would try is to refer to the fields in this manner directly using the . notation: th:field=${student.firstName}.

