在Spring MVC中获取将您重定向到的页面 [英] Get the page that redirected you here, Spring MVC

查看:104
本文介绍了在Spring MVC中获取将您重定向到的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题.我想对应用程序中的每个页面都使用一个控制器.它是每个页面都通过include jsp包含的一种常见形式,它将邮件发送到预定义的电子邮件帐户.问题是,当我发布表单时,尽管被调用的方法实际上是无效的,但我仍被重定向到空白页面,URL是我的RequestMapping值.因此,在将邮件发送到我来自的页面之后,现在我需要重定向我.如何获得重定向到我的页面的URL链接,以发送电子邮件?谢谢

I have the following problem. I want to use a controller, same for every page in my application. It is a common form included via include jsp in every page and it sends a mail to a predefined email account. The problem is, when I am posting the form, I get redirected to a blank page, with the url being my RequestMapping value despite the method called is actually void. So I need now to redirect me, after sending the mail to the page where I came from. How do I get access to the url link of the page that redirected me, into sending the email? Thanks

推荐答案

当返回void并且无法自行编写响应时,Spring MVC委托检测将哪个视图呈现给 RequestToViewNameTranslator 其中只有一个实现 DefaultRequestToViewNameTranslator .它基本上采用传入的URL,去除所有后缀(如.html等)并将其用作视图名.您现在看到的行为.

When returning void and one isn't handling writing the response yourself Spring MVC delegates detection of which view to render to a RequestToViewNameTranslator for which there is a single implementation the DefaultRequestToViewNameTranslator. Which basically takes the incoming URL, strips any suffixes (like .html etc.) and uses that as the viewname. The behavior you see now.

我想您有3种可能的解决方案

I guess you have 3 possible solutions

  1. 添加包含当前页面的隐藏表单属性
  2. 使用引荐来源请求标头确定请求的来源(不是100%解决方案,请参见替代到"Referer"标题)
  3. 通过ajax提交表单,以便您停留在当前页面上

对于选项2,将 HttpServletRequest 作为参数添加到您的请求处理方法中,并检索标头.

For option 2 add the HttpServletRequest as a parameter to your request handling method and retrieve the header.

public String foo(HttpServletRequest request) {
    String referer = request.getHeader("Referer");
    return "redirect:" + referer;
}

我可能会选择选项3,并且在没有可用的javascript时添加选项1作为备用.

I would probably go for option 3 and maybe add option 1 as a fallback when no javascript is available.

这篇关于在Spring MVC中获取将您重定向到的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆