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 8874975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:51:35+00:00 2026-06-14T18:51:35+00:00

I have this class: @Component @Scope(session) @Entity @Table(name = users) public class User {

  • 0

I have this class:

@Component
@Scope("session")
@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue
    @GenericGenerator(name = "incremental", strategy = "increment")
    private Long userID;

    @Column(nullable = false)
    private String username;

    @Column(nullable = false)
    private String email;

    @Column(nullable = false)
    private String password;
    // getters and setters
}

And this controller:

@Controller
@SessionAttributes("user")
@Scope("request")
public class UserCreationWizard {
    @Autowired
    private User user;

    @ModelAttribute("user")
    private User createUser() {
        return user;
    }

    @RequestMapping(value = "/new/users/page/", method = RequestMethod.GET)
    public String begin(HttpServletRequest request) {
        return "wizard"; 
    }

    @RequestMapping(value = "/new/users/page/{page}", method = RequestMethod.POST) 
    public String step(@ModelAttribute("user") User user,
                       @RequestParam("username") String username,
                       @RequestParam("email") String password,
                       @PathVariable() Integer page) {

        return "wizard" + page;
    }

    @RequestMapping(value = "/new/users/page/end", params = "submit", method = RequestMethod.POST) 
    public String end(@RequestParam("password") String password) {

        user.setPassword(password);
        user.setActive(true);
        user.setLastLoggedIn(Calendar.getInstance());

        Session s = HibernateUtils.getSessionFactory().openSession();
        Transaction t = s.beginTransaction();
        try {
            s.persist(user);
            s.flush();
            t.commit();

            s.close();
        } catch (HibernateException e) {
            t.rollback();
        }
        return "wizard";
    }
}

begin() just loads the first view (a jsp) in the user creation wizard. It has input fields for username and email. In the view, you make a POST form submission which triggers step(). In the second view (wizard+page.jsp), you have a password field and a submit input that triggers end().

  1. In debug mode I noticed that in step(), where I’ve passed User as
    a ModelAttribute, I don’t need to set its fields for username and
    password. They are automatically grabbed from the RequestParams
    attributes. In end() however, where I don’t have a ModelAttribute,
    I have to set the password manually. How does Spring manage this?
  2. Also if I take out the createUser() method in the Controller, the
    application fails saying it couldn’t find a session attribute for
    “user”. How is this method linked to MethodAttribute as a method
    parameter?
  3. Lastly, if I take out @SessionAttributes, the application doesn’t fail but I feel like something is going wrong. Will the User user now be global to all httprequests?

My general question is: are spring beans mapped to their name? Eg. Here I have ‘user’ as a user and ‘user’ in the session, ‘password’ as a requestparam and ‘password’ as the User member variable.

  • 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-14T18:51:36+00:00Added an answer on June 14, 2026 at 6:51 pm

    Ok, many questions. Let’s see, all references are to the documentation for the current Spring MVC version.

    1) The behaviour you see in the user attribute is explained in the section “Using @ModelAttribute on a method argument“

    An @ModelAttribute on a method argument indicates the argument should
    be retrieved from the model. If not present in the model, the argument
    should be instantiated first and then added to the model. Once present
    in the model, the argument’s fields should be populated from all
    request parameters that have matching names. This is known as data
    binding in Spring MVC, a very useful mechanism that saves you from
    having to parse each form field individually.

    How does Spring does this? Well, the source code is the ultimate answer but it’s not that hard to make a guess: Spring knows the argument is an instance of User and via reflection it can read the methods of the class, particularly its setters. In this case it finds setUsername() and setEmail() and the argument for those methods is a String so it’s compatible with the parameters from the request.

    (BTW: @RequestParam("email") String password is possibly a mistake. At the very least is confusing)

    2) The method createUser() is preceded by the annotation @ModelAttribute("user"). This is covered by the section “Using @ModelAttribute on a method“

    An @ModelAttribute on a method indicates the purpose of that method is
    to add one or more model attributes.

    Therefore, this method puts an object associated with the name "user" on the model and is then available to be used as a parameter by other methods, like step(). Notice that the annotation controls the identifier used by the object in the model. If you change the code to

    @ModelAttribute("strangeWeirdIdentifier")
    private User createUser() { return user; }
    

    the app will break. But it will work again if you change the step() signature to

    public String step(@ModelAttribute("strangeWeirdIdentifier") User user,
                       @RequestParam("username") String username,
                       @RequestParam("email") String password,
                       @PathVariable() Integer page) {
    

    3) The process described in 1) and 2) stores objects in the model during a request. With the class annotation @SessionAttributes("user") you extend the life of the object adding it to the current Session or something equivalent. By example you could then use the object in other Controllers in the same was as the step() method.

    Finally just to be clear

    • The annotation seen in your question 2 happens before the usage seen in your question 1.
    • The annotation from your question 3 is probably not needed
    • Spring does not map beans to their names in Java code but to the names used in the annotations. Is not uncommon to repeat the same name as in your example for the sake of clarity.

    Hope this manages to be more clear than the official documentation whch, is usually too brief, I’ll give you that.

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

Sidebar

Related Questions

Consider this example @Scope(ScopeType.SESSION) @Name(test) @BypassInterceptors public Class Test { @Unwrap public List<String> test()
The problem is: i have this code: public class Component { public Component() {
I have a Component like this: public partial class FontScalingManager : Component { ...other
I have this class which i would like to map: public class Contract {
i have this class public class Image { public string url { get; set;
I have this class: public static class CsvWriter { private static StreamWriter _writer =
So, given that I have an instance of this component: foo.cfc <cfcomponent> <cffunction name=locateMe>
I have this class mapped as a entity, lets call it Person. Person has
i have this mapping: HasMany<ClassA>(ot => ot.AList) .Table(XPTO) .KeyColumn(IDXPTO) .Component(m => { m.Map(a=> a.X,
I have a MyCanvas class that extends JComponent. On this canvas I have drawn

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.