springmvc ajax 简单例子

ajax.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  <%
  String path = request.getContextPath();
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  %>
  
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
    <head>
     <base href="<%=basePath%>">
     
     <title>My JSP 'index.jsp' starting page</title>
     <meta http-equiv="pragma" content="no-cache">
     <meta http-equiv="cache-control" content="no-cache">
     <meta http-equiv="expires" content="0">    
     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
     <meta http-equiv="description" content="This is my page">
     <!--
     <link rel="stylesheet" type="text/css" href="styles.css">
     -->
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.11.3.min.js"></script>
       <script type="text/javascript">
       $(function(){
           $('#btn').click(function(){
        	   var url="${pageContext.request.contextPath }/ajax.action";
               $.post(url,function(data){
            	   if(data && data == "ajax data wxm"){
            		   alert("ajax data wxm!!!!")
            		   
            	   }
            	   
                   
               });
           });
       });
       </script>
   
   </head>
   
   <body>
    <input type="button" id="btn" value="ajax"/><br>
    <div id="content"></div>
   </body>
 </html>


Controller层:

@Controller
@Scope(value = "prototype")
public class WebUserController { 
        @RequestMapping("/ajax")
	 public void ajax(HttpServletRequest req,HttpServletResponse resq) throws Exception{
	       resq.getWriter().print("ajax data wxm");
	 }
}
	

通过访问项目,点击ajax按钮,调用ajax请求,请求回传controllor的数据到jsp页面


猜你喜欢

转载自blog.csdn.net/swy18929564409/article/details/80897134