Spring MVC中的应用(Spring MVC application)

2019-10-28 15:27发布

大家好I'm学习如何使用Spring,我不有MVC的经验。

所以I'm制作一个网站谁使注册,去注册和变更在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
@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);

为什么它不与保存方法发生?

任何帮助表示赞赏。 谢谢!

Answer 1:

你的问题是你逝去低于参数:

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;
}


Answer 2:

我想你在评论中指定的错误:

不是托管类型:类com.github.elizabetht.model.StudentDelete

是因为你缺少你的模型类的JPA @Entity批注:

import javax.validation.constraints.Size;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table("student")
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;
    }   
}


文章来源: Spring MVC application