在Jsp中显示类中的对象列表

时间:2013-04-11 02:49:57

标签: java jsp spring-mvc jstl sqlresultsetmapping

我正在使用Spring MVC,我实现了 ResultSetExtractor ,因为我的一个类有一个复杂的列表对象。我做了映射,然后从数据库中进行选择。我在显示 ResultSetExtractor 中列表项的数据时遇到问题。我希望我正确地进行了映射。其他属性有数据。问题是在jsp中显示Listvictims和Listcriminals的值。

Under是代码:

JSP

我在crimeRecNo中获取数据,但犯罪详情却不在受害者身上。

修改

我更新了代码但是我没有在受害者列表中获得任何值,在jsp上没有打印任何内容。其他元素正在获取数据。我不确定ResultSetExtractor是否已正确写入列表项。

<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映射

如果我的映射不正确,请纠正我

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;

错误

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)

2 个答案:

答案 0 :(得分:2)

第一次访问或稍后修改时的每个JSP文件都会被编译,每个JSP文件都会像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)

我建议您在Apache Tomcat中观察crimeList.jsp以上行的代码。

示例路径如下:

  

d:\ Apache的Tomcat的\工作\卡塔利娜\本地主机\ myAppName \组织\阿帕奇\ JSP \

此处myAppName将是您的应用程序名称。

编辑:应该是这样的

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

答案 1 :(得分:1)

而不是crimes.getVictims,这应该被称为crimes.victims,EL将负责插入get部分(现在它正在寻找getGetVictims