ajaxjspstruts1

Request attribute not passed from Action to JSP


I have a JSP page which displays tabular data retrieved from database on page load. Every row in the table has a "more" button in last column which opens a modal window displaying detailed information of the record. In this modal window I have a button. On click of this button I want to display the audit history for the record in a new modal window.
My JSP file contains the Ajax call to retrieve the audit details details of the particular record.

function showHistoryLog() {
    var questionId = document.getElementById("txtQuestionId").innerText;

    var url = "myAction.do?dispatchMethodName=getAuditTrail&questionId="+questionId; 

    $.ajax({
    method : "GET",
    url : url,
    data : questionId
    }).done(function(msg) {
        alert("Record retrieved successfully.");
        $('#detmyAudit').modal('show');
    });

    ev.preventDefault();
}

Action file contains code to retrieve parameters from ajax call and then call subsequent service method which returns list of records (Audit history records). I am setting this list as attribute in request object:

request.setAttribute("MyList", myList);  

When I try to access this attribute in my JSP, it is undefined.

<tbody>
        <logic:notEmpty name="MyList"> 

this is is always empty. However, in my action class this has 1 record. Any help on this is appreciated.


Solution

  • JSON is the answer. I created a JSON object and set it to set response object in my Action file.

    String myJsonObj = createJSONObject(myList);
    setReponseObj(response, myJsonObj, "text/html");
    

    Accessed this JSON object in my JSP. Function showHistoryLog is now changed to:

    function showHistoryLog() {
    
        var questionId = document.getElementById("txtQuestionId").innerText;
    
        var url = "myAction.do?dispatchMethodName=getAuditTrail&questionId="+questionId; 
    
        if ( $.fn.DataTable.isDataTable( '#tblmyAudit' ) ) {
            var auditTable = $('#tblmyAudit').dataTable().api();
            auditTable.destroy();
        }
    
        $('#tblmyAudit').DataTable( {
            ajax: {
            url: url,
            dataSrc: 'data'
            },
            columns: [
                  { data: "QuestionId" },
                  { data: "Question" },
                  { data: "Answer" },
                  { data: "UpdatedBy" },
                  { data: "UpdatedDate" }
              ],
            "scrollX": true,
    
        } );
    
        $('#detmyAudit').modal('show');
    }