Spring MVC - 两次提供内容 [英] Spring MVC - Serving Content Twice

查看:122
本文介绍了Spring MVC - 两次提供内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一周的指导如何将内容服务器上传到我的网页***两次,因为切断内容一旦工作,使用Model或ModelAndView,但如果用户再次与页面交互,我希望它能够加载有关SAME PAGE的更多内容。

I've been looking for a week for some guidance on how to server content to my webpage ***twice because severing content once works, with Model or ModelAndView but if a user interacts with the page again I want it to load more content on SAME PAGE.

Java Spring后端方法获取作品帖子没有:

Java Spring back-end methods Get works Post doesn't:

@RequestMapping(value = "explorer", method = RequestMethod.GET)
public String redirectExplorer(@RequestParam(value = "param1", required = false) String name, Model model) {
    if (name != null) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
        OffenderDAO offenderDAO = (OffenderDAO) context.getBean("offenderDAO");
        Offender[] offenders = null;
        try {
            offenders = offenderDAO.requestOffenders(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ((ConfigurableApplicationContext) context).close();
        model.addAttribute("offenderlists", offenders);
    }
    return "explorer";
}

@RequestMapping(value = "explorer", method = RequestMethod.POST)
public String selectionHandler(@RequestParam("offenderid") String text, Model map) {
    String offenderID = text.trim();
    System.out.println("requested more info for: " + offenderID);
    Offender offender = null;
    ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
    OffenderDAO offenderDAO = (OffenderDAO) context.getBean("offenderDAO");
    try {
        offender = offenderDAO.findOffenderById(offenderID);
    } catch (Exception e) {
        e.printStackTrace();
    }
    ((ConfigurableApplicationContext) context).close();
    map.addAttribute("selectedOffender", offender);
    return "explorer";
}

好的,你可以看到GET方法加载一个用户可以列出的列表与之交互并将向所显示的其他方法发送回复。问题是,即使收到帖子并且数据存在于回复中(由println确认),网页也不会更新。

Ok so as you can see the GET method loads a list which the user can interact with and that will send a post response to the other method shown. The issue is that even though the post is received and the data is present in the response back(confirmed by println) the webpage doesn't update.

以下是负责上述模型更改的JSP代码。

Here is the JSP code responsible for the above model changes.

<ul class="result-class">
            <c:forEach var="offender" items="${offenderlists}">
                <li><div class="result-div">
                        <div class="result-img-holder">
                            <img class="lazy" src=<c:out value="${offender.linkToPicture}"/>
                                height="120" width="120" /> <span class=result-div-titles>
                                OffenderID: </span>
                            <c:out value="${offender.offenderId}" />
                        </div>
                        <div class="result-div-oinfo">
                            <ul class="info-list1">
                                <li><span class=result-div-titles> First Name: </span> <c:out
                                        value="${offender.firstName}" /></li>
                                <li><span class=result-div-titles> Last Name: </span> <c:out
                                        value="${offender.lastName}" /></li>
                                <li><span class=result-div-titles> Middle Name: </span> <c:out
                                        value="${offender.middleName}" /></li>
                                <li><span class=result-div-titles> DOB: </span> <c:out
                                        value="${offender.DOB}" /></li>
                                <li><span class=result-div-titles> Sex: </span> <c:out
                                        value="${offender.sex}" /></li>
                                <li><span class=result-div-titles> Risk Level: </span> <c:out
                                        value="${offender.riskLevel}" /></li>
                                <li><span class=result-div-titles> Designation: </span> <c:out
                                        value="${offender.designation}" /></li>
                            </ul>
                            <ul class="info-list2">
                                <li><span class=result-div-titles> Race: </span>
                                <c:out value="${offender.race}" /></li>
                                <li><span class=result-div-titles> Ethnicity: </span>
                                <c:out value="${offender.ethnicity}" /></li>
                                <li><span class=result-div-titles> Hair Color: </span>
                                <c:out value="${offender.hairColor}" /></li>
                                <li><span class=result-div-titles> Eye Color: </span>
                                <c:out value="${offender.eyeColor}" /></li>
                                <li><span class=result-div-titles> Glasses: </span>
                                <c:out value="${offender.correctiveLens}" /></li>
                            </ul>
                            <div class="result-address">
                                <span class=result-div-titles> Primary Address: </span><br>
                                <c:out value="${offender.primaryAddress.address_line1}" />
                                <br>
                                <c:out value="${offender.primaryAddress.city}" />
                                , New York
                                <c:out value="${offender.primaryAddress.zipcode}" />
                            </div>
                        </div>
                    </div></li>
            </c:forEach>
        </ul>


<div id="wide-sidebar">
            First Name: <c:out value="${selectedOffender.firstName}"/>
            <div id="vehicles-sidebar">
                <h1>Vehicles Registered:</h1>
                <ul>
                    <c:forEach  items="${selectedOffender.currentVehicles}" var="vehicle">
                        <li>
                        <span><c:out value="${vehicle.plate}"/></span>
                        <span style="margin:10px;">something1<c:out value="${vehicle.state}"/></span>
                        <span style="margin:10px;"><c:out value="${vehicle.color}"/></span>
                        <span style="margin:10px;"><c:out value="${vehicle.make}"/></span>
                        <span style="margin:10px;"><c:out value="${vehicle.model}"/></span>
                        <span style="margin:10px;"><c:out value="${vehicle.year}"/></span></li>
                    </c:forEach>
                </ul>
            </div>

问题回顾
我有两个列表加载单独的彼此基于一页上的用户交互。一个列表加载但另一个列表不加载。这不是我错过的代码......没有错误

Problem recap I have two lists that load seperate of each other based on user interaction on one page. One list loads but the other will not. It's not the code I am just missing something... There are no errors

目标
这里的目标是加载一个从数据库中选择的列表然后用户可以选择加载另一半网页的选项。但请注意,问题是选择加载正常但不是网页的其余部分。如果我首先加载网页的其余部分它可以工作,但是当请求时列表将不会加载。

Goal The goal here is to load a list of choices from a database and then the user can choose a choice which loads another half of the webpage. But note the problem is that the choices load fine but not the rest of webpage. If I load the rest of the webpage first it works but then the list will not load when requested.

推荐答案

所以我不得不创建一个新的JSP,并使用iframe ...想法灵感来自spotify web player lol;)

So I had to make a new JSP and use an iframe... idea inspired by spotify web player lol ;)

以下是我使用iframe的方式

Here is how I used an Iframe

<iframe src="wideSidebar" id = "wide-sidebar"></iframe> 
<script>
    $(document).ready(function() {
        $('.result-div').click(function() {
            $(this).fadeOut(1000).fadeIn(200);
            var text1 = $(this).parent().text().split(":")[1].split("\n")[1];
            $("#wide-sidebar").attr("src", "wideSidebar?id="+text1);
            $("#wide-sidebar").css("display", "block");
        });
    });
</script>
@RequestMapping(value = "wideSidebar", method = RequestMethod.GET)
    public ModelAndView getSideBar(@RequestParam(value = "id", required = false) String name) {
        ModelAndView map = new ModelAndView("wideSidebar");
        if (name != null) {
            String offenderID = name.trim();
            System.out.println("requested more info for: " + offenderID);
            Offender offender = null;
            ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
            OffenderDAO offenderDAO = (OffenderDAO) context.getBean("offenderDAO");
            try {
                offender = offenderDAO.findOffenderById(offenderID);
            } catch (Exception e) {
                e.printStackTrace();
            }
            ((ConfigurableApplicationContext) context).close();
            map.addObject("selected", offender);
        }
        return map;
    }

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
First Name: <c:out value="${selected.firstName}"/>
            <div id="vehicles-sidebar">
                <h1>Vehicles Registered:</h1>
                <ul>
                    <c:forEach  items="${selected.currentVehicles}" var="vehicle">
                        <li>
                        <span><c:out value="${vehicle.plate}"/></span>
                        <span style="margin:10px;"><c:out value="${vehicle.state}"/></span>
                        <span style="margin:10px;"><c:out value="${vehicle.color}"/></span>
                        <span style="margin:10px;"><c:out value="${vehicle.make}"/></span>
                        <span style="margin:10px;"><c:out value="${vehicle.model}"/></span>
                        <span style="margin:10px;"><c:out value="${vehicle.year}"/></span></li>
                    </c:forEach>
                </ul>
        </div>
</body>
</html>

这篇关于Spring MVC - 两次提供内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆