简体   繁体   English

Spring MVC应用程序

[英]Spring MVC application

Hi all I´m learning how to use Spring, I don´t have experience in MVC. 大家好,我正在学习如何使用Spring,但我没有MVC的经验。

So I´m making a website who makes registrations, de-registrations and changes in the data of a mysql database. 因此,我正在建立一个网站,负责对mysql数据库的数据进行注册,注销和更改。 The login and inserts to DB are ready but I can't make the delete registered user part. 登录名和插入到DB的文件已准备就绪,但是我无法删除已注册的用户部分。

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: 最后是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 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: 在我的delete.jsp中,所有操作都从此开始:

<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? 为什么使用save方法不会发生这种情况?

Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!

Your issue is that parameter you are passing below: 您的问题是您要在下面传递该参数:

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

username or password are empty. 用户名或密码为空。

Hence the null pointer exception. 因此,空指针异常。

Change your implementation to following formate: 将您的实现更改为以下格式:

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

I think the error you specified in the comments: 我认为您在评论中指定的错误:

Not a managed type: class com.github.elizabetht.model.StudentDelete 不是托管类型:com.github.elizabetht.model.StudentDelete类

is because you are missing the JPA @Entity annotation on your model class: 是因为您缺少模型类上的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;
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM