简体   繁体   English

我可以重定向到其他网页内的网页吗?

[英]Can I redirect to a webpage inside another webpage?

For starters, I am sorry if my question title is confusing. 首先,如果我的问题标题令人困惑,我很抱歉。 I wasn't sure about the best way to word it. 我不确定最好的说法。 I also apologize if something like this has been asked and answered before - I tried searching but I was unable to find anything related. 如果之前有人问过并回答过这样的问题,我也很抱歉 - 我尝试过搜索,但我无法找到任何相关信息。

The problem: I am building out a permissions section for a warehouse application - it starts with Features (such as Receiving) which has Functions (such as Order Unloading). 问题:我正在为仓库应用程序构建一个权限部分 - 它以具有功能(例如订单卸载)的功能(例如接收)开始。 There's more to it than that of course but this should be all that's needed for this. 当然还有更多,但这应该是所需要的。

The Features tab has a webpage with a set of tabs - one of these tabs allows me to look at the list of Functions under a Feature, and add/delete Functions (if permission allows). 功能选项卡有一个带有一组选项卡的网页 - 其中一个选项卡允许我查看功能下的功能列表,以及添加/删除功能(如果允许)。 All of the styling is located on this first page, as the other pages are loaded in through the tabs. 所有样式都位于此第一页,因为其他页面通过选项卡加载。 When I update the Functions page (add or delete a Function) my Action does a 当我更新功能页面(添加或删除功能)时,我的操作会执行

return response.sendRedirect("url-for-webpage-here");

that redirects the page back to the same screen with the updated data. 使用更新的数据将页面重定向回同一屏幕。

The problem here is that this page loads outside of the main page with all the styling. 这里的问题是该页面在主页面之外加载了所有样式。

Here is the basics of the main page (feature.jsp): 以下是主页面的基础知识(feature.jsp):

<div class="d-flex flex-row mt-2">
    <div class="col-2 nav-tabs--div">
        <ul class="nav nav-tabs nav-tabs--vertical nav-tabs--left" role="navigation">
            <li class="nav-item">
                <a class="nav-link btn-block active" id="featureTab" data-toggle="tab" href="#featureSummary" role="tab" aria-controls="featureSummary">Feature Details</a>
            </li>
            <li class="nav-item">
                <a class="nav-link btn-block" id="featureFunctionsTab" data-target="#featureFunctions" data-toggle="tabajax" href="/Security/functions.do?method=load&featureID=<%=feature.getFeatureID()%>" role="tab" aria-controls="featureFunctions">Feature Functions</a>
            </li>
        </ul>
    </div>
    <div class="tab-content col-10">
        <div class="tab-pane fade show active" id="featureSummary" role="tabpanel">
            <!-- Stuff goes here -->
        </div>
        <!-- Blank div for AJAX loading of functions -->
        <div class="tab-pane fade" id="featureFunctions" role="tabpanel">

        </div>
    </div>
</div>

Choosing the second nav-item (Feature Functions) will take you to the second tab, inserting its data from the .jsp it uses into <div id="featureFunctions"></div> 选择第二个导航项(特征函数)将转到第二个选项卡,将其使用的.jsp中的数据插入<div id="featureFunctions"></div>

On this page there is a button to add a new Function: 在此页面上有一个添加新功能的按钮:

<a href="/Security/modal_addFunction.jsp?featureID=<%=featureID%>" data-toggle="modal" data-target="#addFunctionModal" data-id="<%=featureID%>" class="addFunction btn btn-md btn-primary float-right mr-1 mt-1"><i class="fa fa-plus"></i>&nbsp;Add New Function</a>

Modal pops up, fill it out and hit the "Add" button and the function executes as expected. 弹出模态,填写并点击“添加”按钮,函数按预期执行。 At the end of the function is: 在功能的最后是:

response.sendRedirect("/Security/functions.do?method=load&featureID="+function.getFeatureID());

This is the exact same call that is made when clicking the tab on the first page, but since it does not go through this same page, the webpage shows up with no styling at all. 这是单击第一页上的选项卡时完全相同的调用,但由于它没有通过同一页面,因此网页显示时根本没有样式。

I am looking for a way to send this redirect through the original page that is loaded (feature.jsp) and then default to the second tab (Feature Functions) so the user doesn't have to click back through to see the list of Functions for this feature, if this is even possible. 我正在寻找一种方法来通过加载的原始页面(feature.jsp)发送此重定向,然后默认为第二个选项卡(功能函数),这样用户就不必单击返回查看功能列表对于此功能,如果这是可能的。 I'm really not sure what my options are here, so again I apologize if this has been covered before! 我真的不确定我的选择是什么,所以我再次道歉,如果之前已经涵盖了这一点!

After a lot of searching and discussions with colleagues, it turns out that the answer is AJAX, and that I was doing it wrong. 经过与同事的大量搜索和讨论,结果证明答案是AJAX,而且我做错了。

On the page mentioned above, modal_addFunction.jsp the action for the form built on the page is set up to direct to a function that will add the new Function to the database. 在上面提到的页面上, modal_addFunction.jsp将页面上构建的表单的操作设置为指向将新函数添加到数据库的函数。 It looks like this: 它看起来像这样:

<form name="newFunctionForm" id="newFunctionForm" method="post" action="/Security/functions.do?method=addNewFunction">
    <!-- Form goes here -->
    <div class="modal-footer">
        <button id="closeModalButton" type="button" class="btn btn-secondary" data-dismiss="modal"><i class="fa fa-times" aria-hidden="true"></i>&nbsp;Close</button>
        <button type="submit" class="btn btn-primary" id="addFunctionSubmit"><i class="fa fa-plus" aria-hidden="true"></i>&nbsp;Add</button>
    </div>
</form>

The action for this form will be prevented in the AJAX call, but we'll get to that. 在AJAX调用中将阻止此表单的操作,但我们将会这样做。

In the action, the function looks like this: 在动作中,函数如下所示:

    public ActionForward addNewFunction(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession(false);
        if(SecurityEJBServices.checkSession(session, request, response))
            return null;
        FunctionsServices fs = new FunctionsServices();
        FunctionForm functionForm = new FunctionForm(request);
        try{
            Function function = new Function();
            //set Function stuff here

            boolean added = fs.addFunction(function);

            PrintWriter out = response.getWriter();
            if(added){
                out.write("success");
            }else{
                out.write("error");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

The PrintWriter writes the response back to the AJAX function that gets built back in the modal. PrintWriter将响应写回到在模态中内置的AJAX函数。 It looks like this: 它看起来像这样:

<script type="text/javascript">
$(document).ready(function(){
    $(function(){
        $('#newFunctionForm').submit(function(e){
            e.preventDefault();
            var form = $(this);
            var post_url = form.attr('action');
            var post_data = form.serialize();
            $.ajax({
                type: 'POST',
                async: false,
                url: post_url,
                data: post_data,
                success: function(data){
                    console.log("DATA: " + data);
                    if(data == 'success'){
                        $('#errorDiv').hide();
                        $('#addFunctionModal').modal('hide');
                        $('#functionTable').load("/Security/functions.do?method=getFunctionsAjax&featureID=<%=featureID%>");
                        $('.modal-backdrop').remove();
                    }else{
                        $('#errorDiv').show();
                    }
                }
            });
        });
    });
});
</script>

The #functionTable table lives in a completely separate jsp as the div that it lives in, and gets referenced with a <jsp:include> call that will load the table that exists in that jsp into the div. #functionTable表作为它所在的div存在于一个完全独立的jsp中,并通过<jsp:include>调用引用,该调用将该jsp中存在的表加载到div中。 This was my error. 这是我的错误。 I had assumed that the entire div being in a separate jsp was enough, but I had to go farther and put the table in another separate jsp to allow just the data to be refreshed and reloaded. 我假设整个div在一个单独的jsp中就足够了,但我必须走得更远,把表放在另一个单独的jsp中,以便只刷新和重新加载数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM