天天看點

ajax執行個體 spring,spring mvc 和ajax異步互動完整執行個體

Spring MVC 異步互動demo:

1.jsp頁面:

pageEncoding="utf-8"%>

html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

Insert title here

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) ;

}

});

}

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

@RequestMapping("/user/*")

public class LoginController {

@RequestMapping(value="login.do")

public @ResponseBody Map login(HttpServletRequest request,HttpServletResponse response) throws IOException{

System.out.println(request.getParameter("name"));

Map map = new HashMap();

if(request.getParameter("name").equals("123")){

System.out.println("城東");

map.put("msg", "成功");

}else{

System.out.println("失敗");

map.put("msg", "失敗");

}

return map;

}

}