jQuery 如何在 Spring MVC 中使用 AJAX 渲染视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4816080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:58:59  来源:igfitidea点击:

How to render a View using AJAX in Spring MVC

jqueryajaxspring-mvc

提问by Neuquino

I'm using Spring MVC and I need to make an asynchronous call to the server and refresh only a piece of the page.

我正在使用 Spring MVC,我需要对服务器进行异步调用并仅刷新页面的一部分。

What I actually have is a Controller that returns a String. I call the Controller using JQuery (.post()) function.

我实际上拥有的是一个返回字符串的控制器。我使用 JQuery ( .post()) 函数调用控制器。

The problem with my solution is that I'm not able to render a JSP like I do when I use ModelAndView as return type.

我的解决方案的问题是我无法像使用 ModelAndView 作为返回类型时那样呈现 JSP。

Is there any way to return a View already rendered?

有没有办法返回已经呈现的视图?

Thanks in advance.

提前致谢。

Neuquino

纽基诺

采纳答案by axtavt

You can put this piece of page into a separate JSP and return a ModelAndViewpointing to it from your method. There are no difference between AJAX and non-AJAX calls from that point.

您可以将这段页面放入一个单独的 JSP 中,并ModelAndView从您的方法中返回一个指向它的指针。从这一点来看,AJAX 调用和非 AJAX 调用之间没有区别。

回答by Bane

This answer is to just confirm that the answer by axtavt works. It took me a minute to realize what he was suggesting, so I thought I'd post a code-snippet to help out anyone coming along behind me. Kudos go to him, though! :)

这个答案只是确认 axtavt 的答案有效。我花了一分钟才意识到他的建议,所以我想我会发布一个代码片段来帮助我身后的任何人。不过还是要为他点赞!:)



MyController.java

我的控制器

@Controller
public class MyController {

    @RequestMapping( method=RequestMethod.GET, value="/mainView" )
    public ModelAndView getMainView( ... ) {        
        /* do all your normal stuff here to build your primary NON-ajax view
         * in the same way you always do
         */             
    }

    /* this is the conroller's part of the magic; I'm just using a simple GET but you
     * could just as easily do a POST here, obviously
     */
    @RequestMapping( method=RequestMethod.GET, value="/subView" )
    public ModelAndView getSubView( Model model ) {
        model.addAttribute( "user", "Joe Dirt" );
        model.addAttribute( "time", new Date() );
        return new ModelAndView( "subView" );
    }

}



mainView.jsp

主视图.jsp

(...)

<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
    function doAjaxPost() {

        $.ajax({
            type: "GET",
            url: "subView",
            success: function(response) {
                $("#subViewDiv").html( response );
            }
        });
    }
</script>
<input type="button" value="GO!" onclick="doAjaxPost();" />
<div id="subViewDiv"></div>

(...)



subView.jsp

子视图.jsp

(...)

<h3>
    User Access Details
</h3>

<p>
    ${user} accessed the system on ${time}
</p>

(...)



And that's it! A thing of beauty; up to now, doing AJAX in Spring has been a huge pain... parsing big @ResponseBody's, building huge sets of HTML by concatenating stuff in JS... ugh... I can't believe how simple and awesome this approach is -- and wasn't aware of it until just now! :)

就是这样!美丽的事物;到目前为止,在 Spring 中使用 AJAX 一直是一个巨大的痛苦......解析大@ResponseBody,通过在 JS 中连接东西来构建大量的 HTML......呃......我不敢相信这种方法是多么简单和棒极了——而且直到现在才知道!:)