Results 1 to 9 of 9
  1. #1
    fierof2 is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

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

  2. #2
    jogep is offline Member
    Join Date
    Dec 2009
    Location
    Germany
    Posts
    43
    Rep Power
    0

    Default

    TestCaseRepo is your Context Root you can make this generic like this.

    Java Code:
    var url = {pageContext.request.contextPath}/authenticate
    If you use Struts2 i recommend to use the <s:url ../> tag to generate the right URL inside your application.

  3. #3
    fierof2 is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

    Default

    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.

  4. #4
    jogep is offline Member
    Join Date
    Dec 2009
    Location
    Germany
    Posts
    43
    Rep Power
    0

    Default

    when you render your javascript in an JSP the pageContext should also be available.

  5. #5
    fierof2 is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

    Default

    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.

  6. #6
    jogep is offline Member
    Join Date
    Dec 2009
    Location
    Germany
    Posts
    43
    Rep Power
    0

    Default

    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.

  7. #7
    fierof2 is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

    Default

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

  8. #8
    jogep is offline Member
    Join Date
    Dec 2009
    Location
    Germany
    Posts
    43
    Rep Power
    0

    Default

    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.

  9. #9
    fierof2 is offline Member
    Join Date
    Jul 2010
    Posts
    24
    Rep Power
    0

Similar Threads

  1. decorator.jsp problem building Struts Menu in JSP when Struts action is used
    By dkirvan in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-29-2010, 05:33 AM
  2. Struts 2 and Ajax drop down Example
    By priyanka.dandekar in forum Web Frameworks
    Replies: 8
    Last Post: 01-05-2010, 11:57 AM
  3. AJAX - Sending a request to a server
    By gilbertsavier in forum XML
    Replies: 0
    Last Post: 07-17-2009, 07:35 AM
  4. Request and repnse in Struts 2
    By Inaam in forum Web Frameworks
    Replies: 0
    Last Post: 03-31-2009, 07:19 PM
  5. Replies: 0
    Last Post: 07-07-2008, 07:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •