I'm trying tu update my @SessionAttributes
like this way, but when my .jsp
reloads, any change is happening to my attributes. I don't understand difference between HttpServletRequest
session and HttpSession
, the only think I know is that HttpSession
is for 30 minutes, according to my web.xml
.
@Controller
@ControllerAdvice
@SessionAttributes({"Usuario", "ImagenUsuario"})
public class UserController {
private UsuarioServicio usuario_servicio;
@Autowired(required=true)
@Qualifier(value="UsuarioServicio")
public void setUsuarioServicio(UsuarioServicio us){
this.usuario_servicio = us;
}
@RequestMapping(value = "/ActualizarImagenPerfil", method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView ActualizarImagenPerfil(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
ModelAndView mav = null;
Usuario usuario = (Usuario) session.getAttribute("Usuario");
Map<String, String> map = new HashMap<>();
if(session.getAttribute("Usuario") != null) {
if(request.getHeader("X-Requested-With") != null && request.getHeader("X-Requested-With").equals("XMLHttpRequest")) {
map.put("redirect", "true");
try{
UsersProfileImage profile_image = new UsersProfileImage();
profile_image.RemoveImage(usuario.getId() + ".");
usuario.setImagenPerfil(profile_image.SaveImage(usuario.getId(), request.getParameter("imagebase64")));
usuario_servicio.ActualizarFotoPerfil(usuario.getId(), usuario.getImagenPerfil());
session.setAttribute("ImagenUsuario", profile_image.Base64ImageRetrieve(usuario.getImagenPerfil()));
session.setAttribute("Usuario", usuario);
map.put("url", request.getContextPath() + "/R_MProfile/1");
} catch(Exception e) {
map.put("url", request.getContextPath() + "/E_Login");
}
try {
ObjectMapper mapper = new ObjectMapper();
response.setContentType("text/html; charset=UTF-8");
response.getWriter().write(mapper.writeValueAsString(map));
response.getWriter().flush();
} catch (IOException ex) {
Logger.getLogger(ProfileController.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
mav = new ModelAndView("forward:/R_MProfile");
}
} else {
mav = new ModelAndView("forward:/Login");
}
return mav;
}
@RequestMapping(value = "/BorrarImagenPerfil", method = RequestMethod.GET)
public ModelAndView BorrarImagenPerfil(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
ModelAndView mav;
Usuario usuario = (Usuario) session.getAttribute("Usuario");
if(session.getAttribute("Usuario") != null) {
try{
UsersProfileImage profile_image = new UsersProfileImage();
profile_image.RemoveImage(usuario.getId() + ".");
usuario_servicio.BorrarFotoPerfil(usuario.getId());
usuario.setImagenPerfil(null);
mav = new ModelAndView("forward:/R_MProfile/2");
} catch(Exception e) {
mav = new ModelAndView("forward:/R_MProfile");
}
} else {
mav = new ModelAndView("forward:/Login");
}
return mav;
}
}```
@SessionAttributes allows you to do is tell Spring which of your model attributes will also be copied to HttpSession before rendering the view.
JSP scriptlet to show the HttpSession attributes.
<h3>Session Scope (key==values)</h3>
<%
java.util.Enumeration<String> sessEnum = request.getSession()
.getAttributeNames();
while (sessEnum.hasMoreElements()) {
String s = sessEnum.nextElement();
out.print(s);
out.println("==" + request.getSession().getAttribute(s));
%><br />
<%
}
%>
Annotate controller class with @SessionAttributes to put the same model attribute (requestObject) in the Spring session.
@Controller
@SessionAttributes("requestObject")
public class MyController {
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.