Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7897681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:13:46+00:00 2026-06-03T08:13:46+00:00

I would like to share Session Attributes betweet two Controllers in Spring MVC, using

  • 0

I would like to share Session Attributes betweet two Controllers in Spring MVC, using the @SessionAttributes annotation.

Here is a simple code I use to test the attributes sharing :

AController.java

@Controller
@SessionAttributes("myParam")
public class AController {

@RequestMapping(value="/a")
public String handle(Model model){

    if(!model.containsAttribute("myParam"))
        model.addAttribute("myParam", randomInt());

    return "a";
}

private int randomInt(){
    return new Random().nextInt(100);
}

}

a.jsp

<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<h1>Page A</h1>
<p>Param = ${myParam}</p>
</html>

BController.java

@Controller
@SessionAttributes("myParam")
public class BController {

@RequestMapping(value="/b")
public String handle(Model model){

    if(!model.containsAttribute("myParam"))
        model.addAttribute("myParam", randomInt());

    return "b";
}

private int randomInt(){
    return new Random().nextInt(100);
}

}

b.jsp

<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<h1>Page B</h1>
<p>Param = ${myParam}</p>

The behaviour I expect is to go to the /a URL, myParam would be set to a random value between 0 and 99, then this value would be shared between the two controllers.

However,what happens is the following:
I go to the /a URL, myParam is set to a value (let’s say 10).
Then i go to the /b URL, myParam is set to another value (let’s say 20).
When I go back to the /a URL, myParam value is the one set by the BController ( myParam =20).

Once the two controllers methods have been executed, the value is shared, but before, each controller redefines a new value.

It seems like if a Controller has never set a value to a SessionAttribute, it does not detect that attribute if it was set by another Controller.

I really would like to be able to share session attributes betwen controllers without using the HttpSession object and sticking with Spring MVC 3 objects.

I would like to know if I missed something or if there are other practices to share data in a session between controllers.

NB : the webapp was deployed on a Tomcat7 server.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:13:48+00:00Added an answer on June 3, 2026 at 8:13 am

    In Spring MVC, @SessionAttributes are not meant to be shared accross Controllers.

    What I did instead was to create an object (via a POJO) with the Session scope, that holds all the values I need to store for a Session.

    I then use the JDK dynamic proxy to Autowire the object to the Controllers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project where I would like to create two session cookies in
I would like to share a code snippet.. if (!key.contains(#)) { val = mp.get(key)
I would like to share 2 folders of content ('stylesheets' and 'graphics') between multiple
I would like to share my Oracle SQL Developer configuration across my several computers
Problem: I would like to share code between multiple assemblies. This shared code will
I have a numpy array that I would like to share between a bunch
My problem is: I have 3 procs that would like to share config loaded
So I'm creating an array called fruits which I would like to share between
I have 10 websites on an IP. I would like to share the IP
I would like to know if there is some way to share a variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.