代码之家  ›  专栏  ›  技术社区  ›  Dmitris

从jsf重定向?

  •  10
  • Dmitris  · 技术社区  · 17 年前

    我正在为我的大学项目使用jsp、jstl和jsf开发应用程序,也就是说,我对jsf还很陌生。

    到目前为止一切都很顺利。然而,我似乎有一个问题,即如何使用双戊基参数从托管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;
            }
    

    提前谢谢你。

    4 回复  |  直到 15 年前
        1
  •  23
  •   Dmitris    17 年前

    以防有人遇到同样的问题。

    这就解决了我的问题:

    FacesContext.getCurrentInstance().getExternalContext().redirect("article.jsp?article_id=" + articleId);
    
        2
  •  2
  •   laginimaineb    17 年前

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

    • 您可以使用DateFormat返回评论日期,只要您想要它(将是) 许多的 清洁剂。
    • 如果错误arraylist只包含字符串,请使用泛型( ArrayList<String> )
    • 你怎么处理这些错误?
    • 你对commentname的态度是 非常 危险的。您应该使用白名单而不是黑名单-定义您希望在评论中接受的内容并阻止其他内容。现在有人可以插入 <img> 带有src的标记,该src指向将导致会话劫持的cookie窃取页。
    • 将分派更改为重定向后,在其下面添加一个返回(您应该始终这样做。不这样做可能会导致你现在看到的结果,也就是说你已经在其他地方发送了响应,但是由于你还没有返回,你已经到达了一个你试图发送另一个的地方)。
        3
  •  1
  •   SystemOut    17 年前

    基本上,在调用response.sendRedirect()之前,已经有东西在向客户端发送输出了。一旦某物被发送到浏览器,你就不能重定向或将它们转发到不同的地方。

    通常,应该在请求上下文中尽早处理可能导致重定向或转发的任何场景,以确保在向客户端发送任何数据之前发生重定向。你是不是在用视图中的标签来调用这段代码?

        4
  •  1
  •   Buhake Sindi Tesnep    15 年前
    FacesContext.getCurrentInstance().getExternalContext().redirect("http://www.myUrl.com");
    

    仁和

    推荐文章