Hi all I′m learning how to use Spring, I don′t have experience in MVC.
So I′m making a website who makes registrations, de-registrations and changes in the data of a mysql database.
The login and inserts to DB are ready but I can't make the delete registered user part.
My model:
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;
}
}
My 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";
}
}
}
My Service and the implementation:
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);
}
Implementation:
public boolean deleteByLogin(String userName, String password) {
StudentDelete stud = studentDeleteRepository.deleteByLogin(userName, password);
if(stud != null) {
return true;
}
return false;
}
And finally The 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);
}
In my delete.jsp all starts with this:
<form:form id="myForm" method="post"
class="bs-example form-horizontal" commandName="studentDelete">
I'm getting this error:
java.lang.NullPointerException
com.github.elizabetht.service.StudentServiceImpl.deleteByLogin(StudentServiceImpl.java:47)
Which is this part:
StudentDelete stud = studentDeleteRepository.deleteByLogin(userName, password);
Why it doesnt happen with the save method?
Any help is appreciated.
Thanks!
See Question&Answers more detail:
os