从jsf重定向?

Dmi*_*ris 10 java jsf jsp jstl javabeans

我正在为我的大学项目使用jsp,jstl和jsf进行应用程序,据说,我对jsf也很新.

到目前为止,一切都很顺利.但是,我似乎有一个问题,想知道如何从托管bean重定向到dinamyc参数的页面.例如article.jsp?article_id=2

有人能告诉我它是如何完成的吗?

我一直想尝试使用像

FacesContext.getCurrentInstance().getExternalContext().dispatch("faces/article.jsp2?article_id=" + articleId);
Run Code Online (Sandbox Code Playgroud)

但得到错误:

javax.servlet.ServletException: #{postComment.postClick}: javax.faces.FacesException: javax.servlet.ServletException: javax.faces.component.UIViewRoot cannot be cast to com.sun.faces.application.StateManagerImpl$TreeNode
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
Run Code Online (Sandbox Code Playgroud)

我一直在努力使用

response.sendRedirect("faces/article.jsp2?article_id=" + articleId);
            return;
Run Code Online (Sandbox Code Playgroud)

但又一次出错了.

javax.servlet.ServletException: Cannot forward after response has been committed
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我在使用jsf时如何从托管java bean重定向?

贝娄是我的代码(可能是错误的,这就是为什么重定向不工作).

HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

        String articleId = request.getSession().getAttribute("article_id").toString();
        //String articleId  = request.getParameter("article_id");
        String authorName = request.getSession().getAttribute("user_name").toString();

        java.util.Calendar calendar = java.util.Calendar.getInstance();
        String commentDate = String.valueOf(calendar.get(java.util.Calendar.DAY_OF_MONTH)) + ".";
        commentDate += String.valueOf(calendar.get(java.util.Calendar.MONTH)) + ".";
        commentDate += String.valueOf(calendar.get(java.util.Calendar.YEAR));

         ArrayList error = new ArrayList();

        if(commentName.contains("<"))
        {
            error.add("Comment name contains illegal characters");
        }

        if(commentBody.isEmpty() && commentBody.contains("<script"))
        {
            error.add("Your message body contains illegal characters");
        }

        if(error.size() > 0)
        {
            request.getSession().setAttribute("error", error);
            error.clear();
            FacesContext.getCurrentInstance().getExternalContext().dispatch("article.jsp2?article_id=" + articleId);
        }
        else
        {
            Comment comment = new Comment();
            comment.setCommentAuthor(authorName);
            comment.setCommentBody(commentBody);
            comment.setCommentDate(commentDate);
            comment.setCommentName(commentName);
            comment.setArticleId(articleId);

            DisplayArticleIO addComment = new DisplayArticleIO();
            addComment.postComment(comment);
//            FacesContext.getCurrentInstance().getExternalContext().dispatch("faces/article.jsp2?article_id=" + articleId);
            response.sendRedirect("faces/article.jsp2?article_id=" + articleId);
            return;
        }
Run Code Online (Sandbox Code Playgroud)

先感谢您.

Dmi*_*ris 23

如果有人遇到同样的问题.

这就解决了我的问题:

FacesContext.getCurrentInstance().getExternalContext().redirect("article.jsp?article_id=" + articleId);
Run Code Online (Sandbox Code Playgroud)

  • 说明:它在`HttpServletResponse #sendRedirect()`之后隐式调用`FacesContext #responseComplete()`来指示JSF它不需要呈现响应.换句话说,另一个(更笨拙)的解决方案是在`response.sendRedirect()`行之后调用`FacesContext.getCurrentInstance().responseComplete()`. (13认同)

小智 1

基本上,在调用response.sendRedirect() 之前,某些东西已经将输出发送到客户端。一旦某些内容发送到浏览器,您就无法将它们重定向或转发到其他位置。

一般来说,任何可能导致重定向或转发的场景都应该在请求上下文中尽早处理,以确保在向客户端发送任何数据之前发生重定向。您是否正在执行类似通过视图中的标签调用此代码之类的操作?