有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用javascript在另一个jsp中包含jsp

我有一个按钮注销。单击注销后,我需要显示另一个页面。如何使用JavaScript实现这一点?有人能帮我吗

我的代码:

<s:form name="LogoutAction"
                id="LogoutAction" action="logout">
    <span class="inlayField2">
    <s:a href="logout" cssClass="planTabHeader" id="logoutId"> <img src="../../KY/images/common/header/lock.png" alt="logout" style="border: none;background-color: transparent;" /> &nbsp;Log out</s:a></span>
    </s:form> 

我试过这个:

$('#logoutId').click(function(event) {

    $('#logoutdiv').load('ConfirmationPopup.jsp');
});

共 (6) 个答案

  1. # 1 楼答案

    1) Create a <div id='someID'></div> while initial page load

    2) on button click, thru javascript create an iframe with your new jsp url

    3) Once the iframe content loaded show it as a popup perhaps in dialog window

  2. # 2 楼答案

    您可以使用一个简单的GET请求来获取辅助.jsp页面呈现的HTML。 基本上,在Ajax中,使用jQuery可以执行以下操作:

    $.get({
        url: "mysite.com/otherpage.jsp",
        data: "some data you want to send, optional",
        success: function(data, textStatus, jqXhr)//the response data, the ajax request status and the ajax request object itself {
            $('#someDiv').html(data);
        }
    });
    
  3. # 3 楼答案

    另一种方法是保持跨度,然后单击“执行”: http://sel2in.com/pages/prog/html/ajax/aPage2.html1

    <span id=confirmSpan></span>
    <script language="Javascript">
    
    function logout1(){
        iconfirmSpan = document.getElementById("confirmSpan")
        if (!confirmSpan){
            alert("Error no confirmSpan contact support!");
        }
        confirmSpan.innerHTML = "<iframe src=\"http://sel2in.com/pages/prog/html/ajax/confirm.html\" height=400 width=700></iframe>"
        //in real can keep height 1 width 1 and location off page if you want it just to load elements, scripts etc
    }
    </script>
    </head>
    <body>
    <form  id="search" action="/search" method="get">
    <input type=button onclick=logout1() value="Do Action">
    </form >
    
  4. # 4 楼答案

    您可以使用ajax从第二个jsp获取html,然后将其附加到DOM或元素的innerHTML

    主页。jsp

    <span id=confirmSpan></span>
    <script language="Javascript">
    function xmlhttpPost(strURL, queryStr) {
        var xmlHttpReq = false;
        var self = this;
        // Mozilla/Safari, opera etc
        if (window.XMLHttpRequest) {
            self.xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }else{
            alert("no ajax")
            return
        }
        self.xmlHttpReq.open('POST', strURL, true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() {
            if (self.xmlHttpReq.readyState == 4) {
                updatepageConfirm(self.xmlHttpReq.responseText);
            }
        }
        self.xmlHttpReq.send(queryStr);
    }
    
    
    function updatepageConfirm(str){
        document.getElementById("confirmSpan").innerHTML = str;
    }
    function logout1(){
    xmlhttpPost("confirm.html");//http://sel2in.com/pages/prog/html/ajax/confirm.html", "")
    }
    </script>
    </head>
    <body>
    <form  id="search" action="/search" method="get">
    <input type=button onclick=logout1() value=logout>
    </form >
    

    带有确认代码的示例页面->;div的任何有效html都可以是 确认。jsp 采取行动。。。你确定吗

    //这里的待办事项 span/pop-up/script/css->;就像一个iframe

    看起来您想要显示确认-一些用户界面询问您是否真的要注销

    最好的方法是创建一个id为“logoutConfirmSpan”的空span,然后单击注销,执行ajax(异步模式->;false,因此它发生在内联)获取html标记并将其设置为span的innerHTML

    html应该是有效的,这样它可以使UI出现,并使表单真正注销

    请参阅Replace inner HTML of a div with Ajax response告诉您如何将ajax与jQuery结合使用

    简单示例:

    HTML snippets

    <span id = 'logoutConfirmSpan'> </span>
    <script>
    // call ajax, with the response call setHtmlForConfirm
    function setHtmlForConfirm(req)
    {
        document.getElementById('logoutConfirmSpan').innerHTML = req.responseText;
    }
    //req.responseText should have the html to show the confirm UI and form to submit yes/ no
    </script>
    
  5. # 5 楼答案

    AJAX是您正在寻找的

    我强烈建议您使用jQueryshowcase -> div)执行AJAX调用,但是在Struts2中,您可以立即尝试使用弃用的Dojo插件,无需下载任何库,这只是为了让您有所了解

    <%@ taglib prefix="s"  uri="/struts-tags"      %>
    <%@ taglib prefix="sx" uri="/struts-dojo-tags" %>
    
    <head>      
        <sx:head/>
        <script>
           function loadAjax(){
               var div = dojo.widget.byId("myDiv");
               div.href = "yourAjaxAction.action";
               div.refresh();
           }
        </script>
    </head>
    <body>
        <input type="button" 
               value="Include a JSP fragment dynamically" 
               onclick="loadAjax();"/>
    
        <sx:div id="myDiv"> </sx:div>
    
    </body>
    

    然后,yourAjaxAction.action必须在成功时返回要包含的JSP代码段

  6. # 6 楼答案

    您不能通过单击客户端来响应JSP,因为它是一种服务器端技术。您可以在发送页面之前在页面中包含所需的HTML,使用CSS隐藏该区域,然后使用JavaScript响应鼠标单击使其可见。在将页面发送到客户端之前,include可能已经发生在服务器上。你可以有这样的东西:

    <div id="confirmPopup" style="display:hidden;">
          <%@ include file="ConfirmationPopup.jsp" %>
    </div>
    <script>
      $('#logoutId').click(function(event) {
       document.getElementById("confirmPopup").style.display="block";
      });
    </script>