1

Within a JSF managed bean, I have the following code taken from this past question:

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

myAction is the name of a Struts action that is defined in my struts.xml.

However when I access the JSF bean, I am not forwarded to /myAction. Instead I'm getting a 404 http error: The requested resource (/MyApp/myAction) is not available.

Why doesn't it work??

When I try to access /myAction directly from a browser by going to http://localhost:8080/MyApp/myAction all works fine.

Community
  • 1
  • 1
rapt
  • 11,810
  • 35
  • 103
  • 145

2 Answers2

0

The clue is in the javadoc for the dispatchmethod:

"Parameters: path - Context relative path to the specified resource, which must start with a slash ("/") character"

i.e it's dispatching to a url relative to your application context,not your webserver root folder

What you need is the redirect method which redirects to an absolute url.

Steve Atkinson
  • 1,219
  • 2
  • 12
  • 30
  • Why does it look to you like the url I used (/myAction), in order for it to work, should be relative to the webserver root folder (`http://mydomain.com`)? It should be relative to the application context (`http://mydomain.com/MyApp`). And as you said, it is. So it should have worked, if I get you right. It does work with xhtml files, that are also found at the root of the application context, such as `/welcome.xhtml`. Did I misunderstand what you meant? – rapt Apr 17 '13 at 20:33
  • For some reason, I thought the source of the problem is that the JSF managed bean lives within a servlet (the JSF servlet), while the Struts action lives within a filter (the Struts 2 filter). And filters are called after all the servlets are completed. So it seems like if I forward from a JSF bean to a framework that is built around a filter, it cannot just work... since there is no filter yet as long as the JSF bean is alive. Or do I misunderstand the life cycle? – rapt Apr 17 '13 at 20:39
  • ahh, I'm sorry. the dangers of posting when tired. I saw "When I try to access /myAction" and thought you were not accessing via application context. In which case, yeah this is odd...but I haven't used struts so I don't know how struts filters should be accessed. the dispatch method though in the servlet api is expecting to land on another servlet I beleive (thats howI've always used them) -so I bet it is because the struts is a filter. Does using redirect work for you? – Steve Atkinson Apr 17 '13 at 21:00
  • I believe redirect wold work, since under the hood it's just two separate requests, the second is launched by the browser. The problem is that after the redirection, I lose the previous request & response data. Any trick how I get out of this deadlock? When I submit a form, I must first let the request pass thru the (legacy) Struts action - then I need to somehow forward it to a JSF managed bean, in order to let JSF create the result view. I should submit the form by POST, not GET, unfortunately. – rapt Apr 18 '13 at 00:25
  • I have seen in the following thread https://forums.oracle.com/forums/message.jspa?messageID=7185500#7185500 they suggest doing what I originally did: forward to the struts action from a managed bean (the guy seems to have a mistake in his 2nd code snippet). However I am not sure he actually tested it. It seems very "reasonable" for it to work, but it does not for me. – rapt Apr 18 '13 at 10:24
0

Set the Struts 2 Filter in web.xml to accept forwards (and direct requests (or redirects)):

In your web.xml, change:

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

To:

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

Credit:

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

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

rapt
  • 11,810
  • 35
  • 103
  • 145