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

  • SEARCH
  • Home
  • 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 6677183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:05:09+00:00 2026-05-26T04:05:09+00:00

I have a session attribute : user, and I have a url that I

  • 0

I have a session attribute : user, and I have a url that I want to be viewed by both logged in users and publically by people not logged in as a user.

So what I want to do is this :

@Controller("myController")
@SessionAttributes({"user"})
public class MyController {

@RequestMapping(value = "/MyPage/{id}", method = RequestMethod.GET)
public ModelAndView getPage(@PathVariable Integer id) {
   return modelandview1;
}

@RequestMapping(value = "/MyPage/{id}", method = RequestMethod.GET)
public ModelAndView getPage(@PathVariable Integer id, @ModelAttribute User user){
  return modelandview2;
}

However, I have a feeling its not going to work … suggestions very welcome.

  • 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-05-26T04:05:09+00:00Added an answer on May 26, 2026 at 4:05 am

    I don’t think it’s a right case for @SessionAttributes. This annotation is usually used to keep original instance of a form-backing object, to avoid passing irrelevant parts of its state via hidden form fields.

    Your sceanrio is completely different, thus it would be better to use HttpSession explicitly:

    @RequestMapping(value = "/MyPage/{id}", method = RequestMethod.GET)
    public ModelAndView getPage(@PathVariable Integer id, HttpSession session) {
        User user = (User) session.getAttribute(...);
        if (user != null) {
            ...
        } else {
            ...
        }
    }
    

    Also note that @ModelAttribute is a subject to data binding – user can change its fields by passing request parameters. You definitely don’t want it in this case.

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

Sidebar

Related Questions

I have a class that defines the names of various session attributes, e.g. class
I have a session class that needs to store session information in a MySQL
Weired problem! ASP.NET Session expires instantly. In my web.config I have this session settings:
I have an NHibernate session. In this session, I am performing exactly 1 operation,
We have various related session variables for a complex page that has various things
I have a ASP.NET user control which hosts a 'HtmlImage'. The src attribute is
Can I rewrite the session attribute WW_TRANS_I18N_LOCALE in Struts2 ? I want to set
I have a j2ee webapp that's being used internally by ~20-30 people. There is
I've read that to use the attribute [Authorize] on MVC, you just have to
I have a tag object that is loaded dinamically by javascript. This tag is

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.