Spring session attribute is not shown in JSP via JSTL

Asked

Viewed 33 times

0

I have a Credential class that I use to store session user information (it’s annotated with @Sessionscope and @Component). When I print some attribute of this class, it usually appears, but if I try to use it in a jsp using JSTL, the session attribute is not found. What is the correct way to set up this session attribute?

Credential Class

@SessionScope
@Component
public class Credencial implements Serializable{

    private String codigoFuncional;

    private String nome;

    private int juncao; 

    private String nomeJuncao;

    private boolean ativo;

    private List<Perfil> perfil;

    private boolean primeiroAcesso;

    //construtor e getters & setters

}

Controller class ```java @Controller @Requestmapping(value="/") public class Credelcontroller {

    @Autowired
    private Credencial credencial;

    @PostMapping(value="/validarLogin/")
    public RedirectView efetuarLogin(RedirectAttributes atributos, @Valid @ModelAttribute("credencialDto") CredencialDTO credencialDto, BindingResult resultado, ModelMap modelo){
        System.out.println(credencial.getCodigoFuncional()); //essa linha funciona

    }
  ```

But when I put the following line in the JSP, it doesn’t seem to find the attribute.

<c:out value="${sessionScope.credencial}" />

Is there any configuration missing to work? what am I doing wrong?

1 answer

0


Ok, since my application was using the Spring configuration via java and not XML, I needed to put a line in the Viewresolver configuration:

@Bean
public InternalResourceViewResolver setupViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setViewClass(JstlView.class);
    resolver.setPrefix("/WEB-INF/pages/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true); //Essa linha

    return resolver;
}

Browser other questions tagged

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