发送HttpServletrequest到Faces Servlet

时间:2018-10-19 13:22:16

标签: java jsp jsf myfaces

public void dispatch1(String path) {
        HttpServletRequest request = (HttpServletRequest).getRequest();
        boolean dontRender = false;
        if (request.getSession().getAttribute("dontRender") != null) {
            dontRender = (boolean) request.getSession().getAttribute("dontRender");
        }
        RequestDispatcher requestDispatcher = request.getRequestDispatcher(path);
        HttpServletResponse response = (HttpServletResponse) getResponse();
        ServletResponse response = response;

        FacesContext facesContext = FacesContext.getCurrentInstance();

        try {
            if (response.isCommitted()) {
                requestDispatcher.include(request, response);
            } else {
                if (!dontRender) {

                    requestDispatcher.forward(request, response);

                } else {
                    // Dont render response
                    // create a new httpservlet request with savebutton clicked and send to faces
                    // servlet
                    Runnable runner = new Runnable() {
                        public void run() {
                            HttpServletRequest req = (HttpServletRequest).getRequest();
                            HttpServletResponse resp = (HttpServletResponse) getResponse();
                            req.setAttribute("Save", "Submit Q");
                            req.setAttribute("action", "test2.jsf");

                            try {
                                req.getRequestDispatcher("test1.jsf").forward(req, resp);

                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (ServletException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                    };
                    Thread thread = new Thread(runner);
                    thread.start();
                }

            }
        }

        catch (ServletException e) {
            e.printStackTrace();
            throw new FacesException(e);
        }
    }
test1.jsp
    <input name="Save" id="Save"  type="submit" value="Submit Q">

如果设置了dontRender,我不想将响应发送给客户端,但是我想创建一个HttpServletRequest消息,该消息与用户单击test1.jsp中的save按钮然后将其发送到面对servlet。不确定这是否正确,因为我得到了空指针异常?

0 个答案:

没有答案