【问题标题】:Spring MVC application that returns several records to View that displays one record at a time将多条记录返回给一次显示一条记录的视图的 Spring MVC 应用程序
【发布时间】:2013-03-05 15:48:01
【问题描述】:

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

市民阶层

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 返回一个公民列表/单个记录,可以是任何值或值的组合。

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;

控制器中会发生以下情况:

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 条记录,但视图上只显示最后一条记录。下面是jsp的样子,它使用spring表单标签。我想要做的是添加下一条记录按钮,当用户单击下一条记录时,我想移动到列表中的下一条记录并将其显示给用户。如何实现这一点,如果不能使用这种方法完成,那么最好的方法是什么。我正在阅读有关 Pagination 的内容,但我不确定如何将它实现到此应用程序中。有人可以帮助我或指导我如何解决这个问题:

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> 

【问题讨论】:

  • model.addAllAttributes(Collection) 使用属性名称生成来引用集合中的每个对象。由于它们都是Citizens 对象,因此它们都将使用键citizens 覆盖先前的值,因此如果您尝试在视图中检索它,则只会出现最后一个。
  • 好的,请告诉我浏览集合中的记录的正确方法是什么
  • 我昨天告诉过你,哈哈,model.addAttribute("citizens", citizenManager.getListOfCitizens(citizen))。然后将列表引用为${citizens},其中“citizens”是您的请求上下文中的键。
  • 您将需要结合使用 javascript/ajax 和表单提交来实现分页。
  • 谷歌java网页分页,例如。 blog.fawnanddoug.com/2012/05/…

标签: java spring spring-mvc pagination


【解决方案1】:

试试这个... 在控制器中

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

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

&lt;%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;


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

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

希望对你有帮助

【讨论】:

  • 好吧,这更像是一个更接近的答案,但是我使用的是弹簧表单标签,所以我想维护
  • 那你为什么要传递一个列表?试着一次做一个。 :)
  • 当你说一次返回一个时,查询返回很多行你的意思是限制它形成数据库吗?
  • yes..u cn 这样做..否则显示我提到的所有返回的公民,然后提供更新每个将在运行时构建的链接。我认为这应该对您有所帮助。我猜您将如何不进行批量更新。所以最好公开另一个专门用于更新每个用户的 URL。
  • 没有机会在进程之间被其他用户添加记录???
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 2017-02-24
  • 2019-02-18
相关资源
最近更新 更多