springmvc ajax 簡單例子


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頁面


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM