1.控制器曾
1 @Controller 2 public class AjaxController { 3 @RequestMapping("/ajax") 4 public void ajax(HttpServletRequest req,HttpServletResponse resq) throws Exception{ 5 resq.getWriter().print("ajax data wxm"); 6 } 7 8 }
2.jsp页面请求:
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 <script type="text/javascript" src="js/jquery-1.11.3.min.js"></script> 22 <script type="text/javascript"> 23 $(function(){ 24 $('#btn').click(function(){ 25 $.post("ajax.do",function(data){ 26 $("#content").html(data); 27 }); 28 }); 29 }); 30 </script> 31 32 </head> 33 34 <body> 35 <input type="button" id="btn" value="ajax"/><br> 36 <div id="content"></div> 37 </body> 38 </html>
通过访问项目,点击ajax按钮,调用ajax请求,请求回传controllor的数据到jsp页面