Java 从jsf重定向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/973732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:45:01  来源:igfitidea点击:

redirect from jsf?

javajspjsfjstljavabeans

提问by Dmitris

I am working on application with jsp, jstl and jsf for my college project, thats being said, I am as well very new to jsf.

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

Everything is going great so far. However, I seems to have a problem figuring out how to do redirect from managed bean to page with dinamyc parameters. For example article.jsp?article_id=2

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

Can somebody tell me how it is done ?

有人能告诉我它是怎么做的吗?

I been trying to use somethinng like

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

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

But get error:

但得到错误:

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)

I been trying to use

我一直在尝试使用

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

But again getting an error.

但是又报错了。

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

Can somebody please tell me how do i redirect from managed java bean when working with jsf ?

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

Bellow is my code (maybe something wrong with that and thats why redirect not working).

波纹管是我的代码(可能有问题,这就是重定向不起作用的原因)。

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;
        }

Thank you in advance.

先感谢您。

回答by SystemOut

Basically, something is already sending output to the client before you make the call to response.sendRedirect(). Once something has been sent to the browser you can't redirect or forward them to a different place.

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

In general, any scenarios that might result in a redirect or a forward should be handled as early as possible in the request context to insure the redirect happens before you send any data to the client. Are you doing something like calling this code via a tag in the view?

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

回答by laginimaineb

Why are you using dispatch in one place and redirect in the other? This isn't the source of the problem - not returning after sending responses, however, is. Other then that, if you don't mind, I have a few friendly suggestions:

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

  • You can use DateFormat to return the comment date as you want it (it will be muchcleaner).
  • If the errors ArrayList only contains Strings, use generics (ArrayList<String>).
  • What are you doing with the errors?
  • Your sanitation of the commentName is verydangerous. You should use whitelisting instead of blacklisting - define what you wish to accept in a comment and block everything else. Right now someone could insert an <img>tag with a src pointing to a cookie stealing page which would result in a session hiHyman.
  • After changing the dispatch to a redirect add a return below it (you should always do this. Not doing this could result in what you're seeing right now, which is that you've already sent a response somewhere else, but since you haven't returned you've reached a place where you're trying to send another).
  • 您可以使用 DateFormat 根据需要返回评论日期(它会清晰)。
  • 如果错误 ArrayList 仅包含字符串,请使用泛型 ( ArrayList<String>)。
  • 你对这些错误做了什么?
  • 你对 commentName 的清理是非常危险的。您应该使用白名单而不是黑名单 - 定义您希望在评论中接受的内容并阻止其他所有内容。现在有人可以插入一个<img>带有指向 cookie 窃取页面的 src的标签,这将导致会话劫持。
  • 将调度更改为重定向后,在其下方添加一个返回(您应该始终这样做。不这样做可能会导致您现在看到的情况,即您已经在其他地方发送了响应,但是因为您没有't 返回,你已经到达了一个你试图发送另一个的地方)。

回答by Dmitris

In case some one will run into same problem.

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

That's what solved my problem:

这就是解决我的问题的原因:

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

回答by Rene

FacesContext.getCurrentInstance().getExternalContext().redirect("http://www.myUrl.com");

René

雷内