spring mvc 和ajax异步交互完整实例(转自CSDN) 附下载地址

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring mvc 和ajax异步交互完整实例(转自CSDN) 附下载地址相关的知识,希望对你有一定的参考价值。


spring mvc 和ajax异步交互完整实例

spring MVC 异步交互demo:

demo下载地址:http://download.csdn.net/download/quincylk/9521375












1.jsp页面: [java] view plain copy print? <%@ 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>

2.controller: [java] view plain copy print? 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; } }

3.pom文件: [java] view plain copy print? <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>xiaoma</groupId> <artifactId>zjl</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>zjl Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> </dependencies> <build> <finalName>zjl</finalName> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <configuration> <stopPort>9966</stopPort> <stopKey>foo</stopKey> <scanIntervalSeconds>0</scanIntervalSeconds> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>8088</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> <webAppConfig> <contextPath>/</contextPath> </webAppConfig> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8088</port> <path>/</path> <uriEncoding>UTF-8</uriEncoding> </configuration> </plugin> </plugins> </build> </project> 这里注意如果相关json包没有添加到pom.xml文件中会报:406 not acceptable

4.spring-servlet.xml文件: [html] view plain copy print? <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射--> <mvc:annotation-driven /> <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean --> <context:component-scan base-package="xm.zjl.controller" /> <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" /> </beans>


5.web.xml文件: [html] view plain copy print? <?xml version="1.0" encoding="UTF-8"?> <web-app 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"> <context-param> <param-name>contextConfigLocation</param-name> <!-- 应用上下文配置文件 --> <param-value>/WEB-INF/spring-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置spring核心servlet --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- url-pattern配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的访问 --> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> 这里需要注意的是: [html] view plain copy print? <span style="color:#ff9900;"><strong> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </strong></span> 如果写成: [html] view plain copy print? <span style="color:#ff9900;"><strong> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </strong></span> 会提示:$ is not defined错误

 

 

spring mvc 和ajax异步交互完整实例

标签: jqueryajaxspring mvc406 not acceptableis not defined
 38922人阅读 评论(7) 收藏 举报
技术分享 分类:

spring MVC 异步交互demo:

1.jsp页面:

 

[java] view plain copy
 
 print?
  1. <%@ page language="java" contentType="text/html; charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <script type="text/javascript" src="js/jquery-2.1.3.js"></script>  
  7. <script type="text/javascript" src="js/jquery-2.1.3.min.js"></script>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=utf8">  
  9. <title>Insert title here</title>  
  10. <script type="text/javascript">  
  11.     function ajaxTest(){  
  12.         $.ajax({  
  13.         data:"name="+$("#name").val(),  
  14.         type:"GET",  
  15.         dataType: ‘json‘,  
  16.         url:"user/login.do",  
  17.         error:function(data){  
  18.             alert("出错了!!:"+data.msg);  
  19.         },  
  20.         success:function(data){  
  21.             alert("success:"+data.msg);  
  22.             $("#result").html(data.msg) ;  
  23.         }  
  24.         });  
  25.     }  
  26. </script>  
  27. </head>  
  28. <body>  
  29.     <input type="text" name="name" id="name"/>  
  30.     <input type="submit" value="登录" onclick="ajaxTest();"/>  
  31.     <div id="result"></div>  
  32. </body>  
  33. </html>  
2.controller:
[java] view plain copy
 
 print?
  1. package xm.zjl.controller;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6.   
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9.   
  10. import org.springframework.stereotype.Controller;  
  11. import org.springframework.web.bind.annotation.RequestMapping;  
  12. import org.springframework.web.bind.annotation.ResponseBody;  
  13.   
  14. /** 
  15.  * 登录controller 
  16.  *  
  17.  * @author Administrator 
  18.  * 
  19.  */  
  20. @Controller  
  21. @RequestMapping("/user/*")  
  22. public class LoginController {  
  23.     @RequestMapping(value="login.do")  
  24.     public @ResponseBody Map<String,Object> login(HttpServletRequest request,HttpServletResponse response) throws IOException{  
  25.         System.out.println(request.getParameter("name"));  
  26.         Map<String,Object> map = new HashMap<String,Object>();  
  27.           
  28.         if(request.getParameter("name").equals("123")){  
  29.             System.out.println("城东");  
  30.             map.put("msg""成功");  
  31.         }else{  
  32.             System.out.println("失败");  
  33.             map.put("msg""失败");  
  34.         }  
  35.         return map;  
  36.     }  
  37.       
  38. }  
3.pom文件:

 

 

[java] view plain copy
 
 print?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>xiaoma</groupId>  
  5.     <artifactId>zjl</artifactId>  
  6.     <packaging>war</packaging>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <name>zjl Maven Webapp</name>  
  9.     <url>http://maven.apache.org</url>  
  10.     <dependencies>  
  11.         <dependency>  
  12.             <groupId>junit</groupId>  
  13.             <artifactId>junit</artifactId>  
  14.             <version>3.8.1</version>  
  15.             <scope>test</scope>  
  16.         </dependency>  
  17.         <dependency>  
  18.             <groupId>org.springframework</groupId>  
  19.             <artifactId>spring-webmvc</artifactId>  
  20.             <version>4.1.0.RELEASE</version>  
  21.         </dependency>  
  22.         <dependency>  
  23.             <groupId>org.springframework</groupId>  
  24.             <artifactId>spring-web</artifactId>  
  25.             <version>4.1.0.RELEASE</version>  
  26.         </dependency>  
  27.         <dependency>  
  28.             <groupId>com.fasterxml.jackson.core</groupId>  
  29.             <artifactId>jackson-databind</artifactId>  
  30.             <version>2.5.0</version>  
  31.         </dependency>  
  32.         <dependency>  
  33.             <groupId>commons-beanutils</groupId>  
  34.             <artifactId>commons-beanutils</artifactId>  
  35.             <version>1.9.2</version>  
  36.         </dependency>  
  37.         <dependency>  
  38.             <groupId>org.codehaus.jackson</groupId>  
  39.             <artifactId>jackson-mapper-asl</artifactId>  
  40.             <version>1.9.13</version>  
  41.         </dependency>  
  42.   
  43.         <dependency>  
  44.             <groupId>org.codehaus.jackson</groupId>  
  45.             <artifactId>jackson-core-asl</artifactId>  
  46.             <version>1.9.13</version>  
  47.         </dependency>  
  48.     </dependencies>  
  49.     <build>  
  50.         <finalName>zjl</finalName>  
  51.         <plugins>  
  52.             <plugin>  
  53.                 <groupId>org.mortbay.jetty</groupId>  
  54.                 <artifactId>jetty-maven-plugin</artifactId>  
  55.                 <configuration>  
  56.                     <stopPort>9966</stopPort>  
  57.                     <stopKey>foo</stopKey>  
  58.                     <scanIntervalSeconds>0</scanIntervalSeconds>  
  59.                     <connectors>  
  60.                         <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">  
  61.                             <port>8088</port>  
  62.                             <maxIdleTime>60000</maxIdleTime>  
  63.                         </connector>  
  64.                     </connectors>  
  65.                     <webAppConfig>  
  66.                         <contextPath>/</contextPath>  
  67.                     </webAppConfig>  
  68.                 </configuration>  
  69.             </plugin>  
  70.   
  71.             <plugin>  
  72.                 <groupId>org.apache.tomcat.maven</groupId>  
  73.                 <artifactId>tomcat7-maven-plugin</artifactId>  
  74.                 <version>2.2</version>  
  75.                 <configuration>  
  76.                     <port>8088</port>  
  77.                     <path>/</path>  
  78.                     <uriEncoding>UTF-8</uriEncoding>  
  79.                 </configuration>  
  80.             </plugin>  
  81.         </plugins>  
  82.     </build>  
  83. </project>  

 

 

这里注意如果相关json包没有添加到pom.xml文件中会报:406 not acceptable 
4.spring-servlet.xml文件:

 

[html] view plain copy
 
 print?
  1. <beans xmlns="http://www.springframework.org/schema/beans"    
  2.  xmlns:context="http://www.springframework.org/schema/context"    
  3.  xmlns:p="http://www.springframework.org/schema/p"    
  4.  xmlns:mvc="http://www.springframework.org/schema/mvc"    
  5.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  6.  xsi:schemaLocation="http://www.springframework.org/schema/beans    
  7.       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  8.       http://www.springframework.org/schema/context    
  9.       http://www.springframework.org/schema/context/spring-context.xsd    
  10.       http://www.springframework.org/schema/mvc    
  11.       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">    
  12.      <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->    
  13.      <mvc:annotation-driven />    
  14.      <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->    
  15.      <context:component-scan base-package="xm.zjl.controller" />    
  16.      <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->    
  17.      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />    
  18. </beans>    
5.web.xml文件:

 

 

[html] view plain copy
 
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <web-app     
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.     xmlns="http://java.sun.com/xml/ns/javaee"     
  5.     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    
  6.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"    
  7.     id="WebApp_ID"     
  8.     version="3.0">    
  9.     <context-param>    
  10.         <param-name>contextConfigLocation</param-name>    
  11.         <!-- 应用上下文配置文件 -->    
  12.         <param-value>/WEB-INF/spring-servlet.xml</param-value>    
  13.     </context-param>    
  14.     <listener>    
  15.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
  16.     </listener>    
  17.     <!-- 配置spring核心servlet -->    
  18.     <servlet>    
  19.         <servlet-name>spring</servlet-name>    
  20.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
  21.         <load-on-startup>1</load-on-startup>    
  22.     </servlet>    
  23.     <!-- url-pattern配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的访问 -->    
  24.     <servlet-mapping>    
  25.         <servlet-name>spring</servlet-name>    
  26.         <url-pattern>*.do</url-pattern>    
  27.     </servlet-mapping>    
  28. </web-app>   
这里需要注意的是:

 

 

[html] view plain copy
 
 print?
  1. <span style="color:#ff9900;"><strong> <servlet-mapping>    
  2.         <servlet-name>spring</servlet-name>    
  3.         <url-pattern>*.do</url-pattern>    
  4.     </servlet-mapping>  </strong></span>  
如果写成:
[html] view plain copy
 
 print?
  1. <span style="color:#ff9900;"><strong> <servlet-mapping>    
  2.         <servlet-name>spring</servlet-name>    
  3.         <url-pattern>/</url-pattern>    
  4.     </servlet-mapping>  </strong></span>  
会提示:$ is not defined错误
























以上是关于spring mvc 和ajax异步交互完整实例(转自CSDN) 附下载地址的主要内容,如果未能解决你的问题,请参考以下文章

ajax异步请求获取后台数据,java mvc 后台应该如何封装各种实体类数据?

Spring Mvc模式下Jquery Ajax 与后台交互操作

325 AJAX介绍,XMLHttpRequest对象,实现Ajax的异步交互步骤,服务器端通信状态

Spring MVC异常处理代码完整实例

Ajax学习

求教Spring mvc 处理 ajax问题,在线等