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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:17:49+00:00 2026-05-25T12:17:49+00:00

I have a controller with 2 methods that return related objects via the @ModelAttribute

  • 0

I have a controller with 2 methods that return related objects via the @ModelAttribute annotation:

@ModelAttribute("site")
public Site getSite(){
.....
return site;
}

@ModelAttribute("document")
public Document getDocument(){
.....
return document;
}

These objects are related to each other with one Site having many Documents. This relationship is mapped in JPA. Both of these objects contain a field with the same name, called “urlAlias”. This field is edited on a page using the following freemarker markup:

<@spring.bind "document" />
....
<@spring.formInput "document.urlAlias" />

When I submit the form to the controller, I retrieve the document object using the following syntax:

@RequestMapping(method = RequestMethod.POST)
    public ModelAndView create(@ModelAttribute("document") @Valid Document document, BindingResult documentResult,
            @ModelAttribute("site") Site site, Model model){
...Do Stuff...
}

It appears that any value that I enter into the Document’s urlAlias field has also been set in the Site object, even though I only edited the value of the field in the Document object.

I’m perplexed as to what is going on here. Am I doing something untoward by mapping more than one ModelAttribute in the same controller? Are there any other likely causes of this behaviour?

  • 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-25T12:17:49+00:00Added an answer on May 25, 2026 at 12:17 pm

    It would appear that the problem is the site parameter in the create() method in my controller:

    @ModelAttribute("site") Site site
    

    Removing that stops Spring binding to fields in that object. For future googlers, I get hold of the Site object within the create() method using the code below instead:

    if (!model.containsAttribute("site")) {
        throw new IllegalArgumentException("Model must contain site attribute.");
    }
    Site site = (Site) model.asMap().get("site"); 
    

    From this it would appear that it is fine to declare more than one ModelAttribute in a controller, but only one can be used at a time as a parameter in a method.

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

Sidebar

Related Questions

I have a few methods inside my view controller that load up for future
I have a Pylons controller action that needs to return a file to the
I have a controller method that returns a jSON object and in one calling
i have a quetion about MVC routing. I have a controller method that is
I have an annotated controller with a method that expects a model and a
In Ruby on Rails, I have an update method in a controller, that is
I would like to have 4 actions with the same name (controller methods may
I have a model called Details, and two controller methods new and create. New
I have a Controller with two Edit methods (see below). When I submit the
ASP.Net MVC Controllers have several methods of forwarding control to another controller or action.

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.