gpt4 book ai didi

java - 检查帐户是否已存在时出现空指针异常 - Spring+Thymeleaf

转载 作者:行者123 更新时间:2023-12-02 10:58:03 26 4
gpt4 key购买 nike

我是编程初学者,所以我对这个问题感到抱歉,但坐了几天还是不知道。我正在制作一个博客,并想在新用户注册时检查该帐户是否已存在(检查登录名)。当我尝试在网站上注册时(甚至数据库中不存在登录名) - 收到此错误:

2018-07-27 22:27:03.561 ERROR 19368 --- [nio-8084-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
at com.jzych.blog_springboot.controllers.SigninController.createUserAccount(SigninController.java:78) ~[classes/:na]
at com.jzych.blog_springboot.controllers.SigninController.registerUserAccount(SigninController.java:61) ~[classes/:na]

UserService 中名为“registerNewUserAccount”的方法似乎存在问题。也许这是一个非常简单的问题,但我找不到它。我试图实例化 UserDto (我正在使用数据传输对象)和 UserEntity 对象,但发生编译时错误。你能帮我找出这段代码有什么问题吗?

有 SigninController:

@Controller
public class SigninController {

@Autowired
UserRepo userRepo;
UserService service;


@GetMapping("/register")
public String registerForm(Model model, WebRequest request){
UserDto accountDto = new UserDto();
model.addAttribute("userentity", accountDto);
return "signinPage";
}

@PostMapping("/register")
public ModelAndView registerUserAccount(@ModelAttribute("userentity") @Valid UserDto accountDto,
BindingResult result, WebRequest request, Errors errors){

UserEntity registered = new UserEntity();
if(!result.hasErrors()){
registered = createUserAccount(accountDto, result); //java:61
}
if(registered == null){
result.rejectValue("login", "message.regError");
}
if(result.hasErrors()){
return new ModelAndView("signinPage", "userentity", accountDto);
}
else{
return new ModelAndView("homePage", "userentity", accountDto);
}
}

private UserEntity createUserAccount(@Valid UserDto accountDto, BindingResult result) {
UserEntity registered = null;
try{
registered = service.registerNewUserAccount(accountDto); //java:78
}catch (LoginExistsException e) {
return null;
}
return registered;
}
}

UserService类

@Service
public class UserService implements IUserService {

@Autowired
private UserRepo userRepo;

@Override
public UserEntity registerNewUserAccount(UserDto accountDto)
throws LoginExistsException
{

if(loginExist(accountDto.getLogin())){
throw new LoginExistsException(
"There is an account with that login:" + accountDto.getLogin());
}

UserEntity userEntity = new UserEntity();

userEntity.setName(accountDto.getName());
userEntity.setLogin(accountDto.getLogin());
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
userEntity.setPassword(passwordEncoder.encode(accountDto.getPassword()));
userEntity.setRole(accountDto.getRole());

return userRepo.save(userEntity);
}

private boolean loginExist(String login){
UserEntity userEntity = userRepo.getByLogin(login);
if(userEntity != null) {
return true;
}
return false;
}
}

界面

public interface IUserService {
UserEntity registerNewUserAccount(UserDto accountDto)
throws LoginExistsException;

和我的模型

@Entity
@Table(name = "user")
public class UserEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idUser;

private String name;

private String login;

private String password;

private String role;

@OneToMany//(mappedBy = "user") kaskadowość przy usuwaniu?
private List<Post> posts = new ArrayList<>();

public UserEntity() {
}

public UserEntity(String name, String login, String password, String role) {
this.name = name;
this.login = login;
this.password = password;
this.role = role;
}
//getters, setters, toString, hashcode and equals

UserDto模型

public class UserDto {

@NotNull
@NotEmpty
private String name;

@NotEmpty
@NotNull
private String login;

@NotEmpty
@NotNull
private String password;

private String role;

//getters, setters and toString

非常原始的 HTML 页面,使用 Thymeleaf 来注册新用户

<body>
sign in
<h1>Form</h1>
<form th:action="@{/register}" th:object = "${userentity}" method="post">
<div>
<label>name</label>
<input th:field = "*{name}" />
<p th:each="error:${#fields.errors('name')}"
th:text="${error}">Validation error</p>
</div>
<div>
<label>login</label>
<input type="text" th:field = "*{login}" />
<p th:each="error:${#fields.errors('login')}"
th:text="${error}">Validation error</p>
</div>
<div>
<label>password</label>
<input type="password" th:field = "*{password}" />
<p th:each="error:${#fields.errors('password')}"
th:text="${error}">Validation error</p>
</div>
<div>
<label>role</label>
<select th:field = "*{role}">
<option value="ROLE_USER">USER_Role</option>
<option value="ROLE_ADMIN">ADMIN_Role</option>
</select>
</div>
<div>
<p><input type="submit" value = "Sign in" /></p>
</div>
</form>
</body>

最佳答案

你忘记了@Autowired UserService 类

错误的方式:

@Autowired
UserRepo userRepo;
UserService service;

正确方法:

@Autowired
UserRepo userRepo;
@Autowired
UserService service;

关于java - 检查帐户是否已存在时出现空指针异常 - Spring+Thymeleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51565533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com