2019独角兽企业重金招聘Python工程师标准>>>
Spring MVC 异步交互demo:
1.jsp页面:
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="js/jquery-2.1.3.js"></script>
<script type="text/javascript" src="js/jquery-2.1.3.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>Insert title here</title>
<script type="text/javascript">function ajaxTest(){$.ajax({data:"name="+$("#name").val(),type:"GET",dataType: 'json',url:"user/login.do",error:function(data){alert("出错了!!:"+data.msg);},success:function(data){alert("success:"+data.msg);$("#result").html(data.msg) ;}});}
</script>
</head>
<body><input type="text" name="name" id="name"/><input type="submit" value="登录" onclick="ajaxTest();"/><div id="result"></div>
</body>
</html>
spring如何使用ajax、2.controller:
package xm.zjl.controller;import java.io.IOException;
import java.util.HashMap;
import java.util.Map;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;/*** 登录controller* * @author Administrator**/
@Controller
@RequestMapping("/user/*")
public class LoginController {@RequestMapping(value="login.do")public @ResponseBody Map<String,Object> login(HttpServletRequest request,HttpServletResponse response) throws IOException{System.out.println(request.getParameter("name"));Map<String,Object> map = new HashMap<String,Object>();if(request.getParameter("name").equals("123")){System.out.println("城东");map.put("msg", "成功");}else{System.out.println("失败");map.put("msg", "失败");}return map;}}