我知道这可能是一个重复的问题,但我找不到这个简单问题的答案。我想在对话框和 div 中加载新的 jsp 文件。
Structure:
-WebContent
-jsp
-viewfolder
-helloworld.jsp
-helloworldedit.jsp
-newworld.jsp
假设我有
helloworld.jsp
,它是从请求调度程序加载的。我想将 newworld.jsp
加载到 helloworld.jsp
中的 div 标签中。
<div id="result"></div>
$('#result').load('/jsp/viewfolder/newworld.jsp');
尝试了上面的代码,没有用。
我也尝试过将 jsp 页面加载到对话框中,但也失败了。
<button id="button">button</button>
<div id="dialog"></div>
$('#button').on("click", function() {
$('#dialog').load('/jsp/viewfolder/helloworldedit.jsp').dialog();
});
我的问题是,这是调用 jsp 页面的正确方法,还是我必须使用 ajax 从请求调度程序加载页面。
为了测试路径是否正确,我尝试将
calendar.gif
放在同一个文件夹中,并且我能够从上下文中访问它。
http://localhost:port/.projectcontext/jsp/viewfolder/calendar.gif.
您必须等待
DOM ready
活动:
$(document).ready(function() {
$('#button').on("click", function() {
$('#dialog').load('/.projectcontext/jsp/viewfolder/helloworldedit.jsp').dialog();
});
});
尝试以下代码:-
假设您在
div
中有一个 newworld.jsp
,其中包含您想要加载到另一个 div
中的所有数据,该数据存在于 helloworld.jsp
中
newworld.jsp
<!doctype html>
<html>
<body>
<div id="target">
<!-- other HTML controls -->
</div>
</body>
</html>
helloworld.jsp
<a href="#" onclick="loadPage();">Click Me</a>
<div id="page"></div>
<script>
function loadPage(){
$('#page').load('newworld.jsp #target');
or
// If you choose this method then give path of JSP to be loaded in
// anchor tag
$('#page').load($(this).attr('href'));
return false;
}
</script>
或
您可以像这样使用JSP包含标签
<div id="page" style="width:300px; height:300px;">
<jsp:include page="path of the page to be loaded"></jsp:include>
</div>