Struts with Ajax problem

Discussions

EJB programming & troubleshooting: Struts with Ajax problem

  1. Struts with Ajax problem (3 messages)

    Hi
    i need your help on ajax with struts
    what i did is i have a text box and a select box with one button what i want to do is i enter a value in the textbox then after press the button that value shuld be inserted in the data base and also displayed in the select box .
    i can able to update the data base and create response like this
    ***************************************************************

    here is my action class:---
    try
      {
         l_CatalougeImpl.addEcardCatagory(idField);// for data base insertion
                
                String value_idField=idField.trim();//get the value here in the idField variable

                StringBuffer l_stringBuffer= new StringBuffer();
                l_stringBuffer.append("<catagory>");
                l_stringBuffer.append("<CatagoryName>"+idField.trim()+"</CatagoryName>");
                l_stringBuffer.append("</catagory>");
                addCatagory=true;
                
                if(addCatagory)
                {
                response.setContentType("text/xml");
                response.setHeader("Cache-Control", "no-cache");
                response.getWriter().write("<Catagories>"+l_stringBuffer.toString()+"</Catagories>");
                }
                else
                {
                    response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                }
                
                
              }catch(Exception e) {
                    return mapping.findForward("error");
                }
              return mapping.findForward("ecards.TestAJAX"); // it will forward to some jsp page as per struts-config.xml mapping
            }
    *********************************************************************************
    Here is my JSP code:--
    <%@page contentType="text/html" %>
    <%@ taglib uri="../WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="../WEB-INF/struts-html.tld" prefix="html" %>
    <%@ taglib uri="../WEB-INF/struts-logic.tld" prefix="logic" %>
    <html:html>
    <head>
    <title><bean:message key="Ecards.SendEcardForm.title"/></title>
    <script>
    var req;
    var catagory;
    function validate()
    {
    var idField=document.getElementById("card_name");
    var url = "<%=request.getContextPath()%>/ajaximpl.do?id=" +idField.value;
    if (window.XMLHttpRequest)
    {
    req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.open("GET", url, true);
    req.onreadystatechange = callback;
    req.send(null);
    }
    function callback()
    {
    if (req.readyState == 4)
    {
    //alert("Ajax response:"+req.responseText);
    if (req.status == 200)
    {
    alert("update dom");
    parseMessages();
    }
    else
    {
    alert("Ajax response:"+req.stutsText);
    }
    }
    }
    function parseMessages()
    {
    alert("inside the parsing Message");
    var xml=req.responseXML;// Here i got the nulll value
    alert("inside while loop");
    var catagories=xml.getElementsByTagName("Catagories");
    alert("catagories:--"+catagories);
    if(catagories !=null)
    {
    var catagoryId=catagories.childNodes;
    if(catagoryId !=null)
    {
    catagory=catagoryId.getElementsByTagName("CatagoryName");
    }
    else
    {
    alert("catagoryId has no value");
    }
    }
    else
    {
    alert("catagories has no value");
    }
    }
    alert("catagory value is :--"+catagory);
    </script>
    </head>
    <body bgcolor="#FFFFFF">
    <!-- Start: template code Vikas Sheel Gupta 22nd Dec 05 -->
    <Center>
    <table border="0" cellpadding="0" cellspacing="0" height='559' width='779'>
    <tr>
    <td valign="top" height='60'><jsp:include page="/header.jsp"/>
    </td>
    </tr>
    <tr>
    <td valign="top" height='439'>
    <!-- Start : Vikas Sheel Gupta Code to display card and card contents -->
    <html:form action="/ajaximpl">
    <table>
    <tr><td><bean:message key="Ecards.SendEcardForm.catagory" /></td>
    <td>
    <!-- code for card catagory-->
    <logic:present name="Array_ecardlist" scope="request">
    <html:select property='catagory' >
    <html:options collection="Array_ecardlist" property="m_strCategoryId" labelProperty="m_strCategory_Name" />
    </html:select>
    </logic:present>
    <!-- End of code for card catagory-->
    </td>
    </tr>
    <tr><td><bean:message key="Ecards.SendEcardForm.card_name" /></td>
    <td>
    <html:text property="card_name" />
    </td>
    </tr>
    <tr>
    <td>
    <html:button property="button" onclick="return validate()"><bean:message key="button.save"/></html:button>
    </td>
    </tr>
    </table>
    </tr>
    </table>
    </html:form>
    <!-- End : Vikas Sheel Gupta Code to display card and card contents -->
    </td>
    </tr>
    <tr>
    <td valign="top" height='60'><jsp:include page="/header.jsp"/></td>
    </tr>
    </table>
    </Center>

    <!-- End: template code-->
    </body>
    </html:html>

    ***************************************************************
    Now my problem is when i call the parseMessages() function in java script then my req.responseXML is null i cant under stand why this happens
    can u pls tell me why this happend
    i shall be thank ful to you



    Warm Regards
    Vikas Sheel Gupta
         09891424705
    vikassheelgupta@rediffmail.com

    Threaded Messages (3)

  2. Struts with Ajax problem[ Go to top ]

    As far as i have knowledge of struts , is the value entered in jsp text box availiable to the action form?
    Try finding that out.
    If yes :
    1)since you are using actionMapping.findforward the page is forwarded to some other page.Hence the response is from action class is not available .

    instead try to follow following thing.
    1)create a simple servlet (not action class).
    2)call it from jsp (instead of actionclass)
    3)do all ur business logic (inserting in db or whatever)in that class and write an xml file on the response
    4)thats it!!!!

    -Sanket Raut
  3. Reponse XML[ Go to top ]

    Hi vikas,

    the first thing that could help may be you not use the mapping.findForward(); instead return null for the ActionForward.

    I have also tried the Ajax in PHP, and while coding i usually had the response in XML which when i use alert(responseXML); it gave me nothing..make sure you pass the correct parameters while invoking methods..

    Regards
    Sanjeev Sobhun V
  4. ResponseXML is null[ Go to top ]

    Hello vikas,

    Yes ive tried it out and i got a null value when doing req.responseXML only when the server fails to load the required method...the reason was that the action i called was not existent..

    So to resolve it i used html:rewrite ...to pass the actionLink as parameter in the Javascript Remote Call Method.

    However if the server has responsed in the status==200, there you will not received null response but empty string..in fact it may be working..

    If you have any difficulty contact me on my mail address..


    A Sample working Code:

    Server Side:

     public ActionForward testValidMessage(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws Exception {


    //Call the general Method
    response.setContentType("text/xml");
    response.setHeader("Cache-Control", "no-cache");
    PrintWriter out = response.getWriter(); response.getWriter().write("<message>valid</message>"); return null;
     }


    CLIENT SIDE:
    Javascript

    function testAjaxOnServer(pInterface, pMethod){

    http.open('GET', pInterface + '?methodToCall=' + pMethod);
    alert('Sending..');
    http.onreadystatechange = handleAjaxResponseFromServer;
    alert('Sent Request..');
    http.send(null);
    }


    JSP:
    use a button here
    <input type="button" value='Test Message' name="txt_test_msg" onClick="testAjaxOnServer('<html:rewrite page="/do/setupTestAjaxAction" />','testValidMessage');">

    Struts-Config

    Ive used a Dispatcher here for the Javascript Remote Calls
    <!-- Test for Ajax -->
              <action path="/setupTestAjaxAction"
                type="net.cgms.tests.SetupTestAjaxDispatchAction"
                scope="request"
                validate="false"
                parameter="methodToCall"
                >
            </action>


    Hope it helps you..

    Regards
    Sanjeev