I cannot display Spring error message

Asked

Viewed 814 times

0

I’m trying to validate some annotated fields, but I can’t bring the messages to the jsp page:

Java user.

mport java.util.Calendar;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Usuario {

private String nome;
private String sobrenome;
private String cpf;
private String identidade;
@NotNull(message="{usuario.email.nulo}")
@Email(message="{usuario.email.invalido}")
private String email;
@NotNull(message = "{usuario.senha.nula}")
@Size(min = 6, message="{usuario.senha.pequena}")
private String senha;
private Endereco endereco;
private Calendar dataNascimento;

Has a Client class that extends User:

And it has Clientecontroller.java

package br.com.wlsnprogramming.loja.controller;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import br.com.wlsnprogramming.loja.model.usuario.Cliente;

@Controller
@RequestMapping("cliente")
public class ClienteController {

@RequestMapping("login")
public String formLogin() {
    return "cliente/login";
}
@RequestMapping("efetuarLogin")
public String efetuarLogin(@Valid Cliente cliente, BindingResult result, HttpSession session) {
    if(result.hasFieldErrors("email") || result.hasFieldErrors("senha")) {
        System.out.println(result.getFieldError("senha").getDefaultMessage());//Aqui em fiz o teste para ver se recuperava a mensagem, ate aqui deu certo
        return "redirect:login";
    }
    //faz a verificação aqui
    return "redirect:meusPedidos";
}
}

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8"/>
<title>Inicio</title>
<link rel="stylesheet" href="../resources/styles/login.css" type="text/css"/>
<link rel="stylesheet" href="../resources/styles/style.css" type="text/css"/>
</head>
<body>
<c:import url="../header.jsp"/>
<div id="conteudo">
<div id="centro-login">
    <h1>Entrar na Conta</h1>
    <p>Ainda não tem uma conta? <a href="#">Crie já!</a></p>
    <form id="login" method="post" action="efetuarLogin">
        <label for="email">E-mail:</label>
        <input type="email" name="email" id="email" required="true" />
        <label for="senha">Senha:</label>
        <input type="password" name="senha" id="senha" required="true" />
        <!--É AQUI QUE EU TENTO RECUPERAR A MENSAGEM DE ERRO, MAS NÃO CONSIGO, NÃO ENTEDI MUITO BEM O QUE VAI NO ATRIBUTO path -->
        <p><form:errors path="cliente.senha" cssStyle="color:red"/></p>
        <!--************************************************-->
        <input type="submit" value="Entrar"/>
        <p>Esqueceu a sua senha? Estão recupere <a href="#">clicando aqui!</a></p>
    </form>
</div>
</div>
<script type="text/javascript">
var menuAberto = false;
function abrirMenu(){
    if(!menuAberto){
        document.getElementById("menu-escondido").style.display = "block";
        menuAberto = true;
    } else {
        document.getElementById("menu-escondido").style.display = "none";
        menuAberto = false;
    }
}
</script>
<c:import url="../fooder.jsp"/>
</body>
</html>

I’ve tried to put everything in the path, but it doesn’t work.

3 answers

0


I already discovered the error, it seems you have to redirect on the server side:

return "cliente/login";// direcionando diretamente para o jsp

or

return "forward:login";// direcionado para a função, mas do lado do servidor

You cannot use redirect to redirect on the client side.

-1

Just insert the Spring validation dependency into the project! Spring-Boot-Starter-Validation.

org.springframework.boot spring-boot-Starter-validation 2.4.0

-2

Try to put dependency on pom. Spring boot worked.

org.springframework.boot spring-boot-Starter-validation

Browser other questions tagged

You are not signed in. Login or sign up in order to post.