Spring MVC 异步交互demo:
1.jsp页面:
Insert title here$.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
*
* @author Administrator
**/@Controller
@RequestMapping("/user/*")public classLoginController {
@RequestMapping(value="login.do")public @ResponseBody Maplogin(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", "失败");
}returnmap;
}
}
3.pom文件:
4.0.0
xiaoma
zjl
war
0.0.1-SNAPSHOT
zjl Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
org.springframework
spring-webmvc
4.1.0.RELEASE
org.springframework
spring-web
4.1.0.RELEASE
com.fasterxml.jackson.core
jackson-databind
2.5.0
commons-beanutils
commons-beanutils
1.9.2
org.codehaus.jackson
jackson-mapper-asl
1.9.13
org.codehaus.jackson
jackson-core-asl
1.9.13
zjl
org.mortbay.jetty
jetty-maven-plugin
9966
foo
0
8088
60000
/
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
8088
/
UTF-8
这里注意如果相关json包没有添加到pom.xml文件中会报:406 not acceptable
4.spring-servlet.xml文件:
5.web.xml文件:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0">
contextConfigLocation
/WEB-INF/spring-servlet.xml
org.springframework.web.context.ContextLoaderListener
spring
org.springframework.web.servlet.DispatcherServlet
1
spring
*.do
这里需要注意的是:
spring
*.do
如果写成:
spring
/
会提示:$ is not defined错误
记录一下