Spring SessionAttributes or httpsession - spring

In my Spring MVC project I have a jsp page , registration page , in which there is a form. In this form the user inserts his data (name, surname and so on).I created a bean User and I bound the form with this bean.But now I have a problem.How can i put the user object into a session in a method of a controller?
Can I use #SessionAttributes,httpsession or I set the scope of the bean to session? How can i solve it?Sorry for my English. Thanks

You can use HttpSession so just need to set your user object in your session and access anywhere into the system.
Refer this httpsession documentation for setting session value.

Related

how many way to access the scope variables in spring-mvc

Some one please me to find out the spring mvc examples,
Because usually, once we log in into the application we will create a session and put some objects into session . we will access later point of time , request scope as well. but spring MVC3 is difficult to understand even documentation also confusing, but every one giving example is basic examples only.
You can access these objects in a JSP/JSTL:
applicationScope
cookie
header
headerValues
initParam
pageContext
pageScope
param
paramValues
requestScope
sessionScope
As well as any request attributes that you add, including model attributes (who's default name is command).
More info here: http://www.informit.com/articles/article.aspx?p=30946&seqNum=7
If you want to access HttpRequest, HttpResponse, HttpSession, add them as arguments to a Spring Controller Handler Method . Spring will pass them in for you.

ModelMap, Session attributes and Spring Portlet MVC

What's the real role of ModelMap in Spring Portlet MVC? I've been testing a few portlets using Spring Portlet MVC 3.0 in WebSphere Portal 7 and it seems every attribute set in ModelMap gets stored in PortletSession.
If that's the case, what's the role of Session Attributes? (by the way, there's a several bug including clearing these guys). And what if I want to store attributes that last only to the current request? (For example, a Confirmation Message). If I put this message in the ModelMap, it gets stored in session and I have to manually clear it every time a user gets into Portal.
If you have added #SessionAttributes annotation to your controller class and have the same #ModelAttribute value, then there is a chance of storing your ModelMap to the PortletSession. But ideally, ModelMap is not defualtly store in PortletSession.

How session sets and unsets in JSF2.0

I want to know about setting and un-setting the session in JSF2.0. Although following some blogs and books (Core JavaServer Faces-3rd Edition), i got to know that using annotation #SessionScoped we can set any manage bean to be in session. I have a loginBean which is #ManagedBean and SessionScoped declared. On the top right corner, my web has login button.
When this session is created (i am not setting it manually, that is why i am confused) and when i gets destroyed? It must be destroyed either by time out or by clicking in logout button only.
JSF uses the Servlet API under the covers. A session scoped managed bean is in essence set as an attribute of the HttpSession. It will be created and set whenever the EL expression referencing the managed bean #{sessionBean} is evaluated for the first time. It will be "removed" from the session whenever the session expires (by either a restart of the client or a timeout in the server) or get invalidated. If you let your logout button call ExternalContext#invalidateSession(), then the session will be invalidated.
If you're familiar with the basic Servlet API, you should already understand how this all works. For an in-depth explanation of the Servlet's HttpSession works under JSF's covers, read this answer: How do servlets work? Instantiation, sessions, shared variables and multithreading.
In jsf 2.0 we can set total class ob as session like i mention
Class_name sm;
ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext(); extContext.getSessionMap().put("Give name for access this property",sm);
Class_name sm = (Class_name) extContext.getSessionMap().get("Give name for access this property");

Set User Object in session using Spring

I would like to set the User Object which contains the details of the user into session in my Spring Application.
I would like to use this session object when I would like to retrieve the details of the logged in user on various JSP pages.
I am using Spring 3 and Spring Security 3
I would like to set the User object in the session from my custom authentication class which is not a controller
How can I achieve this?
I assume you have some implementation of UserDetailsService class. You can return any User object implementing UserDetails from loadUserByUsername(). This object is then automatically placed in your HTTP session. It can then be retrieved with:
User user = (User)SecurityContextHolder.
getContext().getAuthentication().getPrincipal();
Spring Security handles everything you need automatically.

Is it possible to configure a Spring session scoped bean with attributes from the session?

I'm trying to create a service bean that when referenced will be initialized with HttpSession based attributes.
Let's say for sake of argument that my webapp would do the following:
Establish a session
Request login & password
When service is requested (it is scope="session" and has init-method="init()") a new instance is created for the session.
In the init method, can I reference the HttpSession either through passing it in as a parameter and referencing it by EL?
Any ideas on this would be appreciated.
You can access a thread-bound HttpSession as follows:
HttpSession session =
(HttpSession) RequestContextHolder.getRequestAttributes()
.resolveReference(RequestAttributes.REFERENCE_SESSION);

Resources