Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Jsps and Jumping to Anchors

Tags:

spring

anchor

jsp

I was wondering if there is some way in Spring to specify in the controller that I would like to send the client to a specific anchor within the .jsp page that I am using for my view.

I have a section in my .jsp page, identified by an #errors anchor, that displays any form errors that occur. I would like to be able to send them directly to that anchor whenever I need to send them back to the .jsp after model validation fails.

Inside my controller classes, I handle validation errors like so:

if (result.hasErrors())
        {

            for (ObjectError error : result.getAllErrors())
            {

                logger.debug("Validation Error: " + error.getDefaultMessage());
            }

            ModelAndView mv = new ModelAndView();

            mv.setViewName("/secure/formViews/newAdminBookingMenu");
            mv.addObject(BindingResult.MODEL_KEY_PREFIX + "booking", result);

            return mv;
        }

I would like to be able to specify in that code block that when the client gets the rendered newAdminBookingMenu.jsp back that they are sent directly to the #errors anchor tag within that page.

I obviously cannot do this by just adding #errors to the name of the .jsp i wish to render as the InternalResourceViewResolver will interpret the view as /WEB-INF/jsp/jspName#errors.jsp which is clearly incorrect.

I know this can be accomplished with javascript and the onload event but I find that kind of dirty and would really rather find a Spring approach if one exists.

Any ideas?

Cheers.

like image 913
Kristen D. Avatar asked Feb 25 '23 22:02

Kristen D.


1 Answers

Perhaps a RedirectView is an option:

modelAndView.setView(new RedirectView("error/page#errors", true));

Reference:

  • 15.5.3.1 RedirectView
like image 152
Sean Patrick Floyd Avatar answered Mar 07 '23 21:03

Sean Patrick Floyd