欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-08 10:07:22
spring mvc 异步交互demo: 1.jsp页面: <%@ page language="java" contenttype="text/...

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> 

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

3.pom文件:

<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文件:

<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文件:

<?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> 

这里需要注意的是:

<servlet-mapping> 
  <servlet-name>spring</servlet-name> 
  <url-pattern>*.do</url-pattern> 
 </servlet-mapping> 

如果写成:

<servlet-mapping> 
  <servlet-name>spring</servlet-name> 
  <url-pattern>/</url-pattern> 
 </servlet-mapping> 

会提示:$ is not defined错误

记录一下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。