0

I am developing a web application using spring MVC. I just want a simple example of how to do session management in this. I have seen lot of forums but I am not able to get a clear picture of this

My requirement is

I have an object, which I would like to be accessible in all controllers and JSP's I would like to set that in the controller and get that in JSP

I am looking for something like

    Session.setAtribute(); 

Could you please let me know a very simple instance . Thank you

2 Answers 2

2

There are different ways of accessing servlet session in Spring MVC. But I think this one is the one that best suits your problem. You can create a session scoped bean, which holds your desired info:

@Component("myObjectHolder")
@Scope(WebApplicationContext.SCOPE_SESSION)
public class MyObjectHolderImpl implements MyObjectHolder {

    private long userId;
    private String username;
    private Theme theme;

    // Getters & Setter
}

Then, you can access to it from other beans:

@Controller
public class MyController {

    @Autowired private MyObjectHolder myObjectHolder;

    @RequestMapping
    public ModelAndView switchTheme(String themeId) {
        ...
        Theme newTheme = themeService.get(themeId);
        myObjectHolder.setTheme(newTheme);      
        ...
    }

}

You can access directly from your view too, but you must configure it:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    ...
    <property name="exposedContextBeanNames" value="myObjectHolder" />
</bean>

And in your JSP:

Hi ${myObjectHolder.username}, you switched 
application theme to ${myObjectHolder.theme.name}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply.So MyObjectHolderImpl is where I set my seesion object and I can get it from anywhere rite ,.. COz I dun have to set the session object multiple times, I just need to set it once in one of the controlldersin the My
@user1562262: No, you probably don't really need to access to session object. You can save desired info (such as user id, name, theme, preferences...) in MyObjectHolder. I'll update my answer.
1

The simplest approach is to access HttpSession directly by injecting it into your handler method:

@RequestMapping("/page")           
public ModelAndView page(HttpSession session) {           
    session.getAttribute("foo");
}

2 Comments

Thank s for your reply .. I just want to know like, do I have to inject HttpSession session in the controller that sets the session or both only in the controllers that get the session
@user1562262: inject HttpSession whenever you need it :-). Also read a little bit more, there are much more convenient ways to access HTTP session attributes in Spring MVC, but requiring a little bit of setup upfront.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.