【问题标题】:How to prevent iteration over same attributes on jsp page如何防止对jsp页面上相同属性的迭代
【发布时间】:2013-06-26 15:15:12
【问题描述】:

我有一个这样的jsp页面:

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

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

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

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>    

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Page for find kids</title>

</head>

<body>

<a href="#" id="shlink"><h3 align="center">Parameters of search</h3></a>

<form:form action="result" method="get" modelAttribute="fbosAttribute" >

<table id="searchForm" align="center">

<tr id="dateId">
<th>Date:</th> 
<td><form:select  path="particularDate">
<form:option value=""> -Выберите дату-</form:option>
<form:options items="${listOfDates}"></form:options>
</form:select> <td><font color="#FF0000"><b><form:errors path="particularDate"/></b>     </font></td>
</td>    
</tr>   

<tr id="nameId">
<th>Name:</th> 
<td><form:select  path="nameOfInstitution">
<form:option value=""> -Выберите учреждение-</form:option>
<form:options items="${listOfInstitutionsNames}"></form:options>
</form:select></td> <td><font color="#FF0000"><b><form:errors path="nameOfInstitution"/></b></font></td>
</tr>

<tr id="typeId">
<th>Type:</th>
<td>
<form:select  path="typeOfInstitution">
<form:option value=""> -Выберите тип-</form:option>
<form:options items="${listOfInstitutionsTypes}"></form:options>
</form:select> </td> <td><font color="#FF0000"><b><form:errors path="typeOfInstitution"/></b></font></td>
</tr>

<tr>
<td>
<input type="submit" value="Find" id="searchBtn" />
</td>
</tr>

</table>

</form:form>  

<c:choose>


<c:when test="${empty dateAttribute}">

<h1 align="center">Insert parameterst for search</h1>

</c:when>

<c:otherwise>

<table  align="center" border="1" id="resultTable">

<thead>
<tr>
<th>Name of school</th>
<th>Type</th>
<th>Particular date</th>
<th>Day Scheduale</th>
<th>Work Scheduale</th>
<th>Rotation</th>
<th>Number of kids</th>
<th>Kids upper 3 years old</th>
<th>Kids under 3 years old</th>
<th>Kids go to school date </th>
<th>Kids admitted date</th>
</tr>
</thead>    

<c:forEach items="${institutionAttribute}" var="institutionVar">
    <c:forEach items="${dateAttribute}" var="creationDateVar">
        <c:forEach items="${srcAttribute}" var="schRotChildVar">    


<tr>
<td align="center">${institutionVar.nameOfInstitution}</td>
<td align="center">${institutionVar.typeName}</td>
<td align="center">${creationDateVar.particularDate} </td>
<td align="center">${schRotChildVar.dayScheduale}</td>
<td align="center">${schRotChildVar.workScheduale}</td>
<td align="center">${schRotChildVar.rotation}</td>
<td align="center">${schRotChildVar.numberOfChild}</td>
<td align="center">${schRotChildVar.childUnder3YearsOld}</td>
<td align="center">${schRotChildVar.childUpper3YearsOld}</td>
<td align="center"><fmt:formatDate value="${creationDateVar.childGoSchoolDate}" pattern="dd-MM-yyyy" /> </td>
<td align="center"><fmt:formatDate value="${creationDateVar.childAdmissionDate}" pattern="dd-MM-yyyy" /></td>
</tr>


</c:forEach>
    </c:forEach>
        </c:forEach>


</table>

</c:otherwise>

</c:choose>   

</body>

</html>

所以当我不是从这个 jsp 中提取数据,而是从我的 main() 方法中提取数据时,它工作得非常好。什么意味着我的实现 - dao 类工作稳定。但是当我使用这个 jsp 时,它会提取 10 次以上的相似数据。我想到了这个标签&lt;c:forEach&gt;,这里一定是出现了问题。请帮我解决它。你的建议,也许我不需要&lt;c:forEach&gt;标签,可能有不同的东西。

【问题讨论】:

    标签: forms jsp spring-mvc jsp-tags jspx


    【解决方案1】:

    如果您使用 c:forEach 编写的内容过多,则需要时间。从您的示例中,我发现复杂性是 n 立方体。基本上在服务器端生成html,并在服务器发送后在客户端呈现。例如,如果您在页面中写入 1000 行,则会在缓冲区中生成一个巨大的 html 页面。如果缓冲区很大,刷新它需要时间。

    最重要的一点是响应时间取决于您写入的数据量

    因此,为了提高性能,您可以执行以下操作

    • 尽量避免 n 立方 复杂性。在 DAO 中做一些事情,这样你就可以在 jsp 页面中编写 n 复杂度了。
    • 不要一次渲染全部数据。而是渲染部分数据并使用分页来获取更多数据。

    【讨论】:

    • 感谢您的回答。毫无疑问,您的回答非常专业,甚至很难理解。什么是“n 立方体”?我的 dao 工作正常,它会返回我需要的任何东西,以便我更喜欢它在控制台中返回,但现在我需要将所有这些数据放到我的表中,以使其看起来对用户来说更准确。如果我需要迭代它们我有不止一个对象作为模型属性从 dao 返回。我找到的一个解决方案是在 jsp 中迭代它们,但这里有一个问题,我什至无法正确地从 dao 迭代属性。还是谢谢你。
    • 您正在运行 3 个嵌套循环。为简洁起见,我假设每个循环都对 n 个项目进行操作。然后会发生 nnn 次迭代。这就是所谓的 n 立方时间复杂度。你的 DAO 正在获取多少数据?如果jsp必须渲染大量数据,那么最好使用分页。下面是一个简单的 jsp 分页示例:theopentutorials.com/examples/java-ee/jsp/…
    • MAK Ripon 非常感谢您的回答。我的 dao 最大值将获取 40 行,每行有 9 列。这是最大的。我会看这个 pegination 的例子,这看起来是一种解决问题的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多