处理jsp中的浏览器“后退"按钮 [英] Handling the browser 'Back' button in jsp

查看:71
本文介绍了处理jsp中的浏览器“后退"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jsp搜索页面(Search.jsp)和一个结果页面(Result.jsp),它们都可以选择搜索条件.然后将参数传递给Java控制器文件(Controller.java)以构建查询字符串并执行查询搜索.查询字符串和搜索结果将传递到Result.jsp进行显示.

I have a jsp search page (Search.jsp) and a result page (Result.jsp), both of them can choose search criteria. and then passed the parameters to a java controller file (Controller.java) to build a query string and performs query searching. The query string and searched results will be passed to Result.jsp for displaying.

当前,我使用servletContext来记住已处理的查询字符串,如果用户使用Result.jsp选择搜索条件,Controller.java会将这些条件附加到现有查询字符串中.如果我使用Result.jsp进行了一些搜索.例如,查询字符串将在Result.jsp页面上显示((Query1) AND Query2) AND Query3.然后使用浏览器的后退"按钮返回到上一个显示页面.对于同一示例,查询字符串显示(Query1) AND Query2.然后,如果我再次搜索.将使用查询字符串(((Query1) AND Query2) AND Query3) AND Query4.我知道这在我当前的实现中是可以预期的,因为Result.jsp不会对已处理的查询字符串进行任何修改.

Currently I use servletContext to remember the processed query string, and if users use Result.jsp to select search criteria, Controller.java will append such criteria to the existing query string. If I do a few searches using Result.jsp. For example, query string would display ((Query1) AND Query2) AND Query3 on the Result.jsp page. Then using the browser's back button to go back to the previous display page. For the same example, query string displays (Query1) AND Query2. Then if I do search again. The query string (((Query1) AND Query2) AND Query3) AND Query4 would be used. I know this is expected with my current implementation since Result.jsp does not do any modification with the processed query string.

但是,我想当用户使用浏览器的后退"按钮时,例如,查询字符串在页面上显示为(Query1) AND Query2,并执行搜索,查询字符串应为((Query1) AND Query2) AND Query4,其中基于该查询字符串在Result.jsp页面上当前显示的查询字符串上加上当前选择.我怎样才能做到这一点?听起来很简单,但是我尝试了几种使用Result.jsp中的方法更新查询字符串的方法,但仍然无法正确执行.因此,我想知道我使用<c:set>的方法是否错误.我想听听你的建议.预先感谢.

However, I would like when user uses the browser Back button, for example, query string displays on the page as (Query1) AND Query2, and perform search, the query string should be ((Query1) AND Query2) AND Query4 in which the query string is build based on the current displayed query string on the Result.jsp page plus the current selection. How can I do that? It sounds quite simple but I have tried several ways of using the in Result.jsp to update the query string, but still couldn't get it right. Therefore I am wondering maybe my approach of using <c:set> is wrong. I would like to hear your suggestion. Thanks in advance.

推荐答案

当前,我使用servletContext来记住已处理的查询字符串

Currently I use servletContext to remember the processed query string

您是否意识到ServletContext在访问您的Web应用程序的所有用户/会话之间共享?访问者X对其进行修改后,所做的更改将反映给所有其他访问者.您是否不想将其存储在HttpSession中以保持特定于数据访问者的数据?

Do you realize that the ServletContext is shared among all users/sessions who are visiting your webapplication? Once visitor X modifies it, the changes are reflected for all other visitors. Don't you rather want to store it in the HttpSession to keep the data visitor-specific?

出于某些原因,我想使用后退按钮

我真的很难理解您的功能需求(某些原因部分).但是至少,由于您希望使后退按钮正常工作,因此您希望在此处使用幂等请求.因此,在适用的情况下,将POST替换为GET.

I really have a hard time in understanding your functional requirement (the some reason part). But at least, since you'd like to have the back button to work properly, you'd like to use idempotent requests here. So, where applicable, replace POST by GET.

每当您要在服务器上重新启动真实的HTTP请求而不是从浏览器的缓存中加载页面时,都想通过向响应添加以下标头来指示浏览器不要缓存页面:

Whenever you'd like to refire a real HTTP request on the server instead of loading the page from browser's cache, you'd like to instruct the browser to not cache the pages by adding the following headers to the response:

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0); // Proxies.

您可以在控制器中执行此操作,也可以在涉及目标url-pattern的某些Filter中执行此操作.

You can do that in your controller or some Filter covering the url-pattern of interest.

  • Make sure that a webpage is not cached across all browsers
  • Webbrowser caching tutorial

这篇关于处理jsp中的浏览器“后退"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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