struts2

How to avoid 500 error in JSP using <s:iterator> tag in Struts 2?


In my web app I generate dynamic content for id.

Below my code:

function lookup(){
<% List<UserOrganization> el = (List<UserOrganization>) request.getAttribute("uselist");%>
<% for (int i = 0; i < el.size(); i++) {%>   
        var email<%out.print(i);%>="<%out.print(el.get(i).getUser_1().getEmailId());%>";
        $.ajax({
            type: "POST",
            url: "EveConnectOutletsCount",
            data: {
                email:email<%out.print(i);%>
            },
  
            success: function(data) {
    
                $('#ec<%out.print(i);%>').html(data)
                                  
            }  
        });
<% }%>           
}

JSP page:

<s:iterator value="uselist">  
      <span id="ec<%=n%>"></span>
</s:iterator>

This page executes fine, if uselist has some value. It throws 500 error code if uselist is empty.

How can I avoid this error?


Solution

  • I solved the problem by adding below code.

    function lookup() {
    '<s:if test="uselist.size()>0">'
      <% List<UserOrganization> el = (List<UserOrganization>) request.getAttribute("uselist");%>
    <% for (int i = 0; i < el.size(); i++) {%>    
        var email<%out.print(i);%>="<%out.print(el.get(i).getUser_1().getEmailId());%>";
        $.ajax({
            type: "POST",
            url: "EveConnectOutletsCount",
            data: {
                email:email<%out.print(i);%>
            },
            success: function(data) {
                $('#ec<%out.print(i);%>').html(data)
            }
        });
    <% }%>
    '</s:if>'
    }
    

    so if list is null, my code wont generates null pointer exception.