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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:12:34+00:00 2026-05-18T11:12:34+00:00

Imagine a code like this one: @RequestMapping(value=/users, method=RequestMethod.GET) public String list(Model model) { …

  • 0

Imagine a code like this one:

@RequestMapping(value="/users", method=RequestMethod.GET)
public String list(Model model) {
    ...
}

@InitBinder("user")
public void initBinder(WebDataBinder binder) {
    binder.setDisallowedFields("password"); // Don't allow user to override the value
}

@ModelAttribute("user")
public User prepareUser(@RequestParam("username") String username){
    ...
}

@RequestMapping(value="/user/save", method=RequestMethod.POST)
public String save(@ModelAttribute("user") User user, Model model) {        
    ...
}

I use an init binder to avoid that a field can be binded and I mark a method (prepareUser()) with @ModelAttribute to prepare my User object before it is binded. So when I invoke /user/save initBinder() and prepareUser() are executed.

I have set “user” in both @InitBinder and @ModelAttribute so Spring-MVC could understand that this methods should only be applied before executing a method with @ModelAttribute(“user”).

The problem is that the method annotated with @ModelAttribute(“user”) is executed before every mapped method of this controller. For example if I invoke /users prepareUser is executed before list() method. How can I make that this preparer is only executed before save() method having all the methods in the same controller?

Thanks

  • 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-18T11:12:34+00:00Added an answer on May 18, 2026 at 11:12 am

    That’s not really what @ModelAttribute is for. If you use it as a method parameter, it puts the annotated parameter into the model (that’s fine). If you put it on a method, it’s called every time to provide reference data that every method in the controller should have access to.

    If you want to take control of building up your User object, you have several options. The two that are most obvious to me are:

    1. Use your InitBinder method to add a new custom editor (a PropertyEditor class) for building User objects,
    2. Use the conversion service in Spring 3 to convert string usernames to User objects.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is about memory use and objects in actionscript 2. If I have

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.