如何在行动方法中将请求转发到非JSF页面? [英] How to forward a request to non-JSF page in action method?

查看:97
本文介绍了如何在行动方法中将请求转发到非JSF页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将请求从JSF操作方法转发到非JSF页面. 我在JSF操作中使用以下代码:

public String submitUserResponse() {
    // ...

    parseResponse("foo.jsp", request, response);

    // ...

    return "nextpage";
}  

private String parseResponse(String uri, HttpServletRequest request, HttpServletResponse response) {
    if (uri != null) { 
        RequestDispatcher dispatcher = request.getRequestDispatcher(uri);
        dispatcher.forward(request, response);
        return null;
    }

    // ...

    return "xxxx";
}

当用户从JSF页面单击提交"按钮时,将调用submitUserResponse()动作方法,并且该方法返回nextpage字符串.这里,请求按正常流程转发到下一个JSF页面.但是,根据我的要求,我需要将请求转发到下一个非JSF页面.它正在运行,但在服务器中的异常下方显示.

java.lang.IllegalStateException:提交响应后无法转发

我发现使用dispatched.forward(uri)转发我的请求后,parseResponse(...)return "nextpage";之间的代码行仍在执行. response.sendRedirect(url)也发生了同样的事情.这是怎么引起的,我该如何解决?

解决方案

我的疑问是:1.为什么在使用dispatched.forward(uri)转发我的请求后执行下一行代码.在response.sendRedirect(")中发生了同样的事情.

因为您没有调用return来跳出方法块. include()forward()sendRedirect()确实没有某些 magic ,它们可以自动执行.那些仍然像其他方法一样只是Java方法(当然System#exit()除外).它们将按顺序调用,并且代码将继续直到方法块或return语句结束.这完全是您自己编写和控制代码流的过程.

也就是说,JSF的常规做法是您应该使用

仅此而已.无需从JSF底层挖掘不必要的Servlet请求/响应.请注意,该方法声明为void.这是完全可以接受的,尽管一些类似IDE的知识渊博的人会抱怨它,如果是的话,那就忽略它或替换为String并添加return null;.

另请参见:

I want to forward request to a non-JSF page from JSF action method. I am using below code in my JSF action :

public String submitUserResponse() {
    // ...

    parseResponse("foo.jsp", request, response);

    // ...

    return "nextpage";
}  

private String parseResponse(String uri, HttpServletRequest request, HttpServletResponse response) {
    if (uri != null) { 
        RequestDispatcher dispatcher = request.getRequestDispatcher(uri);
        dispatcher.forward(request, response);
        return null;
    }

    // ...

    return "xxxx";
}

The submitUserResponse() action method is being called when user clicks the submit button from the JSF page and this method returns nextpage string. Here the request forwards to next JSF page in normal flow. But in my requirement, I need to forward request to next non-JSF page. It is going, but it is displaying below exception in server.

java.lang.IllegalStateException: Cannot forward after response has been committed

I observed that code lines between parseResponse(...) and return "nextpage"; are still being executed after forwarding my request using dispatched.forward(uri). Same thing happened with response.sendRedirect(url). How is this caused and how can I solve it?

解决方案

Here my doubts are: 1.why next lines of code is being executed after forwarding my request using dispatched.forward(uri) . Same thing happening in response.sendRedirect("").

Because you didn't call return to jump out of the method block. The include(), forward() or sendRedirect() really doesn't have some magic that they automagically does that. Those are still just Java methods like any other (except of System#exit() of course). They will be invoked in order and the code will just continue until end of method block or return statement. It's just all about the code flow you write and control yourself.

That said, the normal JSF practice is that you should use ExternalContext#dispatch() or ExternalContext#redirect() for this (the first is applicable in your case). Not only it keeps your code free from unnecessary "under-the-hood" clutter in JSF code such as the Servlet API, but it also removes the need to call FacesContext#responseComplete() which you could also have done to fix your initial IllegalStateException problem.

In a nutshell: replace your code by

public void submitUserResponse(){
    String uri = "foo.jsp";
    FacesContext.getCurrentInstance().getExternalContext().dispatch(uri);
}  

That's all. No need to unnecessarily dig the Servlet request/response from under the JSF hoods. Note that the method is declared void. This is perfectly acceptable, although some know-it-better like IDE's will complain about it, if so, then just ignore it or replace by String and add return null;.

See also:

这篇关于如何在行动方法中将请求转发到非JSF页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆