Spring MVC 应用程序 [英] Spring MVC application

查看:52
本文介绍了Spring MVC 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi all I´m learning how to use Spring, I don´t have experience in MVC.

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

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.

我的模型:

 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:

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

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 中,所有内容都以这个开头:

In my delete.jsp all starts with this:

<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 方法不会发生这种情况?

Why it doesnt happen with the save method?

感谢任何帮助.谢谢!

推荐答案

我认为您在评论中指定的错误:

I think the error you specified in the comments:

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

Not a managed type: class com.github.elizabetht.model.StudentDelete

是因为您的模型类上缺少 JPA @Entity 注释:

is because you are missing the JPA @Entity annotation on your model class:

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 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆