java - Spring MVC 应用程序将多条记录返回到一次显示一条记录的 View

标签 java spring spring-mvc pagination

我有一个捕获公民信息的表单,所以我有一个公民类

公民阶层

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;......

DAO 类,根据 where_clause 返回公民列表/单个记录,where_clause 可以是任何值或值的组合。

if(where_clause != null){
        //add WHERE to string
        where_clause = "WHERE " + where_clause;
        //get length of string 
        int where_clause_length = where_clause.length();
        //remove last 'and' from string
        where_clause = where_clause.substring(0, where_clause_length - 5);

        logger.info(where_clause);
    }       

    String sql = "SELECT socialSecurityNumber, fName, lName, oName, photo, countryId, addLn1, addLn2, addLn3," 
                +"genderId, ethnicityId, skinColorId, eyeColorId, hairColorId, occupationId, phoneNo, maritalStatusId," 
                +"noticableFeatures, weight, height, citizenTypeId, dob "
                +"FROM crimetrack.tblcitizens " + where_clause;

    logger.debug(sql);

    List<Citizens> listOfCitizens = getJdbcTemplate().query(sql, new CitizensMapper());     
    return listOfCitizens;

Controller 中,会发生以下情况:

if (user_request.equals("Query")){
   logger.debug("about to preform query");
   if(citizenManager.getListOfCitizens(citizen).isEmpty()){
      model.addAttribute("icon","ui-icon ui-icon-circle-close");
      model.addAttribute("results","Notice: Query Caused No Records To Be Retrived!");                           
 }

 model.addAllAttributes(citizenManager.getListOfCitizens(citizen));

 return new ModelAndView("citizen_registration");
 }                   
}        

这里的问题是这个查询返回 3 条记录,但 View 上只显示最后一条记录。下面是jsp的样子,它使用spring表单标签。我想要做的是添加下一条记录按钮,当用户单击下一条记录时,我想移动到列表中的下一条记录并将其显示给用户。如何实现这一点?如果使用这种方法无法完成,最好的方法是什么?我正在阅读有关分页的内容,但我不确定如何将其实现到此应用程序中。有人可以帮助我或指导我如何解决这个问题吗:

Jsp

<form:form id="citizenRegistration" name ="citizenRegistration" method="POST" commandName="citizens" action="citizen_registration.htm">
                    <div id="divRight" class="mainDiv">             
                        <div class="divGroup" id="divCharInfo"> 
                            <label id="status"></label>                             
                            <div id="camera"></div>             
                            <div><p><canvas id="canvas" height="240" width="320"></canvas><canvas id="canvas2" height="240" width="320"></canvas><form:errors path="photo" class="errors"/></div>
                                    <form:input path="photo" id="photo" type="hidden"/>
                                    <input  id="upload" type="button" value="Take Photo">
                                    <input  id="retake" type="button" value="Re-Take Photo">

                            <ol>
                                <li>
                                    <label>Select Gender</label>
                                    <form:select path="genderId" id="genderId" title="Select Your Gender">
                                    <form:options items = "${gender.genderList}" itemValue="genderId" itemLabel="genderDesc" />
                                    </form:select>
                                    <form:errors path="genderId" class="errors"/>
                                </li>               

                                <li><form:label for="weight" path="weight">Enter Weight <i>(lbs)</i></form:label>
                                    <form:input path="weight" id="weight" title="Enter Weight"/><form:errors path="weight" class="errors"/>
                                </li> 

                                <li><form:label for="height" path="height">Enter Height <i>(feet)</i></form:label>
                                    <form:input path="height" id="height" title="Enter Height"/><form:errors path="height" class="errors"/>
                                </li> 

最佳答案

试试这个... 在 Controller 中

model.addAttribute("citizens",citizenManager.getListOfCitizens(citizen));

现在在你的jsp中执行以下操作: 1.在jsp页面顶部添加

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


2. 现在将此代码添加到您的 html 正文中

<c:forEach items="${citizens}" var="citizen">
First name:- ${citizen.fName} , Last Name:- ${citizen.lName}
</c:forEach>

希望这有帮助

关于java - Spring MVC 应用程序将多条记录返回到一次显示一条记录的 View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15228125/

相关文章:

java - 为什么 java clone() 留下相同的对象

java - 我应该同时学习C++和Java吗?

java - 如何销毁/清除在 Spring MVC 中使用 applicationcontext.xml 文件创建的对象?

java - Spring @RequestBody : the request sent by the client was syntactically incorrect

javascript - AngularJS 身份验证与 Spring Security CORS 问题

java - 有人可以澄清@RequestBody吗

java - 使用 Eclipse 和 OSGi 将类动态加载到 Java 类路径

java - 检查当前类的实例时,为什么 "x instanceof getClass()"不起作用?

java - Spring注入(inject)的资源总是空的

spring - 否定 Spring Controller 的 RequestMapping 中的参数