I am creating an insert page for registration. Users can complete the registration form and click the submit button. After displaying a white-label error. What is the problem? How can solve that problem? Used thyelf
Member Controller
package com.booking.system.controller;
import com.booking.system.entity.Doctor;
import com.booking.system.entity.Member;
import com.booking.system.service.DoctorService;
import com.booking.system.service.MemberService;
import net.bytebuddy.implementation.bind.annotation.Super;
import org.apache.catalina.Store;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MemberController {
public MemberService memberService;
public MemberController(MemberService memberService) {
super();
this.memberService = memberService;
}
@GetMapping("/members")
public String MemberPage(){
return "members";
}
//html page name
@GetMapping("/member_register")
public String showSignUpForm(Model model){
model.addAttribute("members", new Member());
return "member_register";
}
@PostMapping("/members")
public String Member (@ModelAttribute("member") Member member){
memberService.saveMember(member);
return "redirect:/members";
}
}
Member(entity)
package com.booking.system.entity;
import javax.persistence.*;
@Entity
@Table(name = "members")
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "email")
private String email;
@Column(name = "first_name", nullable=false)
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "password", nullable = false)
private String password;
@Column(name = "phone_number")
private String phoneNumber;
public Member(){
}
public Member(Long id, String email, String firstName, String lastName, String password, String phoneNumber) {
this.id = id;
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
this.password = password;
this.phoneNumber = phoneNumber;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
MemberSevice
package com.booking.system.service;
import com.booking.system.entity.Member;
import java.util.List;
public interface MemberService {
Member saveMember(Member member);
}
**MemberServiceImpl**
package com.booking.system.service;
import com.booking.system.entity.Member;
import java.util.List;
public interface MemberService {
Member saveMember(Member member);
}
**member_register.html**
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Member Registration </title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-0evHe/X R7YkIZDRvuzKMRqM OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
</head>
<body>
<div >
<h1>Member Registration</h1>
</div>
<div >
<form action="@{/members}" method="POST" th:object="${members}" >
<div >
<label >Email address</label>
<input type="email"
th:field="*{email}"
placeholder="Enter email">
<small id="emailHelp" >We'll never share your email with anyone else.</small>
</div>
<div >
<label >First Name</label>
<input type="text"
th:field="*{firstName}" required
placeholder="Enter First Name">
</div>
<div >
<label >Last Name</label>
<input type="text"
th:field="*{lastName}" required
placeholder="Enter Last Name">
</div>
<div >
<label >Password</label>
<input type="password"
th:field ="*{password}"
placeholder="Password">
</div>
<div >
<label >Phone Number</label>
<input type="text"
th:field="*{phoneNumber}"
placeholder="Enter Phone Number" required>
</div>
<div >
<input type="checkbox" id="exampleCheck1">
<label for="exampleCheck1">Check me out</label>
</div>
<button type="submit" >Register</button>
</form>
</div>
</body>
</html>
**member.html**
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Member Page</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X R7YkIZDRvuzKMRqM OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
</head>
<body>
<div >
<div>
<h1>Welcome Member</h1>
</div>
<div>
<h3><a th:href="@{/member_register}">Register</a></h3>
</div>
</div>
</body>
</html>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bookingSystem</groupId>
<artifactId>bookingsystem</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Booking_System</name>
<description>Booking System for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.1.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
MemberRepository
package com.booking.system.repository;
import com.booking.system.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MemberRepository extends JpaRepository<Member, Long> {
}
CodePudding user response:
You are actually getting a 404 (Not Found) error. The URL in the screenshot looks kind of weird too. Can you verify that the registration-page is really located on the server at the path you are trying to access (/@{/Members})
I believe the @{ and } in your URL are there because in the form-action within the HTML, you're calling "@{members}". Are you sure that using the @{} is correct? I'm not sure which framework that would be
CodePudding user response:
Below code will work correctly for you:
<form action="members" method="POST" th:object="${members}" >