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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:46:54+00:00 2026-05-30T01:46:54+00:00

I have a Spring MVC controller and when an exception occurs I would like

  • 0

I have a Spring MVC controller and when an exception occurs I would like to show the exception message in the view and rollback the open transactions. The view contains a form like this:

<form:form method="POST" modelAttribute="registrationForm">
    <form:errors path="*" cssClass="error-message"/>
    ...
</form:form>

I would like to show the exception message in the view, using the <form:errors ... /> feature. This is my current quite horrible solution:

@RequestMapping(value = "/registration", method = RequestMethod.POST)
public ModelAndView submitForm(@ModelAttribute("registrationForm") RegistrationForm registrationForm,
                         BindingResult result,
                         ModelAndView modelAndView,
                         HttpServletRequest request) throws Exception
{
    registrationValidator.validate(registrationForm, result);

    if(result.hasErrors())
    {
        return setupForm(modelAndView, registrationForm);
    }
    else
    {
        try
        {
            // ... Some non-transactional operations...

            // The following operation is the only one annotated with @Transactional
            // myExampleDao is @Autowired, can throw exception
            myExampleDao.createFoo(bar);

            // ... Other non-transactional operations...

            return new ModelAndView("redirect:successful");
        }
        catch(Exception e)
        {
            throw new RegistrationException(e, registrationForm, result);
        }
    }
}

@ExceptionHandler(value = RegistrationException.class)
public ModelAndView registrationExceptionHandler(RegistrationException e) throws Exception
{
    RegistrationForm registrationForm = e.getRegistrationForm();
    BindingResult result = e.getBindingResult();

    result.reject("exception", e.getMessage());
    Map<String, Object> model = result.getModel();
    return setupForm(new ModelAndView("registration", model), registrationForm);
}

private ModelAndView setupForm(ModelAndView modelAndView, RegistrationForm registrationForm) throws Exception
{
    Map<String,Object> model = modelAndView.getModel();
    model.put("currentYear", Calendar.getInstance().get(Calendar.YEAR));
    return new ModelAndView("registration", model);
}

The problem I’m facing is that when the exception is thrown, the transaction is not rolled back.

Can anyone help?

Thank you.

Update: slightly changed the question for better understanding

Update: found a quite horrible solution to display the exception message in the view. Still facing the problem with the transaction that is not rolled back when the exception is thrown.

Update: I changed @Transactional to @Transactional(rollbackFor = Exception.class) in the MyExampleDao.createFoo(...) method and now everything is working perfectly. This solution is still ugly IMO, does anyone have a better solution?

  • 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-30T01:46:55+00:00Added an answer on May 30, 2026 at 1:46 am

    No one suggested a better solution than my ugly one. Here is my solution that solved the problem I had:

    @RequestMapping(value = "/registration", method = RequestMethod.POST)
    public ModelAndView submitForm(@ModelAttribute("registrationForm") RegistrationForm registrationForm,
                             BindingResult result,
                             ModelAndView modelAndView,
                             HttpServletRequest request) throws Exception
    {
        registrationValidator.validate(registrationForm, result);
    
        if(result.hasErrors())
        {
            return setupForm(modelAndView, registrationForm);
        }
        else
        {
            try
            {
                // ... Some non-transactional operations...
    
                // The following operation is the only one annotated with @Transactional
                // myExampleDao is @Autowired, can throw exception
                myExampleDao.createFoo(bar);
    
                // ... Other non-transactional operations...
    
                return new ModelAndView("redirect:successful");
            }
            catch(Exception e)
            {
                throw new RegistrationException(e, registrationForm, result);
            }
        }
    }
    
    @ExceptionHandler(value = RegistrationException.class)
    public ModelAndView registrationExceptionHandler(RegistrationException e) throws Exception
    {
        RegistrationForm registrationForm = e.getRegistrationForm();
        BindingResult result = e.getBindingResult();
    
        result.reject("exception", e.getMessage());
        Map<String, Object> model = result.getModel();
        return setupForm(new ModelAndView("registration", model), registrationForm);
    }
    
    private ModelAndView setupForm(ModelAndView modelAndView, RegistrationForm registrationForm) throws Exception
    {
        Map<String,Object> model = modelAndView.getModel();
        model.put("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return new ModelAndView("registration", model);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm starting with Spring Web MVC. I have very simple controller and view, but
I'm writing my Exception Handler from Spring MVC controller and I have the following
I have a controller (Spring-MVC) that generates a PDF report on a get request.
I have a Spring Web MVC application that I'd like to serve a large,
I have a website that uses JSP as its view technology (and Spring MVC
I have a Spring MVC controller with an action that's called using AJAX. @SessionAttributes({userContext})
I am using spring 3 mvc/security frameworks. I have created a Controller class that
I am developing my application using spring-web-mvc... Now at my Controller it returns like
I am using Spring MVC with annotation configuration. I have a controller class for
I have a Spring MVC controller annotated with @Transaction , and under certain inputs

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.