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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:06:41+00:00 2026-05-27T10:06:41+00:00

I have created a custom HandlerInterceptorAdapter to override the postHandle method: public class AcmeInterceptor

  • 0

I have created a custom HandlerInterceptorAdapter to override the postHandle method:

public class AcmeInterceptor extends HandlerInterceptorAdapter {

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);

        AcmeController controller = (AcmeController) handler;

        controller.finalize(modelAndView);
    }
}

In the AcmeModel, I define a field annotated with NumberFormat:

public class AcmeModel {
    private BigDecimal cost = BigDecimal.valueOf(67890.6789);

    @NumberFormat(style = Style.CURRENCY)
    public BigDecimal getCost() {
        return cost;
    }
}

In acme.jsp I use <spring:bind> to output the formatted value:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<spring:bind path="acmeModel.cost">
    Cost: <c:out value="${status.value}" />
</spring:bind>

Now, first I try the controller like this:

@Controller
public class AcmeController {

    @RequestMapping("/")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("WEB-INF/views/acme.jsp");
        modelAndView.addObject(new AcmeModel());
        return modelAndView;
    }

    public void finalize(ModelAndView modelAndView) {
    }
}

And this is the output I get:

Cost: $67,890.68

Here is the puzzling part. If I move the call to addObject into the body of finalize:

@Controller
public class AcmeController {

    @RequestMapping("/")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("WEB-INF/views/acme.jsp");
        //modelAndView.addObject(new AcmeModel());
        return modelAndView;
    }

    public void finalize(ModelAndView modelAndView) {
        modelAndView.addObject(new AcmeModel());
    }
}

Then the output becomes:

Cost: 67890.6789

What is the difference between adding an object to ModelAndView in the handler method as opposed to a normal controller method that affects <spring:bind>?

Edit: Here is the bean definition for the servlet.

<beans ...>     
    <mvc:annotation-driven />       
    <context:component-scan base-package="com.example" />       
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean id="acmeInterceptor" class="com.example.numberformat.AcmeInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>
</beans>
  • 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-27T10:06:42+00:00Added an answer on May 27, 2026 at 10:06 am

    Spring handles the formatting via the BindingResult object. When you add values to the model in a controller then spring validates them, creates a BindingResult and adds it to the Model. This mechanism only works for the controllers, not for the interceptors, may it be intentional or not I am not sure but it definitely works like that. If you want binding to be done for values set in an interceptor then you need to do the binding yourslef. Modify your interceptor like this :

    public class AcmeInterceptor extends HandlerInterceptorAdapter {
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {      
    
            AcmeController controller = (AcmeController) handler;
    
            AcmeModel acmeModel = controller.getAcmeModel();
            String key = "acmeModel";
    
            BeanPropertyBindingResult bpb = new BeanPropertyBindingResult(
                    acmeModel, key);
    
            bpb.initConversion(controller.getBinder().getConversionService());
    
            modelAndView.addObject(key, acmeModel);
            modelAndView.addObject(BindingResult.MODEL_KEY_PREFIX + key, bpb);
    
        }
    
    }
    

    as you can see this piece of code adds the extra BindingResult object to the model. It needs to get hold of the ConversionService. For now it is done by getting it from the controller. The Controller can access it using the @InitBinder annotation like this

    public class AcmeController {
    
        private WebDataBinder binder;
    
        @InitBinder
        protected void initBinder(WebDataBinder binder) {
    
            this.binder = binder;
    
        }
    

    An another option is to implement the preHandle method in the interceptor instead of the postHandle. PreHandle can’t access the model, but it can access the request, so in the preHandle you can add AcmeModel to the reqiest as an attribute then in your controller you can get the value, add it to the model, then it will be Bound by Spring just like the rest of the model attributes.

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

Sidebar

Related Questions

i have created a custom dialog class public class NewPost extends Dialog { //
I have created custom aplication resource: class Custom_Entitymanager extends Zend_Application_Resource_ResourceAbstract{ //... public function init
I have created custom exception class public class Web2PDFException : Exception { public Web2PDFException(string
I have created a custom attribute that I am using on my class MyClass
I have created a custom class for my dates and need move dates forward
I have created custom result class to serialize json data to xml. I want
I have created a Custom preference which has the following constructor public CoordinatesPreference(Context context,
I have created a custom ImageView and in its onDraw method I need to
I have created a custom class from a tutorial online. It works fine, but
I have created a custom widget in gwt which extends the composite.I am using

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.