javajspspring-mvcjstlsqlresultsetmapping

Displaying Object List From Class In Jsp


I am using Spring MVC and i implemented ResultSetExtractor since one of my classes had a complex list object. I did the mappings and then made the select from the database. I am having problems displaying the data from the list items in the ResultSetExtractor. I am hoping that i did the mapping correctly. The other attributes have data. The issue is displaying the values from the Listvictims and Listcriminals in the jsp.

Under is the code:

Jsp

I am getting data in the crimeRecNo and the crimeDetails however non in the victim.

EDIT:

I updated the code however i am not getting any values back in the victims list nothing is printed on the jsp. The other elements are getting data. I am not sure if the ResultSetExtractor was written up properly for the list items.

<form:form id="crimeList" name ="crimeList">
    <div id ="content">
        <c:forEach items="${crimes}" var="crime">
            <div id="table">        

                <label>${crime.crimeRecNo}</label>
                <label>${crime.crimeDetails}</label>

                <c:forEach items="${crime.victims}" var="victim">
                    <label>${victim.fName}</label>     
                </c:forEach>

            </div>
        </c:forEach>
    </div>
</form:form>

ResultSetExtractor Mappings

Please correct me if i did the mapping incorrectly

private static final class CrimeMapper implements ResultSetExtractor<List<Crime>>{

    public List<Crime> extractData(ResultSet rs) throws SQLException, DataAccessException {

        Map<Integer, Crime> map = new HashMap<Integer, Crime>();
        Crime crime = null;

        while (rs.next()){

            int crimeRecNo = rs.getInt("crimeRecNo");
            crime = map.get(crimeRecNo);
            if (crime == null){
                crime = new Crime();
                crime.setCrimeRecNo(crimeRecNo);
                crime.setCrimeDate(rs.getString("crimeDate"));
                crime.setCrimeDetails(rs.getString("crimeDetails"));
                crime.setCrimeStreetLn1(rs.getString("crimeStreetLn1"));
                crime.setCrimeStreetLn2(rs.getString("crimeStreetLn2"));
                crime.setCrimeLatLocation(rs.getBigDecimal("crimeLatLocation"));
                crime.setCrimeLongLocation(rs.getBigDecimal("crimeLongLocation"));
                crime.setCrimeTime(rs.getString("crimeTime"));
                crime.setInvestigatingOfficerId(rs.getString("investigatingOfficerId"));
                crime.setCrimeLevelId(rs.getInt("crimeLevelId"));
                crime.setOffenceCatId(rs.getInt("offenceCatId"));
                crime.setOffenceTypeId(rs.getInt("offenceTypeId"));
                crime.setStatusId(rs.getInt("statusId"));
                crime.setRecordedById(rs.getString("recordedById"));
                crime.setVictims(new ArrayList<Citizens>());
                crime.setCriminals(new ArrayList<Citizens>());
                map.put(crimeRecNo, crime);
            }

            Integer socialSecurityNumber = rs.getInt("socialSecurityNumber");
            Integer citizenTypeId = rs.getInt("citizenTypeId");
            if(socialSecurityNumber > 0 & citizenTypeId == 3){
                Citizens victims = new Citizens();
                victims.setAddLn1(rs.getString("addLn1"));
                victims.setAddLn2(rs.getString("addLn2"));
                victims.setAddLn3(rs.getString("addLn3"));
                victims.setCitizenTypeId(rs.getInt("citizenTypeId"));
                victims.setCountryId(rs.getInt("countryId"));
                victims.setEthnicityId(rs.getInt("ethnicityId"));
                victims.setfName(rs.getString("fName"));
                victims.setGenderId(rs.getString("genderId"));
                victims.setHairColorId(rs.getString("hairColorId"));
                victims.setHeight(rs.getDouble("height"));
                victims.setlName(rs.getString("lName"));
                victims.setMaritalStatusId(rs.getInt("maritalStatusId"));
                victims.setNoticeableFeatures(rs.getString("noticableFeatures"));
                victims.setOccupationId(rs.getInt("occupationId"));
                victims.setoName(rs.getString("oName"));
                victims.setPhoneNo(rs.getString("phoneNo"));
                victims.setPhoto(rs.getString("photo"));
                victims.setSkinColorId(rs.getString("skinColorId"));
                victims.setSocialSecurityNumber(rs.getInt("socialSecurityNumber"));
                victims.setWeight(rs.getDouble("weight"));
                victims.setEyeColorId(rs.getString("eyeColorId"));
                victims.setDob(rs.getDate("dob"));
                victims.setName(rs.getString("fName") +" "+ rs.getString("lName"));

                crime.getVictims().add(victims);
            } else if(socialSecurityNumber > 0 & citizenTypeId == 2){
                Citizens criminal = new Citizens();
                criminal.setAddLn1(rs.getString("addLn1"));
                criminal.setAddLn2(rs.getString("addLn2"));
                criminal.setAddLn3(rs.getString("addLn3"));
                criminal.setCitizenTypeId(rs.getInt("citizenTypeId"));
                criminal.setCountryId(rs.getInt("countryId"));
                criminal.setEthnicityId(rs.getInt("ethnicityId"));
                criminal.setfName(rs.getString("fName"));
                criminal.setGenderId(rs.getString("genderId"));
                criminal.setHairColorId(rs.getString("hairColorId"));
                criminal.setHeight(rs.getDouble("height"));
                criminal.setlName(rs.getString("lName"));
                criminal.setMaritalStatusId(rs.getInt("maritalStatusId"));
                criminal.setNoticeableFeatures(rs.getString("noticableFeatures"));
                criminal.setOccupationId(rs.getInt("occupationId"));
                criminal.setoName(rs.getString("oName"));
                criminal.setPhoneNo(rs.getString("phoneNo"));
                criminal.setPhoto(rs.getString("photo"));
                criminal.setSkinColorId(rs.getString("skinColorId"));
                criminal.setSocialSecurityNumber(rs.getInt("socialSecurityNumber"));
                criminal.setWeight(rs.getDouble("weight"));
                criminal.setEyeColorId(rs.getString("eyeColorId"));
                criminal.setDob(rs.getDate("dob"));
                criminal.setName(rs.getString("fName") +" "+ rs.getString("lName"));
                crime.getCriminals().add(criminal);                 
            }               
        }           
        return new ArrayList<Crime>(map.values());
    }       
}   

Crime.java

public class Crime implements Serializable{

    private int crimeRecNo;
    private String crimeDetails;
    private BigDecimal crimeLatLocation;
    private BigDecimal crimeLongLocation;
    private String crimeStreetLn1;
    private String crimeStreetLn2;
    private String crimeDate;
    private String crimeTime;
    private List<Citizens> victims;
    private List<Citizens> criminals;   
    private String recordedById;
    private int statusId;
    private int offenceCatId;
    private int offenceTypeId;
    private int crimeLevelId;
    private String investigatingOfficerId;

..........

Citizens.java

public class Citizens implements Serializable{

    private int socialSecurityNumber;
    private String fName;
    private String lName;
    private String oName;
    private String photo;
    private int countryId;
    private String addLn1;
    private String addLn2;
    private String addLn3;
    private String genderId;
    private int ethnicityId;
    private String skinColorId;
    private String hairColorId;
    private int occupationId;
    private String phoneNo;
    private int maritalStatusId;
    private String noticeableFeatures;
    private double weight;
    private double height;
    private int citizenTypeId;
    private String eyeColorId;
    private Date dob;
    private String name;

Error

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/crimeList.jsp at line 62

59:        <label>${crime.crimeRecNo}</label>
60:        <label>${crime.crimeDetails}</label>
61:        
62:        <c:forEach items="${crimes.getVictims}" var="victim">
63:        <label>${victim.getName}</label>
64:        
65:        </c:forEach>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

java.lang.NumberFormatException: For input string: "getVictims"
    java.lang.NumberFormatException.forInputString(Unknown Source)
    java.lang.Integer.parseInt(Unknown Source)
    java.lang.Integer.parseInt(Unknown Source)
    javax.el.ListELResolver.coerce(ListELResolver.java:173)
    javax.el.ListELResolver.getValue(ListELResolver.java:52)
    org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
    org.apache.el.parser.AstValue.getValue(AstValue.java:183)
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
    org.apache.jsp.WEB_002dINF.jsp.crimeList_jsp._jspx_meth_c_005fforEach_005f1(crimeList_jsp.java:249)
    org.apache.jsp.WEB_002dINF.jsp.crimeList_jsp._jspx_meth_c_005fforEach_005f0(crimeList_jsp.java:216)
    org.apache.jsp.WEB_002dINF.jsp.crimeList_jsp._jspx_meth_form_005fform_005f0(crimeList_jsp.java:164)
    org.apache.jsp.WEB_002dINF.jsp.crimeList_jsp._jspService(crimeList_jsp.java:127)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

Solution

  • Each JSP file when accessed first time or when modified later gets compiled and each JSP file will be something like crimeList_jsp.java.

     org.apache.jsp.WEB_002dINF.jsp.crimeList_jsp._jspx_meth_c_005fforEach_005f1(crimeList_jsp.java:249)
        org.apache.jsp.WEB_002dINF.jsp.crimeList_jsp._jspx_meth_c_005fforEach_005f0(crimeList_jsp.java:216)
        org.apache.jsp.WEB_002dINF.jsp.crimeList_jsp._jspx_meth_form_005fform_005f0(crimeList_jsp.java:164)
        org.apache.jsp.WEB_002dINF.jsp.crimeList_jsp._jspService(crimeList_jsp.java:127)
    

    I suggest you to observe code for above lines for crimeList.jsp in your Apache Tomcat.

    The sample path would be like this:

    D:\apache-tomcat\work\Catalina\localhost\myAppName\org\apache\jsp\

    Here myAppName would be your application name.

    EDIT: It should be like this

    <c:forEach items="${crimes.victims}" var="victim">
                <label>${victim.name}</label>       
    </c:forEach>