2012-12-12 55 views
1

爲了我的庫的依賴性管理,我已將RequireJS放入我的Spring MVC應用程序中。我正在使用jQuery和jQuery UI等等。我有它的工作,但每當我加載/刷新頁面,頁面將首先顯示沒有任何庫已被加載(一切顯示不正確),然後頁面將閃爍,一切都會正確顯示。這是正常的嗎?以下是我迄今爲止:RequireJS庫在Spring MVC應用程序中加載緩慢

main.js:

require([ "jquery-1.8.3", "jquery-ui-1.9.2.custom", "newsearch" ], function() { 

}); 

mysearch.jsp:

<%@ page session="true"%> 
<%@page contentType="text/html;charset=UTF-8"%> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<html> 
<head> 
<title>MyApp</title> 

<!-- CSS is loaded here --> 

<!-- JavaScript --> 
<c:url value="/resources/scripts/main" var="main" /> 
<c:url value="/resources/scripts/require-jquery.js" 
var="require" /> 

<script data-main="${main}" src="${require}"></script> 

... 

newsearch.jsp:

$(document).ready(
    function() { 
     // JS Code 
}); 

回答

1

下面的代碼通常適用於我的Spring MVC。請注意,$(document).ready(...)調用將位於mysearch.jsp的底部。

<script type="text/javascript" src="${pageContext.request.contextPath}/path/to/require-2.0.2.min.js"></script> 
<script type="text/javascript" src="${pageContext.request.contextPath}/path/to/jquery-1.7.1.min.js"></script> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     require(["${pageContext.request.contextPath}/path/to/a-dependency.js", 
       "${pageContext.request.contextPath}/path/to/another-dependency.js"], 

      function() { 
       console.log('Page dependencies loaded...'); 
       // More initialization code... 
      } 
     );    
    }); 
</script> 
+0

感謝ialencar,所以你說,含$的document.ready(...)的