springMVC項(xiàng)目中遇到要使用jqueryAjax向前臺(tái)傳遞動(dòng)態(tài)表格的需求,原來的做法是在js中接收數(shù)據(jù)然后拼接成表格再給jsp顯示,后來在這個(gè)國外的網(wǎng)站上看到了如下的使用“模板”的設(shè)計(jì),覺得很是合理,自己測(cè)試了一下,覺得比之前js中拼接好用很多,大大減少了js的壓力。我就直接復(fù)制原作者的回答了(回答真的很詳細(xì)了),記錄一下,感覺自己又成長了。
目前創(chuàng)新互聯(lián)公司已為1000多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、虞城網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
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
(...) (...)
subView.jsp
(...)User Access Details
${user} accessed the system on ${time}
(...)
下面是原文的地址:
http://stackoverflow.com/questions/4816080/how-to-render-a-view-using-ajax-in-spring-mvc
感謝這位大神!也感謝我偉大的遠(yuǎn)哥!