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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:33:55+00:00 2026-05-26T17:33:55+00:00

I need help with understanding following example with @ModelAttribute from Spring documentation: (method populatePetTypes()

  • 0

I need help with understanding following example with @ModelAttribute from Spring documentation: (method populatePetTypes())

@Controller
@RequestMapping("/owners/{ownerId}/pets/{petId}/edit")
@SessionAttributes("pet")
public class EditPetForm {

    // ...

    @ModelAttribute("types")
    public Collection<PetType> populatePetTypes() {
        return this.clinic.getPetTypes();
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processSubmit(
            @ModelAttribute("pet") Pet pet,
            BindingResult result, SessionStatus status) {

        new PetValidator().validate(pet, result);
        if (result.hasErrors()) {
            return "petForm";
        }
        else {
            this.clinic.storePet(pet);
            status.setComplete();
            return "redirect:owner.do?ownerId=" + pet.getOwner().getId();
        }
    }

}

I am undestood this like additional “value” which our Model object could get in the whole current controller. Is it true?

I am trying to do some test, by adding that code to my controller:

@ModelAttribute("user")
public Integer getKod(){
    return new Integer(123321);
}

another method is:

@RequestMapping("/register")
public String register(Map<String, Object> map, @ModelAttribute("user") MyUser user, BindingResult result) {
...
}

and now I am try just show “kod” into my form:

<form:form method="post" action="" commandName="user">
(...)
        <form:label path="kod">kod</form:label>:
        <form:input path="kod"/>
(...)
</form:form>

but I got:

java.lang.IllegalArgumentException: java.lang.ClassCastException@229d7c57

please help!

  • 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-26T17:33:56+00:00Added an answer on May 26, 2026 at 5:33 pm

    The annotation means different things depending on where you put it.

    @ModelAttribute on a method tells Spring to put the method’s return vlaue into the Map, under that name. It is not telling it to do property binding on that object. So you’re putting an Integer called “user” in the Map.

    @ModelAttribute on a parameter tells Spring “Find something in the map with this name, and assign it to this method parameter.”

    So you put an Integer in the map under the name “user” then asked Spring to assign to an Object of type MyUser. Of course Integer cannot be cast to MyUser!

    I think you’re confusing the ‘Model’ in the classic MVC sense, which is your MyUser, with Spring’s ModelMap, which is all of the data that’s available as part of the view. When the Spring controller refers to ‘Model’ it means the ModelMap of all things that are relevant to building the screen. Your MyUser object is one entry in that map, but there can be more other things that are also part of the screen in there. In spring-ese we would frequently refer to your MyUser as the ‘Form Backing Object’ because it’s the actual binding target of your form.

    For example, in the snippet you posted, the “types” list of PetTypes is part of the Spring ModelMap. It’s reference data needed to build the view. But it’s not an attribute of the ‘Pet’ object, which is the form backing object.

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

Sidebar

Related Questions

I need some help understanding the Java memory model.The following is a gerneric example
I need help understanding the following. Say I have a display that is 854x480
I need help understanding this type signature which is from the Thrist package. import
I need help in understanding the following C++ code (in a .h file): bool
Hi need a little help understanding the following statement and the logic of what
I need help understanding some C++ operator overload statements. The class is declared like
I need some help understanding this bit of code pre { white-space: pre; white-space:
I'm a SQL noob, and I need a little bit of help understanding the
Need help writing a script downloads data from google insight using c# this is
I need help on this following aspx code aspx Code: <asp:Label ID =lblName runat

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.