Spring Jsps and Jumping to Anchors

╄→尐↘猪︶ㄣ 提交于 2019-12-23 12:51:09

问题


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:


Perhaps a RedirectView is an option:

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

Reference:

  • 15.5.3.1 RedirectView


来源:https://stackoverflow.com/questions/5170472/spring-jsps-and-jumping-to-anchors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!