• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Spring MVC, passing data, one or many controllers  RSS feed

 
Marshal
Posts: 5987
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two related Spring MVC questions:

  • How do I pass data from one controller to another, and
  • Should I use one controller?


  • I know that if I use one controller I can use SessionData but I currently have several controllers. Combining them would not be a problem (I think) but I don't want to do that if it's not necessary.
     
    Ranch Hand
    Posts: 672
    4
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    why do you need to pass data between two controllers? Are you trying to create a wizard page?
     
    Knute Snortum
    Marshal
    Posts: 5987
    155
    Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have a model of a user and I want to pass this user from controller to controller if they're logged in.
     
    Prasad Krishnegowda
    Ranch Hand
    Posts: 672
    4
    Eclipse IDE Java Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Then i would suggest to use a Facade design pattern.. You can implement an SessionFacade with session scope, and populate this bean once the user logs in for the first time, from there on, you can use this bean (by autowiring) in whichever controller you would require this information.
     
    Knute Snortum
    Marshal
    Posts: 5987
    155
    Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I've been working on this, with no success. I've never gotten data from a bean I've autowired, so I may be doing things incorrectly. I created a Session object like this:



    I used proxy mode because when I used value = "session", I got an error message saying that Spring couldn't instantiate the bean, and that I should try proxy more instead.

    I set the loggedIn field like this:



    I try to use my Session object like this:



    session.isLoggedIn() is always false, even when it has been set elsewhere.
     
    Prasad Krishnegowda
    Ranch Hand
    Posts: 672
    4
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    First thing i would suggest, Session is a builtin class name in Java, so change the class name to something else.

    Also, with @Scope annotation, along with proxymode attribute, you may need to use value attribute and set it to session, by default, scope of spring bean is singleton.
     
    Knute Snortum
    Marshal
    Posts: 5987
    155
    Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the suggestions. Renamed Session to Session Info. SessionInfo has this annotation:

    @Scope( value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS )

    Still getting loggedIn = false. I am logging messages so I know that the code is hitting the SessionInfo update. Any other ideas?
     
    Rancher
    Posts: 2759
    32
    Eclipse IDE Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are creating a new instance of session in your controller. This will not change the state of the session object that is held by Spring

     
    Ranch Hand
    Posts: 491
    5
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Cause:





    Reason:

    With DEBUGGER + Doc Spring Framework Reference Documentation 4.0.4.RELEASE

    4.5 Bean scopes
    Scoped beans as dependencies

    If you use DEBUGGER AND set a break at below line



    then examine


    You will see it's an already created DI PROXY. SessionInfo$$EnhancerBySpringCGLIB

    When you have below line, the session is just a plain object SessionInfo. It's NOT a PROXY




    >>> When you're in HomeController, you still the PROXY but the value you set was for session = new Session( editor );

    So session.isLoggedIn() give false which is NORMAL. This comes from the PROXY.


    Solution:


    Since you already have an injected Proxy in LoginController, you can set the values (setters)
     
    Knute Snortum
    Marshal
    Posts: 5987
    155
    Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you both; that was exactly the problem.
     
    Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
    ScroogeXHTML - small and flexible RTF to HTML converter library
    https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!