Bonjour,
Dans Spring MVC(2.5, mais je peux aussi utiliser le 3), je voudrais mettre un objet en session.
Actuellement j'utilise cela :
et quand je veux stocker ma variable en session, j'utilise :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 @ModelAttribute("userinfo") public UserInfoBean getModelAttribute(HttpServletRequest request) { HttpSession session = request.getSession(false); if (session != null) { return (UserInfoBean) session.getAttribute(SESSION_ATTRIBUTE); } else { return null; } }
Cela marche, mais cela ne me plait pas des masses de mettre les mains dans le cambouis (au niveau de la request et de la session) ...
Code : Sélectionner tout - Visualiser dans une fenêtre à part request.getSession().setAttribute(SESSION_ATTRIBUTE, userinfo);
Je me suis dit, je vais mettre au niveau de mon contrôleur un bean userinfo avec le scope session !
Mais je n'y arrive pas :
Je déclare mon bean :
Dans mon contrôleur j'ai le code suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part <bean id="userinfo" class="mesPackages.springmvc.web.bean.UserInfoBean" scope="session"/>
Mais quand je lance mon application (sous Jetty) j'ai l'erreur suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 @Controller @RequestMapping(value = {"/hello.html", "/index.html"}) public class HelloSpringMVC { @Autowired private UserInfoBean userinfo; ...
Je ne comprends pourquoi je ne peux pas mettre de bean avec un scope session dans mon contrôleur ... et je ne vois pas comment faire pour pouvoir avoir une solution de ce type !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloSpringMVC': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.orange.mhc.springmvc.web.bean.UserInfoBean MesPackages.springmvc.web.HelloSpringMVC.userinfo; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userinfo': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
Je vous remercie.
Partager