Receive property from a spring form annotated with @Sessionscope

Asked

Viewed 77 times

0

I have a Spring form to login to the system. This login must be kept in session, so I noted the classe Credencial (equivalent to login) with @SessionScope. I receive the data of this credential from the login form, only when it is annotated with @SessionScope, the form values are not received by the method.

Credential Class:

@SessionScope
@Component
public class Credencial {

    @CodigoFuncional(message="O valor informado não é um código funcional!")
    private String codigoFuncional;

    @Size(min=7, message="A senha deve ter pelo menos 7 caracteres")
    private String senha;

    private String nome;

    private int juncao; 

    private boolean ativo;

    public Credencial() {}

    public Credencial(String codigoFuncional, String nome,
            int juncao, boolean ativo) {
        this.codigoFuncional = codigoFuncional;
        this.nome = nome;
        this.juncao = juncao;
        this.ativo = ativo;
    }

    public String getCodigoFuncional() {
        return codigoFuncional;
    }

    public void setCodigoFuncional(String codigoFuncional) {
        this.codigoFuncional = codigoFuncional;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getJuncao() {
        return juncao;
    }

    public void setJuncao(int juncao) {
        this.juncao = juncao;
    }

    public boolean isAtivo() {
        return ativo;
    }

    public void setAtivo(boolean ativo) {
        this.ativo = ativo;
    }

}

Controller class:

@Controller
@RequestMapping(value="/")
//@SessionAttributes("credencial")
public class CredencialController {

    @Autowired
    private Credencial credencial;

    @PostMapping(value="/validarLogin/")
    @ModelAttribute("credencial")
    public RedirectView efetuarLogin(RedirectAttributes atributos, @Valid @ModelAttribute("credencial") Credencial credencial, BindingResult resultado, ModelMap modelo){

        RedirectView view = new RedirectView();
        if(resultado.hasErrors()){
            view.setUrl("/SCDA/");
            atributos.addFlashAttribute("org.springframework.validation.BindingResult.credencial", resultado);
            atributos.addFlashAttribute("credencial", credencial);
        }else{

            if(Utilitario.validaSenha(credencial.getCodigoFuncional(), credencial.getSenha())){
                atributos.addFlashAttribute("credencial", credencialService.captura(credencial.getCodigoFuncional()));
                view.setUrl("/SCDA/inicio/");
            }else{
                view.setUrl("/SCDA/");
                resultado.reject("errorCode", "Usuário e/ou senha incorretos!");
                atributos.addFlashAttribute("credencial", credencial);
                atributos.addFlashAttribute("org.springframework.validation.BindingResult.credencial", resultado);
            }
        }   
        return view;
    }
}

Form:

<form:form class="m-t" id="form-login" modelAttribute="credencial" name="form-login" role="form" action="/SCDA/validarLogin/" method="POST">
    <form:errors cssClass="erro"/>
    <form:hidden id="senha-encriptada" path="senha" />
    <div class="form-group">
        <form:input path="codigoFuncional" type="text" class="form-control" placeholder="Código Funcional" required />
        <form:errors path="codigoFuncional" cssClass="erro"/>
    </div>
    <div class="form-group">
        <input type="password" id="senha" class="form-control" placeholder="Senha do CORP" />
        <form:errors path="senha" cssClass="erro"/>
    </div>              
    <a href="javascript:validaLogin()" title="Fazer Login" class="btn btn-primary block full-width m-b">Login</a>

When I give the submit, the Spring cannot take the value of the Credential when calling the method, everything is null. If I comment the annotation "@SessionAttributes" the Spring not even enough in this method.

How do I log this credential into session using Spring? (Without directly using the HttpServletRequest.getSession())

2 answers

0

  • hmm, when I note down the Credential class with @Modelattribute the compiler complains that this annotation is not allowed at this location

0


Hello,

Sorry, it is not in the class this annotation but in a separate method just to bind the attributes, it would look something like

@Controller
@RequestMapping(value="/")
@SessionAttributes("credencial")
public class CredencialController {
   . . .

   @PostMapping(value="/validarLogin/")
   public RedirectView efetuarLogin(RedirectAttributes atributos, @Valid @ModelAttribute("credencial") Credencial credencial, BindingResult resultado, ModelMap modelo){
     . . . 
   }

   @ModelAttribute("credencial")
   public Credencial credencial() {
     return new Credencial();
   }

One more example

https://www.javadevjournal.com/spring-mvc/spring-mvc-session-attributes/

Browser other questions tagged

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