【问题标题】:redirect from jsf?从jsf重定向?
【发布时间】:2010-11-01 16:54:01
【问题描述】:

我正在为我的大学项目申请 jsp、jstl 和 jsf,也就是说,我对 jsf 也很陌生。

到目前为止一切都很好。但是,我似乎在弄清楚如何使用 dinamyc 参数从托管 bean 重定向到页面时遇到问题。 例如article.jsp?article_id=2

谁能告诉我它是怎么做的?

我一直在尝试使用类似的东西

FacesContext.getCurrentInstance().getExternalContext().dispatch("faces/article.jsp2?article_id=" + articleId);

但得到错误:

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)

我一直在尝试使用

response.sendRedirect("faces/article.jsp2?article_id=" + articleId);
            return;

但又报错了。

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

有人可以告诉我在使用 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;
        }

提前谢谢你。

【问题讨论】:

    标签: java jsp jsf jstl javabeans


    【解决方案1】:

    万一有人遇到同样的问题。

    这就是解决我的问题的方法:

    FacesContext.getCurrentInstance().getExternalContext().redirect("article.jsp?article_id=" + articleId);
    

    【讨论】:

    • 解释:它在HttpServletResponse#sendRedirect() 之后隐式调用FacesContext#responseComplete() 以指示JSF 它不需要呈现响应。换句话说,另一个(更笨拙)的解决方案是自己在response.sendRedirect() 行之后调用FacesContext.getCurrentInstance().responseComplete()
    【解决方案2】:

    为什么你在一个地方使用 dispatch 而在另一个地方使用重定向?这不是问题的根源 - 但是,在发送响应后不返回。除此之外,如果您不介意,我有一些友好的建议:

    • 您可以使用 DateFormat 根据需要返回评论日期(它会更加更简洁)。
    • 如果错误 ArrayList 仅包含字符串,请使用泛型 (ArrayList&lt;String&gt;)。
    • 您如何处理这些错误?
    • commentName 的卫生状况非常很危险。您应该使用白名单而不是黑名单 - 在评论中定义您希望接受的内容并阻止其他所有内容。现在有人可以插入一个带有 src 的 &lt;img&gt; 标记,该标记指向一个 cookie 窃取页面,这将导致会话劫持。
    • 将调度更改为重定向后,在其下方添加一个返回(您应该始终这样做。不这样做可能会导致您现在看到的结果,即您已经在其他地方发送了响应,但是因为您还没有返回,所以您已经到达了一个您正试图发送另一个的地方。

    【讨论】:

    • 您列出了一些非常好的建议。非常感谢,一定会调查的。
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:
      FacesContext.getCurrentInstance().getExternalContext().redirect("http://www.myUrl.com");
      

      勒内

      【讨论】:

        猜你喜欢
        • 2012-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-26
        • 2014-04-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多