溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring MVC中jquery ajax傳遞view

發(fā)布時間:2020-06-24 19:00:47 來源:網(wǎng)絡(luò) 閱讀:713 作者:java程序猿 欄目:web開發(fā)

    springMVC項(xiàng)目中遇到要使用jqueryAjax向前臺傳遞動態(tài)表格的需求,原來的做法是在js中接收數(shù)據(jù)然后拼接成表格再給jsp顯示,后來在這個國外的網(wǎng)站上看到了如下的使用“模板”的設(shè)計(jì),覺得很是合理,自己測試了一下,覺得比之前js中拼接好用很多,大大減少了js的壓力。我就直接復(fù)制原作者的回答了(回答真的很詳細(xì)了),記錄一下,感覺自己又成長了。

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, value="/subView" )
    public ModelAndView getSubView( Model model ) {
        model.addAttribute( "user", "Joe Dirt" );
        model.addAttribute( "time", new Date() );
        return new ModelAndView( "subView" );
    }

}

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

(...)

<h4>
    User Access Details
</h4>

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

(...)


下面是原文的地址:

http://stackoverflow.com/questions/4816080/how-to-render-a-view-using-ajax-in-spring-mvc


感謝這位大神!也感謝我偉大的遠(yuǎn)哥!





向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI