【问题标题】:Including jsp in a div asynchronously on same jsp when clicking a link单击链接时,在同一 jsp 上异步将 jsp 包含在 div 中
【发布时间】:2024-05-23 04:25:01
【问题描述】:

我正在尝试实现用户帐户页面。在页面中,当我单击左侧的链接时,例如“搜索用户”、“准备笔记”等。我想在右侧的 div 中包含相关的 jsp 页面,如下所示。

我首先使用 iframe 代替 div,但我可以将会话对象属性传递给 iframe 中加载的页面,因为 iframe 发送单独的 http 请求。

我想在右侧 div 中使用 jsp:include 而不是 iframe 来显示相应的 jsp 页面。新的目标 div 将与 iframe 相同。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">

<script src="${pageContext.request.contextPath}/js/jquery-1.11.2.min.js"></script>
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
<style>
 #dashboard-iframe{
    border: 1px solid gray;
    height: 200px;
    width: 200px;
    }
</style>
<script type="text/javascript">


//jquery function i tried   
(document).ready(function() {
    $("#searchUser").click(function(){

        $("#dashboard-iframe").load("${pageContext.request.contextPath}/searchuser");

    });
});

//plain javascript
function searchClick(){
    document.getElementById('dashboard-iframe').innerHTML=
        "<jsp:include page="${pageContext.request.contextPath}/searchuser">";
}



</script>
</head>
<body onload="hidedisplay()">
    <div class="panel panel-default">
        <div class="panel-body">
            <div class="list-group pull-left">
                <a id="searchUser" onclick="searchClick()" href="#" class="list-group-item" target="dashboard-iframe">
                    <span class="glyphicon glyphicon-search"></span> Search Users
                </a>
                <a href="${pageContext.request.contextPath}/message_board" target="dashboard-iframe" class="list-group-item">
                    <span class="glyphicon glyphicon-inbox"></span> Message Board
                </a>
                <a href="${pageContext.request.contextPath}/prepare_notes" class="list-group-item" target="dashboard-iframe">
                    <span class="glyphicon glyphicon-list-alt"></span> Prepare Notes
                </a>
                <a href="${pageContext.request.contextPath}/UploadYourFile" target="dashboard-iframe" class="list-group-item">
                    <span class="glyphicon glyphicon-upload"></span> Share File
                </a>
                <a href="${pageContext.request.contextPath}/ComposeMessage" target="dashboard-iframe" class="list-group-item">
                    <span class="glyphicon glyphicon-envelope"></span> Compose Message
                </a>
                <a href="${pageContext.request.contextPath}/edit-profile" target="dashboard-iframe" class="list-group-item">
                    <span class="glyphicon glyphicon-pencil"></span> Edit Profile
                </a>
                <a href="${pageContext.request.contextPath}/connected-users" target="dashboard-iframe" class="list-group-item">
                    <span class="glyphicon glyphicon-link"></span> Connected Users
                </a>

            </div><!-- list-group end -->
        </div>
    </div>

    <div id="dashboard-iframe">
        <!-- Where i want to display the pages -->
    </div>
</body>
</html>

我尝试了一些 jquery 以及普通的 javascript,但它不起作用。我很早就需要解决方案......对不起。请帮忙。

【问题讨论】:

    标签: javascript jquery html jsp iframe


    【解决方案1】:

    你可以写:

    function searchClick(){
        $('#dashboard-iframe').load('/searchuser');
    }
    

    .load() 从服务器加载数据并将返回的 HTML 放入匹配的元素中。

    【讨论】:

    • 如果您能解释一下您显示的代码的作用,以及该代码为何/如何回答问题,这将使您的回答更有帮助。
    【解决方案2】:

    首先,我在您的代码中没有看到任何iframe 标记。 HTML 应包含 iframe,例如:

    <div id="dashboard-iframe">
       <iframe></iframe>
    </div>
    

    然后你的 JS 代码应该是:

    $("#searchUser").click(function(){    
       $("#dashboard-iframe > iframe").attr("src", "${pageContext.request.contextPath}/searchuser");    
    });
    

    【讨论】:

    • 对不起,但这是我尝试过的版本...我真的不想要 iframe,所以我没有添加。我只想在 div 'dashboard-iframe' 中异步包含 jsp 页面。这样我就可以使用会话对象。因为会话对象不能传递到 iframe 页面
    最近更新 更多