0

This is my ajax code and jstl code. I get the request attribute passed by the controller. The request was printed using "System.out.println("im here1: "+value);", but the value are not seen on the page.

$.ajax({
    type: 'post',
    url: '<%=formActionURL%>',
    data: data,
    success: function () {
     console.log('succ');
     <%
       
       ArrayList<?> msgArray = (ArrayList<?>) request.getAttribute("msgList");
       int index = 0;
       for (Object value : msgArray) {
          System.out.println("im here1: "+value);
          index++;
       }
       request.setAttribute("msgArray", msgArray);  
       %>
       
     }
  });

JSTL CODE:

<c:forEach var="msgArrays" items="${msgArray}">
        <p><c:out value="${msgArrays}" />
        </p>
    </c:forEach>

But when I use static values, it is getting printed on the page but was loaded on load of the document:

ArrayList<String> msgArray = new ArrayList<String>();
   msgArray.add("pink");
   msgArray.add("bubble");
   int index = 0;
   for (String value : msgArray) {
    System.out.println("im here1: "+value);
    index++;
   }
   request.setAttribute("msgArray", msgArray); 

Can you advise me on the possible problems? I am using form submit before but it is refreshing the whole page that is why I am using ajax call. Form submit is working fine, it is just refreshing my whole page, if you can also suggest ways on not submitting the whole page, it will be great.

  • Have you tried Hashmap in the controller to pass "msgList"? – M123 May 11 '22 at 06:08
  • Not yet. It is a very old code though that's why I am not configuring the controller. But what is the difference if I use hashmap vs list or array? Because the current prob is it doesn't display – Iskooolar2 iskoolar2 May 11 '22 at 06:27
  • Instead of using HttpServletRequest or ServletRequest to set the ArrayList, you can set this value directly in a hashmap like "hashmap.put(key,value)" so that you can get that arraylist value in response to an Ajaxcall. – M123 May 11 '22 at 06:54
  • the controllers are all returning jsp page, that is why they are setting the request though, can't change the controller :( – Iskooolar2 iskoolar2 May 12 '22 at 02:18
  • I only have power to change the jsp/jsp codes. – Iskooolar2 iskoolar2 May 12 '22 at 02:19

0 Answers0