3

I am trying to review the code in Spring Show Case from git

https://github.com/SpringSource/spring-mvc-showcase

But I am missing something on the first page if you click on the simple link you get back "Hello World" in green.

So I looked in the JSP page and I found this code:

<ul>
            <li>
                <a id="simpleLink" class="textLink" href="<c:url value="/simple" />">GET /simple</a>
            </li>
            <li>
                <a id="simpleRevisited" class="textLink" href="<c:url value="/simple/revisited" />">GET /simple/revisited</a>
            </li>
        </ul>

Where does the call to the contoller but I dont see how the JSP page knows where to put the "Hello World" in green.

I then reviewed the control and I found:

@Controller
public class SimpleController {

    @RequestMapping("/simple")
    public @ResponseBody String simple() {
        return "Hello world!";
    }

}

Which sents the "Hello World" out to the JSP but how does the JSP know where to put it? I dont see any tag

4

1 回答 1

4

看起来魔术正在JSP 代码底部的 jQuery 中发生。这对于 Hello World 教程来说有点复杂,但这不是重点。

单击链接后,使用 AJAX 从服务器请求数据:

$("a.textLink").click(function(){
    var link = $(this);
    $.ajax({ url: link.attr("href"), dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, link); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, link); }});
    return false;
});

最终,通过 JavaScript 方法,您到达了设置响应元素并在页面上显示Hello World消息的这段代码。

MvcUtil.showResponse = function(type, text, element) {
    var responseElementId = element.attr("id") + "Response";
    var responseElement = $("#" + responseElementId);
    if (responseElement.length == 0) {
        responseElement = $('<span id="' + responseElementId + '" class="' + type + '" style="display:none">' + text + '</span>').insertAfter(element);
    } else {
        responseElement.replaceWith('<span id="' + responseElementId + '" class="' + type + '" style="display:none">' + text + '</span>');
    responseElement = $("#" + responseElementId);
    }
    responseElement.fadeIn("slow");
};
于 2013-03-07T13:30:14.353 回答