【问题标题】:Where is the "Hello World" coming from in Spring MVC SamplesSpring MVC 示例中的“Hello World”来自哪里
【发布时间】:2013-02-22 17:14:25
【问题描述】:

我正在尝试从 git 查看 Spring Show Case 中的代码

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

但是,如果您单击简单的链接,您会返回绿色的“Hello World”,但我在第一页上遗漏了一些东西。

于是我查看了JSP页面,发现了这段代码:

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

对控制器的调用在哪里,但我看不到 JSP 页面如何知道将“Hello World”置于绿色的位置。

然后我查看了控件,发现:

@Controller
public class SimpleController {

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

}

它将“Hello World”发送到 JSP,但是 JSP 怎么知道把它放在哪里?我没有看到任何标签

【问题讨论】:

    标签: java jquery spring spring-mvc


    【解决方案1】:

    看起来魔术正在JSP code 底部的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");
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多