【问题标题】:load a jsp in a dialog and div在对话框和 div 中加载 jsp
【发布时间】:2014-12-07 10:33:22
【问题描述】:

我知道这可能是一个重复的问题,但我找不到这个简单问题的答案。我想在一个对话框和一个div中加载一个新的jsp文件。

Structure:
 -WebContent
   -jsp
     -viewfolder
       -helloworld.jsp
       -helloworldedit.jsp
       -newworld.jsp

假设我有helloworld.jsp,它是从请求调度程序加载的。我想在helloworld.jsp的div标签中加载newworld.jsp

<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.

【问题讨论】:

  • 请注意,在上面的代码中,您尝试选择 result 元素,而不是 id="result" 的元素。你有什么理由不能只使用服务器端包含?

标签: java javascript jquery jsp


【解决方案1】:

你必须等待DOM ready事件:

$(document).ready(function() {
    $('#button').on("click", function() {
        $('#dialog').load('/.projectcontext/jsp/viewfolder/helloworldedit.jsp').dialog();
    });
});

【讨论】:

  • 有什么错误吗?如果你的JS在JSP页面中,是不是用script标签括起来了?
  • 您尝试加载的jsp 页面的绝对URL 是什么?
  • http://localhost:port/.projectcontext/jsp/viewfolder/helloworldedit.jsp 这应该是绝对 URL。但是jsp页面打不开
  • 那么您想使用URL /.projectcontext/jsp/viewfolder/helloworldedit.jsp 或绝对URL,或者如果此页面与加载它使用的文件夹位于同一文件夹中,helloworldedit.jsp
  • 我也试过{request.contextPath}/jsp/viewfolder/helloworldedit.jsp。这也不起作用。我得到的只是500 Internal Server Errorhelloworldedit.jsp,它们也不起作用。
【解决方案2】:

试试下面的代码:-

假设您在newworld.jsp 中有一个div,其中包含您要加载到另一个div 中的所有数据,而helloworld.jsp 中存在该div

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> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 2013-02-24
    • 2019-08-15
    • 1970-01-01
    • 2011-07-08
    相关资源
    最近更新 更多