0

i have a scenario where i need to forward the request from struts 2 action to legacy action class which is not Struts 2.Here is my flow

JSP(serverd from struts 2 action under customer namespace)--->Action1(Struts 2 action under customer namespace)--->Action2(Non Struts 2 Action)--->ResultJSP.

Here is my result annotation which actually forwards the request to legacy action

@Result(name = "displayCustomer",location = "legacyAction.do", type = "dispatcher")

I can see my ResultJSP correctly. But issue is that browser is looking for all resources included in that jsp like images,javascript file under namespace i.e customer( which is first action namespace and also the namespace of the JSP page from which request got trigerred). Is it the default behaviour of browser that it will look for page resources(and submit the form) under the path for which request got triggered( provided the path is relative)?

Is there a way i can make browser so that it look for resources relative to the path of actual page served instead of original request path ?

May be query is not crystal clear but i tried my best to put it in words

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • Browser does not know anything about your server-side forwards. To make browser aware of your new request path a redirect might be send. But this will force browser to make a new request. Is this what you are looking for? – efan Mar 03 '13 at 12:38
  • I can not use redirect becoz i am first doing form submission and then forwarding the request. If i take resort of redirect, each form parameter needs to be passed manually again in redirect url. Right? – M Sach Mar 03 '13 at 12:43
  • Is there a way i can redirect the complete request again will all the parameters i have got from orginal request(provided i dont have to make query string again in java from all original request paramters and then do redirect)? – M Sach Mar 03 '13 at 12:45
  • Well this can be tricky, see this for example: http://stackoverflow.com/questions/46582/response-redirect-with-post-instead-of-get What about changing code in legacy action to use absolute path? Or add something to path relative to current request, to redirect to old ones? – efan Mar 03 '13 at 12:48
  • I think there must be some way where we can let browser know about the path to look for resource as in case of forward request (probably there is some field in HTTPservletRequest or in response) but not sure? – M Sach Mar 03 '13 at 13:42
  • Forward is done purely server-side. For browser there's no difference whether you processed request in single jsp, of forwarded to several. Browser will be able to distinguish absolute, document-relative, and site root-relative paths. That's it. So what is the problem you're trying to solve? Links not working in result jsp? – efan Mar 03 '13 at 14:20
  • As i mentioned in my post i am forwarding the request from one action to another which are in different location. Then ultimately resultJSP response is returned from action 2 which includes various js files whose path is relative .The issue i am seeing is that when browser makes request for these included js/image files it appends the namespace of first action(probably because browser will try to resolve the relative path against the path for which initial request was made not for forwarded request). I can make changes in resultjsp as its legacy code and limitations on it. – M Sach Mar 03 '13 at 15:16
  • If this is some external system you can just forward to say : `http://www.google.com` then you type in the search field and then it render a result. If the system is external, just forward to the resource and you no longer have any involvement. – Quaternion Mar 03 '13 at 22:43
  • @MSach: Show how your resources are included. – Aleksandr M Mar 04 '13 at 08:48

1 Answers1

0

I will try to summarize what we discussed in comments. First, be sure to understand what is done on client on what on server side. For document-relative links browser does look up based on what you see in address bar. Here is a long answer describing various ways to create links in jsp. Second there are ways you can instruct browser to change location, e.g. sending redirect. However for redirect(302 http status code) browser will make a GET request. This is not good for you as your are doing POST and want parameters to be redirected as well. But instead of 302 status code a 307 status code can be used. This one is tricky and the link i posted before, although about .net(just the first one popped up when googling on redirects), describes these status codes in more details and also mentions about solution to do redirect without using 302, or 307. This can be done by some client side javascript. Basically instead of forwarding to your second action you would return a dummy jsp with your form and javascript which submits this form to second action immediately on page load.

Now having said all this it seems rather strange for me that js/image files are using links relative to you servlet path. I would make those relative to your site root. And well you can place them there. And in case some legacy action which you cannot change is using other locations you can use something like UrlRewriteFilter

Community
  • 1
  • 1
efan
  • 968
  • 6
  • 11