无法将表单保存到spring Boot thymeleaf应用程序中的ysql数据库

niwlg2el  于 2023-02-16  发布在  Spring
关注(0)|答案(1)|浏览(131)

我用spring Boot 和thymeleaf构建了一个简单的CRUB,但是表单在提交时不保存值到数据库。
@控制器公共类ClientController {私有静态最终记录器LOG = LoggerFactory.getLogger(ClientController.class);

@Autowired
private ClientRepository clientRepository;

@GetMapping("/patients")
public String getAllClients(Model model) {
    List<Client> clients = clientRepository.findAll();
    model.addAttribute("clients", clients);
    return "patient/list-patients";
}

@GetMapping("/add-patient")
public String addClient(Model model) {
    model.addAttribute("client", new Client());
    return "patient/add-patient";
}

@PostMapping("/add-patient")
public String addClient(@Valid @ModelAttribute Client client, BindingResult bindingResult) {
    LOG.info("Saving client: {}", client);
    if (bindingResult.hasErrors()) {
    return "patient/add-patient";
    }
    clientRepository.save(client);
    return "redirect:/add-patient";
}

@GetMapping("/edit/{id}")
public String editClient(@PathVariable("id") Long id, Model model) {
    model.addAttribute("client", clientRepository.findById(id).orElse(null));
    return "patient-edit";
}


    @GetMapping("/delete/{id}")
    public String deleteClient(@PathVariable("id") Long id) {
        clientRepository.deleteById(id);
        return "redirect:/patients";
    }
}

该模型具有以下代码:

@Entity
@Data
public class Client {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String patientId;
    
    @Size(min=2, max=30)
    private String firstName;

    @Size(min=2, max=30)
    private String lastName;
    private String otherName;
    private String gender;
    private String email;
    private Date dateOfBirth;
    private String ageGroup;
    private String phoneNumber;
    private String address;
    
//    Identity
    private String cardType;
    private String cardNumber;
    private String iDCardNumber;
    
    
    //getters and setters
}

最后,表格如下:

< div >
    <
    form action = "#"
th: action = "@{/add-patient}"
method = "post" >
    <
    p >
    <
    label
for = "firstName" > First Name: < /label> <
    input type = "text"
id = "firstName"
name = "firstName" / >
    <
    /p> <
    p >
    <
    label
for = "lastName" > Last Name: < /label> <
    input type = "text"
id = "lastName"
name = "lastName" / >
    <
    /p> <
    p >
    <
    label
for = "otherName" > Other Name(s): < /label> <
    input type = "text"
id = "otherName"
name = "otherName" / >
    <
    /p> <
    p >
    <
    label
for = "dateOfBirth" > Date of Birth: < /label> <
    input type = "date"
id = "dateOfBirth"
name = "dateOfBirth" / >
    <
    /p> <
    p >
    <
    label
for = "address" > Address: < /label> <
    input type = "text"
id = "address"
name = "address" / >
    <
    /p> <
    p >
    <
    label
for = "email" > Email: < /label> <
    input type = "email"
id = "email"
name = "email" / >
    <
    /p> <
    p >
    <
    label
for = "phoneNumber" > Phone Number: < /label> <
    input type = "tel"
id = "phoneNumber"
name = "phoneNumber" / >
    <
    /p> <
    p >
    <
    label
for = "gender" > Gender: < /label> <
    select id = "gender"
name = "gender" >
    <
    option value = "Male" > Male < /option> <
    option value = "Female" > Female < /option> <
    option value = "Other" > Other < /option> <
    /select> <
    /p> <
    p >
    <
    label
for = "ageGroup" > Age Group: < /label> <
    select id = "ageGroup"
name = "ageGroup" >
    <
    option value = "Under 18" > Under 18 < /option> <
    option value = "18-25" > 18 - 25 < /option> <
    option value = "26-35" > 26 - 35 < /option> <
    option value = "36-45" > 36 - 45 < /option> <
    option value = "46-55" > 46 - 55 < /option> <
    option value = "Over 55" > Over 55 < /option> <
    /select> <
    /p> <
    p >
    <
    label
for = "cardType" > Card Type: < /label> <
    select id = "cardType"
name = "cardType" >
    <
    option value = "passport" > Passport < /option> <
    option value = "Ghana Card" > Ghana Card < /option> <
    /select> <
    /p> <
    p >
    <
    label
for = "cardNumber" > Card Number: < /label> <
    input type = "text"
id = "cardNumber"
name = "cardNumber" / >
    <
    /p> <
    input type = "submit"
value = "Save" / >
    <
    /form> <
    /div>
t9aqgxwy

t9aqgxwy1#

解决了它,显然我必须更新我的控制器的方法:

@PostMapping("/add-patient")
public String addClient(@ModelAttribute Client client) {
    LOG.info("Saving client: {}", client);
    
    // Format the date
    Date date = client.getDateOfBirth();
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String strDate = dateFormat.format(date);
    
    client.setDateOfBirth(client.getDateOfBirth());
    
    clientRepository.save(client);
    
    return "redirect:/add-patient";
}

最后加上

@DateTimeFormat(pattern = "yyyy-MM-dd")

对我有用

相关问题