简体   繁体   English

使用Thymeleaf和Spring MVC以一种形式添加属于两个具有不同属性名称的两个不同模型的对象

[英]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 FN

Student First Name Image 学生名字图像

And this for guardian: First Name: Guardian FN 监护人的名字:名字:Guardian FN

Guardian First Name Image 监护人名字图片

I get the following record in the database for both student and guardian tables: Student FN,Guardian FN 我在数据库中同时获得了学生表和监护人表的以下记录:学生FN,监护人FN

Is there any way to make Thymeleaf distinguish between the two? 有什么方法可以使Thymeleaf区分两者吗? Apart from changing the models obviously. 除了明显改变模型。

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=${} . 看起来当您使用th:field=*{}您正在定义th:object=${}的div之外进行此操作。 Try to do this inside the same div. 尝试在同一div内执行此操作。 Another thing I would try is to refer to the fields in this manner directly using the . 我想尝试的另一件事是直接使用来以这种方式引用字段。 notation: th:field=${student.firstName} . 表示法: th:field=${student.firstName}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM