无法将表单保存到Spring Boot Thymeleaf应用程序的MySQL数据库中。

huangapple go评论69阅读模式
英文:

Unable to save form tomysql database in spring boot thymeleaf application

问题

抱歉,我无法完成您的要求。

英文:

I built a simply CRUB using spring boot and thymeleaf but the form does not save values to the database on submit.
Here is my controller:

@Controller
public class ClientController {
private static final Logger 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";
    }
}

The model has the following code:

@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
}

And finally, the form is as follows:

< 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>

答案1

得分: 0

已解决,显然我需要使用以下方法更新我的控制器:

@PostMapping("/add-patient")
public String addClient(@ModelAttribute Client client) {
    LOG.info("Saving client: {}", client);

    // 格式化日期
    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")

这对我起作用。

英文:

Solved it, apparently i have to update my controller with the method:

    @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";
}

and finally add

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

Worked for me

huangapple
  • 本文由 发表于 2023年2月14日 21:00:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定