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

Convenient access to session and request attributes in controller methods [SPR-13894] #18468

Closed
spring-projects-issues opened this issue Jan 26, 2016 · 5 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jan 26, 2016

Rossen Stoyanchev opened SPR-13894 and commented

The existing @SessionAttributes is for model attributes to be saved in the HTTP session during a controller workflow. Such attributes are temporarily stored and are to be removed once the controller indicates completion of the workflow. There is a need for convenient access to more permanent session attributes managed globally, outside the controller. Currently it's possible to inject the WebRequest, HttpSession, or to use @Value with an EL expression.

A dedicated @SessionAttribute parameter annotation with a required flag would look up and cast the session attribute. The session attribute name will default to the parameter type name since unlike a model attribute, a session attribute could be a simple type. A similar @RequestAttribute would do the same for access to request attributes.


Affects: 4.2.4

Issue Links:

Referenced from: commits e62ada8, 698f923

1 votes, 5 watchers

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 26, 2016

Condor commented

As already mentioned in #10050:

Instead of different annotations (@SessionAttribute, @RequestAttribute) I would prefer a single annotation (e.g. @RequestAttribute) with a "scope" element (with value "request", "session", "globalSession" or "singleton").

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 26, 2016

Rossen Stoyanchev commented

Thanks for the comment. I think "application" might be a better name for ServletContext attributes. That said for such attributes it would also be natural to implement ServletContextAware and pick them up on startup (or later) hence modeling global attributes as class fields request-specific attributes as controller method arguments. The added advantage is that @RequestAttribute and @SessionAttribute provide extra convenience and readability.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 26, 2016

Rossen Stoyanchev commented

For example compare a full usage for a session attribute that is not required and must be disambiguated (never mind more realistic, longer class and variable names):

@RequestAttribute(name="foo1", required=false, scope=AttributeScope.Session) Foo foo
@SessionAttribute(name="foo", required=false) Foo foo

And also the minimum:

@RequestAttribute(scope=AttributeScope.Session) Foo foo
@SessionAttribute Foo foo

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 28, 2016

Marten Deinum commented

Isn't #9676 also solved by this?

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 28, 2016

Rossen Stoyanchev commented

Indeed it is, thanks Marten Deinum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants