当前位置 : 主页 > 编程语言 > java >

jsp – 将通知消息设置为请求属性,该属性应在sendRedirect之后显示

来源:互联网 收集:自由互联 发布时间:2021-06-25
我创建了两组servlet:视图和控制器/处理程序 视图:执行简单的读取并将数据转发到JSP 控制器:执行数据库更新或插入,并向JSP或View类型的servlet发送通知 这里的通知是用户的状态消息
我创建了两组servlet:视图和控制器/处理程序

>视图:执行简单的读取并将数据转发到JSP
>控制器:执行数据库更新或插入,并向JSP或View类型的servlet发送通知

这里的通知是用户的状态消息.示例:“您已成功更新了… …”

如果我在控制器中使用requestDispatcher.forward()并且用户刷新(在控制器将控制权传递给view / jsp之后),则通过确认重新发送页面,可能会执行重复操作

如果我使用response.sendRedirect()我似乎无法在会话中设置这些通知而发送任何通知

有一个好的设计实践有帮助吗?任何处理此特定场景的java w / o框架的MVC的良好链接将不胜感激.

我没有使用Spring或Struts – 只是普通的旧HTTPServlet

示例 – 控制器:

public XServlet extends HttpServlet{
     public void processRequest(request, response) throws ...{ 
         //Do some stuff here
         if(success){
             setUserMessage("Hooray ur profile pic is uploaded!");
         } else {
             setUserMessage("Oh no! We couldn't upload that file its too biG!");
         }

         //Send the notification
         request.setAttribute("status", getUserMessage());
         request.getRequestDispatcher("editProfile.jsp").forward(request, response);
    }
}

这样做意味着如果用户尝试刷新页面,控件将再次传递给该控制器,并且可能会不必要地重复某些操作.

但是,如果我使用sendRedirect(),那么无法使用会话属性或将其附加到网址,我就无法显示状态消息.

你正在寻找“ flash scope”.

The flash scope is backed by a short living cookie which is associated with a data entry in the session scope. Before the redirect, a cookie will be set on the HTTP response with a value which is uniquely associated with the data entry in the session scope. After the redirect, the presence of the flash scope cookie will be checked and the data entry associated with the cookie will be removed from the session scope and be put in the request scope of the redirected request. Finally the cookie will be removed from the HTTP response. This way the redirected request has access to request scoped data which was been prepared in the initial request.

在普通的Servlet术语中,如下所示:

>创建Flash范围并添加条目:

String message = "Some message";

// ...

Map<String, Object> flashScope = new HashMap<>();
flashScope.put("message", message);

>在重定向之前,将其存储在由唯一ID键入的会话中,并将其设置为cookie:

String flashScopeId = UUID.randomUUID().toString();
request.getSession().setAttribute(flashScopeId, flashScope);
Cookie cookie = new Cookie("flash", flashScopeId);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);

// ...

response.sendRedirect(request.getContextPath() + "/someservlet");

>在下一个请求中,找到flash cookie,将其映射回请求范围并删除co​​okie:

if (request.getCookies() != null) {
    for (Cookie cookie : request.getCookies()) {
        if ("flash".equals(cookie.getName())) {
            Map<String, Object> flashScope = (Map<String, Object>) request.getSession().getAttribute(cookie.getValue());

            if (flashScope != null) {
                request.getSession().removeAttribute(cookie.getValue());

                for (Entry<String, Object> entry : flashScope.entrySet()) {
                    request.setAttribute(entry.getKey(), entry.getValue());
                }
            }

            cookie.setValue(null);
            cookie.setMaxAge(0);
            cookie.setPath(request.getContextPath());
            response.addCookie(cookie);
        }
    }
}

这可以使用特定于上下文的辅助方法(如setFlashAttribute())和带有响应包装器的servlet过滤器进一步抽象.

网友评论