gpt4 book ai didi

Spring Web MVC 渲染 View 时存在巨大的性能问题

转载 作者:行者123 更新时间:2023-12-03 01:30:03 27 4
gpt4 key购买 nike

我正在使用 Spring 框架创建一个项目,并使用 Spring MVC 框架来构建我的 View 。

现在一切工作正常并且运行顺利,除了这个简单的 GET 页面需要 2 秒甚至更多的时间才能在本地主机上加载。

正如您在日志(链接)中看到的那样,GenericConversionService 尝试查找转换器来绑定(bind)属性,速度非常慢。

非常感谢您的帮助!

提前致谢(对我的拼写错误表示歉意):)

更新:

显然,转换服务针对“http://www.springframework.org/tags/form ”命名空间中的每个(与 Path 属性绑定(bind))表单标签运行。我使用的表单标签越多,页面加载速度就越慢。我应该使用普通的 html 表单标签来提高性能还是有办法阻止它寻找“正确的”转换器?

其他信息:

日志: Link (滚动大约 1/3,您将看到大量 GenericConversionService 日志

Spring Insight 分析信息:Profile information

Controller (马虎)代码:

@RequestMapping(value = "/route/create", method = RequestMethod.GET)
public ModelAndView getCreateRoute(){
RouteCreateUpdateViewModel result = new RouteCreateUpdateViewModel();
User u = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
u = us.get(u.getId());

ModelAndView mav = new ModelAndView("route/create", "routeCreateUpdateModel", result);
mav.addObject("favLocations", u.getLocations());
mav.addObject("cars", u.getCars());

result.setDateDay(Calendar.getInstance().get(Calendar.DAY_OF_MONTH) + 1); // January = 0
result.setDateMonth(Calendar.getInstance().get(Calendar.MONTH));
result.setDateYear(Calendar.getInstance().get(Calendar.YEAR));

mav.addObject("nextYear", result.getDateYear() + 1);

return mav;
}

查看代码:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>


<!-- //TODO I18N -->

<style type="text/css">
li:focus {
background-color:green;
}
</style>

<script>
var highlightedElement = null;
$(document).ready(function(){
$("input,select").click(function() {
if(highlightedElement != null)
{
$(highlightedElement).removeClass("curFocus");
if(highlightedElement.hasClass("location"))
{
checkLocation(highlightedElement);
}
}
var newHighlightedElement = $(this).parent().parent();
$(newHighlightedElement).addClass("curFocus");
highlightedElement = newHighlightedElement;
});
});

function checkLocation(highlightedElement)
{
var parameters = {
zipcode: $(highlightedElement).find(".zipcode").val(),
street: $(highlightedElement).find(".street").val(),
streetNr: $(highlightedElement).find(".streetNr").val()
};

$.getJSON('/location/validate.php', parameters, function(data) {
if(data.result != null)
{
$(highlightedElement).removeClass("badData");
$(highlightedElement).addClass("goodData");
}
else {
$(highlightedElement).removeClass("goodData")
$(highlightedElement).addClass("badData");
}
});
}

</script>

<div>
<h1><fmt:message key="route.create.header"/></h1>
<div class="form-container">
<c:url value="/route/create.php" var="actUrl"/>
<form:form method="POST" action="${actUrl}" modelAttribute="routeCreateUpdateModel">
<ul>
<li class="location">
<form:label path="fromZipcode" cssClass="title">Van<span class="required">*</span></form:label>
<span>
<form:input path="fromZipcode" cssClass="zipcode"/>
<form:label path="fromZipcode" cssClass="desc">Gemeente</form:label>
</span>
<span>
<form:input path="fromStreet" cssClass="street"/>
<form:label path="fromStreet" cssClass="desc">Straat</form:label>
</span>
<span>
<form:input path="fromStreetNr" cssClass="streetNr"/>
<form:label path="fromStreetNr" cssClass="desc">Nr</form:label>
</span>
</li>
<li class="location">
<form:label path="toZipcode" cssClass="title">Naar<span class="required">*</span></form:label>
<span>
<form:input path="toZipcode" cssClass="zipcode"/>
<form:label path="toZipcode" cssClass="desc">Gemeente</form:label>
</span>
<span>
<form:input path="toStreet" cssClass="street"/>
<form:label path="toStreet" cssClass="desc">Straat</form:label>
</span>
<span>
<form:input path="toStreetNr" cssClass="streetNr"/>
<form:label path="toStreetNr" cssClass="desc">Nr</form:label>
</span>
</li>
<li>
<form:label path="dateDay" cssClass="title">Datum<span class="required">*</span></form:label>
<span>
<tags:showDayPicker path="dateDay" currentDay="${routeCreateUpdateModel.dateDay}"/>
<form:label path="dateDay" cssClass="desc">dd</form:label>
</span>
<span>
<tags:showMonthPicker path="dateMonth" currentMonth="${routeCreateUpdateModel.dateMonth}"/>
<form:label path="dateMonth" cssClass="desc">mm</form:label>
</span>
<span>
<tags:showYearPicker path="dateYear" startYear="${routeCreateUpdateModel.dateYear}"
stopYear="${nextYear}"/>
<form:label path="dateYear" cssClass="desc">yyyy</form:label>
</span>
</li>
<li>
<form:label path="days" cssClass="title">Dagen<span class="required">*</span></form:label>
<span>
<form:checkbox path="days" value="1"/>
<form:label path="days" cssClass="desc">Ma</form:label>
</span>
<span>
<form:checkbox path="days" value="2"/>
<form:label path="days" cssClass="desc">Di</form:label>
</span>
<span>
<form:checkbox path="days" value="3"/>
<form:label path="days" cssClass="desc">Wo</form:label>
</span>
<span>
<form:checkbox path="days" value="4"/>
<form:label path="days" cssClass="desc">Do</form:label>
</span>
<span>
<form:checkbox path="days" value="5"/>
<form:label path="days" cssClass="desc">Vrij</form:label>
</span>
<span>
<form:checkbox path="days" value="6"/>
<form:label path="days" cssClass="desc">Za</form:label>
</span>
<span>
<form:checkbox path="days" value="7"/>
<form:label path="days" cssClass="desc">Zo</form:label>
</span>
</li>
<li>
<form:label path="stopDateDay" cssClass="title">Herhalen tot<span class="required">*</span></form:label>
<span>
<tags:showDayPicker path="stopDateDay" currentDay="${routeCreateUpdateModel.dateDay}"/>
<form:label path="stopDateDay" cssClass="desc">dd</form:label>
</span>
<span>
<tags:showMonthPicker path="stopDateMonth" currentMonth="${routeCreateUpdateModel.dateMonth}"/>
<form:label path="stopDateMonth" cssClass="desc">mm</form:label>
</span>
<span>
<tags:showYearPicker path="stopDateYear" startYear="${routeCreateUpdateModel.dateYear}"
stopYear="${nextYear}"/>
<form:label path="stopDateYear" cssClass="desc">yyyy</form:label>
</span>
</li>
<li>
<form:label path="departureTime" cssClass="title">Vertrek uur<span class="required">*</span></form:label>
<span>
<tags:showHourPicker path="departureTime"/>
<form:label path="departureTime" cssClass="desc">uu</form:label>
</span>
<span>
<tags:showMinutePicker path="departureTime"/>
<form:label path="departureTime" cssClass="desc">mm</form:label>
</span>
</li>
<li>
<form:label path="arrivalTime" cssClass="title">Aankomst uur<span class="required">*</span></form:label>
<span>
<tags:showHourPicker path="arrivalTime"/>
<form:label path="arrivalTime" cssClass="desc">uu</form:label>
</span>
<span>
<tags:showMinutePicker path="arrivalTime"/>
<form:label path="arrivalTime" cssClass="desc">mm</form:label>
</span>
</li>
<li>
<form:label path="car" cssClass="title">Auto</form:label>
<span>
<form:select path="car">
<form:options items="${cars}" itemLabel="carName" itemValue="Id" />
</form:select>
</span>
</li>
</ul>
<input type="submit" value="Route toevoegen"/>
</form:form>
</div>
</div>

如果您需要更多信息,请询问,我会立即提供。

再次感谢:)

最佳答案

问题可能是由 <form:option> 中的类型转换次数过多引起的标签。

对于不需要类型转换的简单值,您可以使用 HTML <option>按原样标记:

<form:select path="${path}">
<c:forEach var="i" begin="0" end="59" step="${interval}">
<option value="${i}"><fmt:formatNumber value="${i}" minIntegerDigits="2" /></option>
</c:forEach>
</form:select>

作为更积极的优化,您可以摆脱 <c:forEach><fmt:formatNumber>并填充 <form:select> 的正文使用预先生成的 HTML。

关于Spring Web MVC 渲染 View 时存在巨大的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5246685/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com