如何使用 Thymeleaf 根据上一页动态地使后退按钮转到上一页? [英] How to make back button go to previous page dynamically based on previous page using Thymeleaf?

查看:45
本文介绍了如何使用 Thymeleaf 根据上一页动态地使后退按钮转到上一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个 html 页面(页面 A、B、C),其中页面 A 和页面 B 将有一个指向页面 C 的超链接.在页面 C 中会有一个返回"链接.按钮.

I have 3 html pages (Page A,B,C) where Page A and Page B will have a hyperlink that will direct to Page C. In Page C there will be a "Back" button.

所以问题是,我想要返回"按钮(页面 C)根据用户从(页面 A)或(页面 B)单击超链接的目的地返回上一页.是否可以通过使用 Thymeleaf 而不是在Back"上使用 "javascript:history.go(-1)\" 来实现这一点?按钮?到目前为止,我无法到处找到解决方案.

So the question is, I want the "Back" button (Page C) go back to previous page based on the destination where user clicked the hyperlink from either (Page A) or (Page B). Is it possible to achieve this by using Thymeleaf other than using "javascript:history.go(-1)\" on the "Back" button? So far I couldn't find the solution all over the place.

推荐答案

可以使用referrer请求头:

You can use the referrer request header:

做这样的事情:

public class MyController {

  @GetMapping
  public String myMethod(@RequestHeader(value = HttpHeaders.REFERER, required = false) final String referrer, Model model) {
    if( referrer != null ) {
      model.addAttribute("previousUrl", referrer);
    }
  }
}

请注意,它可能并不总是有效.如果用户在浏览器中直接输入页面C的URL,则没有referrer URL.

Note that it might not always work. If a user directly enters the URL of page C in the browser, then there is no referrer URL.

这篇关于如何使用 Thymeleaf 根据上一页动态地使后退按钮转到上一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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