如何在刷新时防止重复的条目? [英] How to prevent duplicate entries while refreshing?

查看:89
本文介绍了如何在刷新时防止重复的条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 index.jsp 页面,其中我有一个表单,用户输入一些数据,使用Controller servlet存储在数据库中。



我想在输入数据库中的数据后显示相同的页面( index.jsp )。我试图使用 forward() RequestDispatcher 的方法。它工作正常(这意味着我能够再次显示相同的表单,并显示该用户输入的所有数据使用JSTL下的表单)。



但问题是无论何时用户按下 Refresh或F5 按钮,所有以前的数据也会被输入到数据库中,并且正在显示所有数据,并且重复的条目也出现了。



我想过使用POST-REDIRECT-GET模式,但问题在于重定向时我没有使用JSTL显示这些数据。



我该怎么做?

解决方案


我想到了使用POST-REDIRECT-GET模式,但是问题在于当我重定向时,我没有使用JSTL显示这些数据。


只需发送一个请求参数,以确定您希望在新的GET请求中显示的信息。

  // ... 
Long id = dataService.save(d ATA);
// ...
response.sendRedirect(request.getContextPath()+/ index?editId =+ id);

然后在 servlet ,它映射到 / index

  @Override 
protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
Long editId = Long.valueOf(request.getParameter(editId)); //自己处理nullcheck。
Data editData = dataService.find(editId);
request.setAttribute(editData,editData); //用于编辑表单。
清单<资料> allData = dataService.list();
request.setAttribute(allData,allData); //用于所有数据的表格/列表。
request.getRequestDispatcher(/ index.jsp)。forward(request,response);
}


I have an index.jsp page where I have a form where the user enters some data that gets stored in the database using a Controller servlet.

I want to display the same page (index.jsp) with that form after entering the data in the database. Also, I want to display all the entries that the user entered in the database.

I tried using the forward() method of RequestDispatcher. It works fine (meaning I am able to display that same form again and also display all the data entered by that user below the form using JSTL).

But the problem is whenever the user presses the Refresh or F5 button, all the previous data also gets entered in the database and as I am displaying all the data, and that duplicate entries also come up.

I thought of using the POST-REDIRECT-GET pattern, but the problem is when I redirect I don't get those data to be displayed using JSTL.

How do I do it?

解决方案

I thought of using POST-REDIRECT-GET pattern but the problem is when I redirect I don't get those data to be displayed using JSTL.

Just send a request parameter along identifying the information you'd like to display in the new GET request.

// ...
Long id = dataService.save(data);
// ...
response.sendRedirect(request.getContextPath() + "/index?editId=" + id);

and then in the servlet which is mapped on an URL pattern of /index

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Long editId = Long.valueOf(request.getParameter("editId")); // Handle nullcheck yourself.
    Data editData = dataService.find(editId);
    request.setAttribute("editData", editData); // For the edit form.
    List<Data> allData = dataService.list();
    request.setAttribute("allData", allData); // For the table/list of all data.
    request.getRequestDispatcher("/index.jsp").forward(request, response);
}

这篇关于如何在刷新时防止重复的条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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