Spring Jsps and Jumping to Anchors

2019-06-28 01:55发布

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.

1条回答
趁早两清
2楼-- · 2019-06-28 02:23

Perhaps a RedirectView is an option:

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

Reference:

查看更多
登录 后发表回答