标签:jquery ajax
springMVC项目中遇到要使用jqueryAjax向前台传递动态表格的需求,原来的做法是在js中接收数据然后拼接成表格再给jsp显示,后来在这个国外的网站上看到了如下的使用“模板”的设计,觉得很是合理,自己测试了一下,觉得比之前js中拼接好用很多,大大减少了js的压力。我就直接复制原作者的回答了(回答真的很详细了),记录一下,感觉自己又成长了。
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
(...) <h3> User Access Details </h3> <p> ${user} accessed the system on ${time} </p> (...)
下面是原文的地址:
http://stackoverflow.com/questions/4816080/how-to-render-a-view-using-ajax-in-spring-mvc
感谢这位大神!也感谢我伟大的远哥!
本文出自 “java程序猿的博客” 博客,请务必保留此出处http://chengxuyuan.blog.51cto.com/5789198/1598960
标签:jquery ajax
原文地址:http://chengxuyuan.blog.51cto.com/5789198/1598960