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

转载于:https://www.cnblogs.com/wxm-bk/p/6562384.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值