【问题标题】:How do i pass two th:object in a single form我如何以单一形式传递两个 th:object
【发布时间】:2021-01-25 23:39:51
【问题描述】:

我是 thymeleaf 的新手,找不到在一个 HTML 表单中传递两个 th:objects 的方法。如何传递两个不同的实体对象。在这种情况下,我的实体是 Patient 和 Doctor。我的表格如下。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet"
    href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<title>LOG IN PAGE</title>
</head>
<body>
    <div class="container">
        <form th:action="@{/loggedProfile}" th:object="${patient,doctor}"
            method="get">
            <div class="form-group">
                <label for="patientEmail">Email address</label> <input type="email"
                    class="form-control" placeholder="Enter Your email"
                    th:field="*{eMail}"> <small id="emailHelp"
                    class="form-text text-muted">We'll never share your email
                    with anyone else.</small>
            </div>
            <div class="form-group">
                <label for="patientPassword">Password</label> <input type="password"
                    class="form-control" placeholder="Enter Your Password"
                    th:field="*{password}">
            </div>
            <button type="submit" class="btn btn-primary">Login</button>
        </form>
    </div>
</body>
<div th:insert="Header-Footer/common_header_footer"></div>
</html>

这是我的控制器,它是原始的。

    @GetMapping("/logIn")
public String logIn(Model model) {
    Patient patient = new Patient();
    model.addAttribute("patient", patient);
    
    Doctor doctor = new Doctor();
    model.addAttribute("doctor", doctor);
    
    return "UI-Pages/LogIn_Page";
}

@GetMapping("/loggedProfile")
public String loggedProfile(@ModelAttribute Doctor doctor,@ModelAttribute Patient patient,Model model) {
    doctor = docRep.findByeMailAndPassword(doctor.geteMail(), doctor.getPassword());
    patient = patRep.findByeMailAndPassword(patient.geteMail(), patient.getPassword());
    model.addAttribute("doctor", doctor);
    model.addAttribute("patient", patient);
    return "Profile-Pages/Patient_Profile";
}

谢谢。

【问题讨论】:

    标签: java html rest spring-mvc thymeleaf


    【解决方案1】:

    您创建一个包含PatientDoctor 的新对象。

    public class LoginForm {
      private Patient patient;
      private Doctor doctor;
      
      // getters and setters
    }
    

    然后像这样访问字段:

    <form th:action="@{/loggedProfile}" th:object="${form}"
      th:field="*{patient.eMail}"
    
      ....
      th:field="*{doctor.password}"
    </form>
    

    等等……

    【讨论】:

      【解决方案2】:

      使用映射到表单数据中的字段的专用数据传输对象可能会更好:

      public class LoginFormData {
        private String email;
        private String password;
      }
      

      然后将PatientDoctor 实体转换为LoginFormData 并返回到您的控制器中。

      旁注:

      • 如果打算使用表单更改数据,请在表单中使用 th:method="post",并在控制器中使用 @PostMapping
      • 拥有findByeMailAndPassword 方法不是您应该拥有的。密码应该以纯文本形式存在于数据库中,尝试使用其纯文本密码查找某个用户会很奇怪。

      【讨论】:

      • 谢谢你,我从你的回答中得到了另一个想法
      猜你喜欢
      • 2017-09-28
      • 2012-07-01
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      相关资源
      最近更新 更多