Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject a session scoped instance with specified name (session attribute name) #97

Closed
gissuebot opened this issue Jul 7, 2014 · 2 comments

Comments

@gissuebot
Copy link

From kevin.wang.kp on April 28, 2007 01:49:37

Maybe just I don't know it, I can't find a way to inject a session scoped
instance with specified name (session attribute name). Thus I can't use the
instance on my jsp page, for example, sessionScope.<myAttributName>.value.
Or that's not the way the thing should go?

Original issue: http://code.google.com/p/google-guice/issues/detail?id=97

@gissuebot
Copy link
Author

From tonyandpat on January 13, 2008 16:38:07

The session scoped instance is stored under the name obtained by converting the "Key"
to a string.
For an injected field of type "com.example.Service" and no annotation, this will look
something like "Key[type=com.example.Service, annotation=[none]]"
Within your JSP page you can then use:
<c:set var="serivceRef" value="${sessionScope['Key[type=com.example.Service,
annotation=[none]]']}"/>

Of course, it would be nice to check if the key includes the
'com.google.inject.name.Named' annotation, and then just use the name as the session
attribute name.

Otherwise you can always create your own custom scope (based on
com.google.inject.servlet.ServletScopes) to do this.

@gissuebot
Copy link
Author

From limpbizkit on May 31, 2008 12:16:41

Usually you should use @SessionScoped so I don't need to interact with the HttpSession directly. To do so,
create a binding like this one in your module:
        bind(MyService.class).toProvider(new Provider<MyService>() {
          @Inject HttpSession session;
          public MyService get() {
            return (MyService) session.getAttribute("myService");
          }
        });

Guice's built-in support is preferred since it provides more type safety (no casts!).

Status: WontFix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant