Results 1 to 9 of 9
- 07-13-2010, 05:44 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
URL formation for Ajax request in struts.
Hi all
I am building an struts application and using ajax also. I have observed that request url for making ajax request is slightly different.
for example in a normal jsp application the url will be like below
var url = "/authenticate"
but is struts its
var url = "TestCaseRepo/authenticate.do"
I think this is not the right way since we are hard coding the root directory path. If tomorrrow if i deploy in another folder instead of TestCaseRepo then i have to change the url in all the locations in my struts application which is not really good.
I would great if someone could tell me how to resolve this issue, so that my application work if deployed on any folder.
- 07-13-2010, 09:08 AM #2
Member
- Join Date
- Dec 2009
- Location
- Germany
- Posts
- 43
- Rep Power
- 0
TestCaseRepo is your Context Root you can make this generic like this.
If you use Struts2 i recommend to use the <s:url ../> tag to generate the right URL inside your application.Java Code:var url = {pageContext.request.contextPath}/authenticate
- 07-13-2010, 09:31 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
I am using Struts 1.3. I am making an ajax request. Is this
pageContext.request.contextPath possible from ajax. does javascript knows pageContext ?.
I would also like to know why .do is required in the url.
- 07-13-2010, 01:54 PM #4
Member
- Join Date
- Dec 2009
- Location
- Germany
- Posts
- 43
- Rep Power
- 0
when you render your javascript in an JSP the pageContext should also be available.
- 07-13-2010, 02:29 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
Thanks a lot jogep. I have learn t something from you.
I have also found another solution the below code also works
var url = "authenticate.do";
The only thing i dont understand is you do we need to give .do string. if i dont provide it, it doesnt work.
- 07-13-2010, 03:33 PM #6
Member
- Join Date
- Dec 2009
- Location
- Germany
- Posts
- 43
- Rep Power
- 0
take a look in your web.xml!
There is a mapping for all request with suffix *.do to your Struts Servlet.
At this point you can also rename it to another suffix.
- 07-14-2010, 05:51 AM #7
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
Thanks Jogep.
I can understand this but when i submit the form
<html:form action="/login" method="post">
using the above form, the form gets submitted perfectly. My question is
in the above action attribute i have not mentioned action="/login.do".
I would like to know how is the form getting submitted without the .do extension ?.
- 07-14-2010, 08:37 PM #8
Member
- Join Date
- Dec 2009
- Location
- Germany
- Posts
- 43
- Rep Power
- 0
with <html:form action="/login" method="post"> you use a struts tag.
I guess it renders you a <form action="login.do" method="post"> html tag.
I'm not sure but I think you can change the suffix *.do in your web.xml.
- 07-16-2010, 09:42 AM #9
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
decorator.jsp problem building Struts Menu in JSP when Struts action is used
By dkirvan in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 04-29-2010, 05:33 AM -
Struts 2 and Ajax drop down Example
By priyanka.dandekar in forum Web FrameworksReplies: 8Last Post: 01-05-2010, 11:57 AM -
AJAX - Sending a request to a server
By gilbertsavier in forum XMLReplies: 0Last Post: 07-17-2009, 07:35 AM -
Request and repnse in Struts 2
By Inaam in forum Web FrameworksReplies: 0Last Post: 03-31-2009, 07:19 PM -
Error using ajax call in onchange event of select box.... struts 1.x?
By prabhurangan in forum Web FrameworksReplies: 0Last Post: 07-07-2008, 07:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks