Spring Data Jpa 页面重定向和数据不存储在数据库proprely在springboot

hi3rlvi2  于 2023-11-19  发布在  Spring
关注(0)|答案(1)|浏览(107)

我使用thymeleaf作为前端技术和作为后端的springboot.我试图通过注册页面注册新用户,但当我插入数据,数据不存储在数据库中,当用户数据提交并按下提交按钮,它不会重定向到successPage.它给了我这种类型的错误页面.

userRegistration.html

<form th:action="@{/createNewuser}" th:object="${user}" method="post">
          <div class="form-group">
            <label>NAME</label> <input type="text" name="name"
                                       th:field="*{name}" class="form-control" placeholder="Enter name" />
          </div>
          <div class="box-footer">
            <button type="submit" class="btn btn-primary">SUBMIT</button>
          </div>
        </form>

字符串
UserEntityController.java

@Controller
@RequestMapping("/user")
public class UserEntityController {
    @Autowired
    private UserEntityServiceImpl userServiceImpl;

    }
    @GetMapping("/newUser")
    public String saveUser(Model model) {
        UserEntity user = new UserEntity();
        //model.addAllAttributes("user",user);
        model.addAttribute("user",user);
        return "userRegistration";
    }
    @GetMapping("/successPage")
    public String successPage() {
        return "successPage"; 
    }

    @PostMapping("/createNewuser")
    public String createUawe(@ModelAttribute("user") UserEntity user,
                             BindingResult bindResult,Model model){

        if (bindResult.hasErrors()){
            return "userRegistration";
        }
        if (userServiceImpl.isEmailAlreadyRegistered(user.getEmail())) {
            bindResult.rejectValue("email", "error.user", "Email is already registered.");
            return "userRegistration";
        }

        if (userServiceImpl.isMobileNoAlreadyRegistered(user.getMobileNo())) {
            bindResult.rejectValue("mobileNo", "error.user", "Mobile number is already registered.");
            return "userRegistration";
        }
         user=userServiceImpl.saveUser(user);
         model.addAttribute("name",user.getName());
        return "redirect:/successPage";
    }


}

hkmswyz6

hkmswyz61#

您正在使用的URL与UserLogistyController类不匹配。
您正在浏览器中访问localhost:8080/localhost:8080/localhost:8080
查看您的Controller类。在类级别,您将请求Map设置为**/user**。

@Controller
@RequestMapping("/user")
public class UserEntityController {

字符串
然后,用于创建用户的方法具有与之Map的“创建新用户”。

@PostMapping("/createNewuser")
// please correct your method name
public String createUawe(@ModelAttribute("user") UserEntity user,
                             BindingResult bindResult,Model model){


你的道路应该是:
localhost:8080/user/用户名新用户

相关问题