gpt4 book ai didi

java - Spring MVC 应用

转载 作者:行者123 更新时间:2023-11-29 07:33:20 24 4
gpt4 key购买 nike

大家好,我正在学习如何使用 Spring,我没有 MVC 经验。

所以我正在制作一个网站,可以对 mysql 数据库的数据进行注册、取消注册和更改。登录和插入数据库已准备就绪,但我无法删除注册用户部分。

我的模型:

 import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;

public class StudentDelete {

@NotEmpty
@Size(min=4, max=20)
private String userName;

@NotEmpty
@Size(min=4, max=8)
private String password;

public String getPassword() {
return password;
}

public String getUserName() {
return userName;
}

public void setPassword(String password) {
this.password = password;
}

public void setUserName(String userName) {
this.userName = userName;
}
}

我的 Controller :

@Controller
@SessionAttributes("student")
public class StudentController {

@Autowired
private StudentService studentService;
@RequestMapping(value="/delete", method=RequestMethod.GET)
public String delete(Model model) {
Student studentDelete = new Student();
model.addAttribute("studentDelete", studentDelete);
return "delete";
}

blablabla

@RequestMapping(value="/delete", method=RequestMethod.POST)
public String delete(@Valid @ModelAttribute("studentDelete") StudentDelete studentDelete, BindingResult result) {
if (result.hasErrors()) {
return "delete";
} else {
boolean found = studentService.findByLogin(studentDelete.getUserName(), studentDelete.getPassword());
if (found) {
studentService.deleteByLogin(studentDelete.getUserName(), studentDelete.getPassword());
return "successD";
} else {
return "failureD";
}
}
}

我的服务和实现:

package com.github.elizabetht.service;

import com.github.elizabetht.model.Student;

public interface StudentService {
Student save(Student student);
boolean findByLogin(String userName, String password);
boolean findByUserName(String userName);
boolean deleteByLogin(String userName, String password);
}

实现:

public boolean deleteByLogin(String userName, String password) {

StudentDelete stud = studentDeleteRepository.deleteByLogin(userName, password);

if(stud != null) {
return true;
}

return false;
}

最后是 StudentDeleteRepository:

package com.github.elizabetht.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.github.elizabetht.model.Student;
import com.github.elizabetht.model.StudentDelete;

@Repository("studentDeleteRepository")
public interface StudentDeleteRepository extends JpaRepository<StudentDelete, Long> {

@Query("delete s from Student s where s.userName = :userName and s.password = :password")
StudentDelete deleteByLogin(@Param("userName") String userName, @Param("password") String password);

}

StudentRepository.java

package com.github.elizabetht.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.github.elizabetht.model.Student;

@Repository("studentRepository")
public interface StudentRepository extends JpaRepository<Student, Long> {

@Query("select s from Student s where s.userName = :userName")
Student findByUserName(@Param("userName") String userName);

}

在我的delete.jsp中,一切都以此开始:

<form:form id="myForm" method="post"
class="bs-example form-horizontal" commandName="studentDelete">

我收到此错误:

java.lang.NullPointerException
com.github.elizabetht.service.StudentServiceImpl.deleteByLogin(StudentServiceImpl.java:47)

这是哪一部分:

StudentDelete stud = studentDeleteRepository.deleteByLogin(userName, password);

为什么 save 方法没有发生这种情况?

感谢任何帮助。谢谢!

最佳答案

您的问题是您在下面传递的参数:

StudentDelete stud = studentDeleteRepository.deleteByLogin(userName, password);

用户名或密码为空。

因此出现空指针异常。

将您的实现更改为以下格式:

public boolean deleteByLogin(String userName, String password) {

if(userName == null || password == null)
return false; // it failed essentially

StudentDelete stud = studentDeleteRepository.deleteByLogin(userName, password);

if(stud != null) {
return true;
}

return false;
}

关于java - Spring MVC 应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31820342/

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