将来自JSF托管bean的请求转发到Struts 2操作不起作用

时间:2013-04-17 18:38:00

标签: jsf servlets struts2 forward

在JSF托管bean中,我从this past question获取以下代码:

String uri = "/myAction";
FacesContext.getCurrentInstance().getExternalContext().dispatch(uri);

myAction是我struts.xml中定义的Struts操作的名称。

但是当我访问JSF bean时,我没有转发到/myAction。相反,我收到了404 http错误:The requested resource (/MyApp/myAction) is not available

为什么不起作用?

当我尝试直接从浏览器访问/myAction时,转到http://localhost:8080/MyApp/myAction一切正常。

2 个答案:

答案 0 :(得分:0)

线索在dispatch方法的javadoc中:

“参数: path - 指向资源的上下文相对路径,该路径必须以斜杠(“/”)字符开头“

即它调度到相对于您的应用程序上下文的URL,而不是您的Web服务器根文件夹

您需要的是重定向到绝对网址的redirect方法。

答案 1 :(得分:0)

web.xml中设置Struts 2过滤器以接受转发(和直接请求(或重定向)):

web.xml中,更改:

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

要:

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>

    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    <!-- uncomment if you plan to include struts action output -->
    <!--
    <dispatcher>INCLUDE</dispatcher>
    -->
</filter-mapping>

信用:

http://www.coderanch.com/t/59584/Struts/request-dispatcher-struts-action

http://docs.oracle.com/cd/B32110_01/web.1013/b28959/filters.htm#BCFIEDGB

相关问题