Spring MVC - 两次提供内容

时间:2015-10-06 16:22:30

标签: javascript java html spring-mvc

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

Java Spring后端方法获取作品帖子没有&#t; 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方法加载了一个用户可以与之交互的列表,并且会向所显示的其他方法发送一个post响应。问题在于,即使收到帖子并且数据存在于回复中(由println确认),网页也不会更新。

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

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

问题回顾 我有两个列表,根据一个页面上的用户交互加载彼此分开。一个列表加载但另一个列表不加载。它不是我错过的代码......没有错误

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

1 个答案:

答案 0 :(得分:0)

所以我不得不制作一个新的JSP并使用iframe ...这个想法受到spotify web player lol的启发;)

以下是我使用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>