【问题标题】:Spring MVC redirection is adding some parameters in the urlSpring MVC 重定向在 url 中添加了一些参数
【发布时间】:2015-07-02 14:46:40
【问题描述】:

我有一个带有以下代码的 spring mvc web 应用程序。当用户未登录时,我将发送一个图块视图。

当用户登录时,我会重定向到特定的 url 模式。

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() throws IOException {
        if (logger.isDebugEnabled()) {
            logger.debug("Requested with /login mapping");
        }

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            List<String> userRoles = AuthenticationUtils.getUserRoles();
            if (userRoles.contains("ROLE_ADMIN")) {
                return "redirect:/admin.html";
            } else if (userRoles.contains("ROLE_USER")) {
                return "redirect:/user.html";
            }
        }
        return "template";
    }

我得到了重定向,但带有一些意想不到的参数。如何删除它们?

http://localhost:8081/app/admin.html?total=48&loggedInUserRoles=207

我尝试了以下网址,但没有成功。

Spring MVC Controller: Redirect without parameters being added to my url

我不知道是哪部分代码添加了参数。

【问题讨论】:

  • 为什么不把这些页面放在一个 JSP 中并使用像这样的返回“nameofjspwithoutextension”。所以 admin.jsp 将返回“admin”;。我也是这样做的,而且效果很好。

标签: java spring jsp spring-mvc redirect


【解决方案1】:

您可以让您的方法返回View 而不是String,然后以某种方式创建RedirectView

RedirectView view = new RedirectView(url);
view.setExposeModelAttributes(false);
return view;

【讨论】:

    猜你喜欢
    • 2010-10-24
    • 1970-01-01
    • 2012-10-26
    • 2014-06-20
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    相关资源
    最近更新 更多