Struts with Ajax problem


EJB programming & troubleshooting: Struts with Ajax problem

  1. Struts with Ajax problem (3 messages)

    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:---
         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();
                response.setHeader("Cache-Control", "no-cache");
              }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" %>
    <title><bean:message key="Ecards.SendEcardForm.title"/></title>
    var req;
    var catagory;
    function validate()
    var idField=document.getElementById("card_name");
    var url = "<%=request.getContextPath()%>/" +idField.value;
    if (window.XMLHttpRequest)
    req = new XMLHttpRequest();
    else if (window.ActiveXObject)
    req = new ActiveXObject("Microsoft.XMLHTTP");
    }"GET", url, true);
    req.onreadystatechange = callback;
    function callback()
    if (req.readyState == 4)
    //alert("Ajax response:"+req.responseText);
    if (req.status == 200)
    alert("update dom");
    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");
    if(catagories !=null)
    var catagoryId=catagories.childNodes;
    if(catagoryId !=null)
    alert("catagoryId has no value");
    alert("catagories has no value");
    alert("catagory value is :--"+catagory);
    <body bgcolor="#FFFFFF">
    <!-- Start: template code Vikas Sheel Gupta 22nd Dec 05 -->
    <table border="0" cellpadding="0" cellspacing="0" height='559' width='779'>
    <td valign="top" height='60'><jsp:include page="/header.jsp"/>
    <td valign="top" height='439'>
    <!-- Start : Vikas Sheel Gupta Code to display card and card contents -->
    <html:form action="/ajaximpl">
    <tr><td><bean:message key="Ecards.SendEcardForm.catagory" /></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" />
    <!-- End of code for card catagory-->
    <tr><td><bean:message key="Ecards.SendEcardForm.card_name" /></td>
    <html:text property="card_name" />
    <html:button property="button" onclick="return validate()"><bean:message key=""/></html:button>
    <!-- End : Vikas Sheel Gupta Code to display card and card contents -->
    <td valign="top" height='60'><jsp:include page="/header.jsp"/></td>

    <!-- End: template code-->

    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

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

    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 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 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.setHeader("Cache-Control", "no-cache");
    PrintWriter out = response.getWriter(); response.getWriter().write("<message>valid</message>"); return null;


    function testAjaxOnServer(pInterface, pMethod){'GET', pInterface + '?methodToCall=' + pMethod);
    http.onreadystatechange = handleAjaxResponseFromServer;
    alert('Sent Request..');

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


    Ive used a Dispatcher here for the Javascript Remote Calls
    <!-- Test for Ajax -->
              <action path="/setupTestAjaxAction"

    Hope it helps you..