Java Spring MVC数据库异常未获取

时间:2015-12-19 05:58:26

标签: java spring jsp spring-mvc

在我的春季项目中,我希望将学生信息保存在mysql数据库中。 如果有任何错误,则它将显示在相应输入框内的jsp页面中。

所以,我有: -

StudentDao 班级

public interface StudentDao {
    public void add ( Student student );
    }

StudentDaoImpl

public class StudentDaoImpl implements StudentDao {

    @Autowired
    private SessionFactory sessionFactory;

     public void setSessionFactory(SessionFactory sessionFactory){
            this.sessionFactory = sessionFactory;
        }

    public void add( Student student){
        sessionFactory.getCurrentSession().save(student);
    }}

StudentService

public interface StudentService {
        Student add(String name, String email) throws SQLException, 
                                                   DataAccessException,
                                                   DataIntegrityViolationException,
                                                   ConstraintViolationException{
}

StudentServiceImpl

public class StudentServiceImpl implements StudentService {


    @Autowired
    private StudentDao studentDao;

    @Transactional(propagation = Propagation.REQUIRED)
    public Student add(String name, String email) throws SQLException, 
                                                   DataAccessException,
                                                   DataIntegrityViolationException,
                                                   ConstraintViolationException{


    Student student = new Student(name,email);
        studentDao.add(student);

        return student;
            }

}

控制器

@RequestMapping(value = "/add", method = RequestMethod.POST)
    public String doAdd(@Valid @ModelAttribute("student") Student student,BindingResult result,HttpServletRequest request,
            HttpServletResponse response,Model model){

        validator.validate(student, result);
         if (result.hasErrors()) {

                return "signup";
            }

                @SuppressWarnings("unused")
                Student student1;

                 try{   
                try {
                    student1= studentService.add(student.getName(), student.getEmail());
                } catch (ConstraintViolationException e) {

                    model.addAttribute("email", "already exists");
                    e.printStackTrace();
                     return "signup";
                } catch (DataAccessException e) {

                    model.addAttribute("email", "already exists");
                    e.printStackTrace();
                     return "signup";
                } catch (SQLException e) {

                    model.addAttribute("email", "already exists");
                    e.printStackTrace();
                     return "signup";
                }
                 }catch (DataIntegrityViolationException e) 
                 {
                        model.addAttribute("email", "already exists");

                        e.printStackTrace();
                         return "signup";

                 }

        return "signup";
    }

但是我的数据库电子邮件字段中的问题是unique.so,对于重复条目我收到日志消息:

 WARN JDBCExceptionReporter:100 - SQL Error: 1062, SQLState: 23000
  JDBCExceptionReporter:101 - Duplicate entry 'df@gmail.com' for key 'unique_index2'
org.hibernate.exception.ConstraintViolationException: could not insert: [com.myweb.model.Student]
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'df@gmail.com' for key 'unique_index2'

我想将此错误发送到jsp视图电子邮件字段电子邮件已存在我在服务类和控制器类中的try-catch块中使用了throws块。虽然为什么错误没有显示在jsp查看?

3 个答案:

答案 0 :(得分:1)

所以,你正在使用带休眠的spring .. 例外是: ConstraintViolationException  这就是为什么:

org.hibernate.exception.ConstraintViolationException

只需添加:

<h1>${email}</h1>

您可以使用其他标记来显示该消息。

答案 1 :(得分:0)

只需在 ConstraintViolationException 中写下语法,而不是在Ever Exception中。

语法:  model.addAttribute(“任何变量名称”,“您的消息/值”);

正如您所定义的   model.addAttribute(“email”,“已经存在”);

所以在jsp方面

  

您将收到电子邮件变量消息/值为$ {email}

答案 2 :(得分:0)

添加 jsp页面中的 $ {email} el变量。