How to take user image in session and add to html?

Asked

Viewed 234 times

0

I’m developing a web application using spring boot. I already have saved image of the user in the database, now I would like to assign the image of the user logged in the session in the profile photo of my Dashboard. How to do this? I can bring other information like name and cnpf, but his image can not.

inserir a descrição da imagem aqui

My controller:

@Controller
public class IndexController {

@Autowired
private UsuarioService service;
@RequestMapping("/index")
public ModelAndView index(HttpSession session) {
    Usuario usuario = service.findByEmail(SecurityContextHolder.getContext().getAuthentication().getName());
    ModelAndView mv = new ModelAndView("/home");
    session.setAttribute("usuario", usuario);
    return mv;
}

My html page:

<div class="avatar">

                    <img th:src="@{/session.usuario/image/{image_id}(image_id=${session.usuario.id})}">
                </div>
                <div class="title">
                    <h1 class="h6" th:text="${session.usuario.nome}"></h1>
                    <p class="h2" th:text="${session.usuario.cnpjCpf}"></p>
                </div>
            </div>
  • See if the answers in this question help you.

  • 1

    @Statelessdev not necessarily, that’s why I already do there in detail, the question is, I have to take the image of the user in the session, because if it is according to the answer you sent I would have to do it for all controllers, if it would not return null object.

1 answer

0


Hello, in this question there is an answer that may be useful, the second solution has the line:

Mav.addObject("userImage", base64Encoded );

Switch to:

Session.setAttribute("photoUsuario", base64Encoded);

And in JSP use:

<img src="data:image/jpeg;base64,${fotoUsuario}" />

  • Don’t forget to use the rest of the solution to turn your image into Base64.

Browser other questions tagged

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