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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:58:41+00:00 2026-06-02T18:58:41+00:00

Using Spring MVC, is there any way to factorize the org.springframework.ui.Model , in order

  • 0

Using Spring MVC, is there any way to factorize the org.springframework.ui.Model, in order to not to have to specify it in the method parameters within any controller?

In other words, I’m currently doing it like this:

public abstract class AbstractController {

    @Autowired
    protected MultipartHttpServletRequest request;

}

@Controller
public class SigninController extends AbstractController {

    @RequestMapping(value = "/signin", method = RequestMethod.GET)
    public String signin(@ModelAttribute User user, Model model) {
        // do stuff with user (parameter)
        // do stuff with model (parameter) <--
        // do stuff with request (attribute)
        return "/signin/index";
    }

}

And I would like to do like that:

public abstract class AbstractController {

    @Autowired
    protected MultipartHttpServletRequest request;

    @Autowired
    protected Model model;

}

@Controller
public class SigninController extends AbstractController {

    @RequestMapping(value = "/signin", method = RequestMethod.GET)
    public String signin(@ModelAttribute User user) {
        // do stuff with user (parameter)
        // do stuff with model (attribute) <--
        // do stuff with request (attribute)
        return "/signin/index";
    }

}

But when calling the URL, an exception is thrown:

...Could not autowire field: protected org.springframework.ui.Model...
...No matching bean of type [org.springframework.ui.Model] found for dependency...

I got the same error when using org.springframework.ui.ModelMap.

Any solution of genious?

Thanks for helping 🙂

  • 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-02T18:58:42+00:00Added an answer on June 2, 2026 at 6:58 pm

    I finally found a solution. I’m not sure the game is worth it, but it works 🙂

    First, add those stuff in your AbstractController:

    public abstract class AbstractController {
    
        @Autowired
        protected MultipartHttpServletRequest request;
    
        protected ModelMap model;
    
        public void setModel(ModelMap model) {
            this.model = model;
        }
    
        public ModelMap getModel() {
            return model;
        }
    
    }
    

    Then, create an interceptor implementing org.springframework.web.servlet.HandlerInterceptor like this one:

    public class UserContextInterceptor implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
            if (handler instanceof AbstractController) {
                AbstractController controller = (AbstractController) handler;
                controller.setModel(new ModelMap());
            }
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
            if (handler instanceof AbstractController && modelAndView != null) {
                AbstractController controller = (AbstractController) handler;
                modelAndView.addAllObjects(controller.getModel());
            }
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        }
    
    }
    

    Finally, add these lines in your applicationContext.xml:

    <mvc:interceptors>
        <bean class="path.to.my.just.created.UserContextInterceptor" />
    </mvc:interceptors>
    

    And of course, make your controllers implementing your AbstractController.

    Here it is! You need to specify neither your request nor your model within your controllers methods parameters anymore 🙂 I’m not really convinced of the usefulness of that trick though, but yeah. If it can make maniacal developers happier 🙂

    Still open to easier solutions though.

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

Sidebar

Related Questions

I'm using spring MVC, and I have a custom authentication/security system that I had
On my current project we're using Spring 3 MVC and have a requirement to
Is there any way I can pre-process a PHP view script without using a
I'm using Spring MVC 3 + Tiles for a webapp. I have a slow
I am using Spring MVC with annotation configuration. I have a controller class for
I am using Hibernate in Spring MVC 3.05 and an Oracle database. I have
I'm building a webapp using spring MVC and am curious as to whether there
Is there any way to have multiple actions with different parameters? I've seen it
I have a Spring 3 MVC website using Spring Security 3.1.0RC2. Currently I am
I'm using Spring MVC and my jsp's have a <div id=debug> section that I

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.