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

网友投稿 208 2023-06-15


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

spring MVC 异步交互demo:

1.jsp页面:

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

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

}

}

3.pom文件:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

xiaoma

zjl

war

0.0.1-SNAPSHOT

zjl Maven Webapp

http://maven.QTgGuFJXRWapache.org

junit

junit

3.8.1

test

org.springframework

spring-webmvc

4.1.0.RELEASE

org.springframework

spring-web

4.1.0.RELEASE

&lhttp://t;groupId>com.fasterxml.jackson.core

jackson-databind

2.5.0

commons-bhttp://eanutils

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

/

QTgGuFJXRW

org.apache.tomcat.maven

tomcat7-maven-plugin

2.2

8088

/

UTF-8

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

xiaoma

zjl

war

0.0.1-SNAPSHOT

zjl Maven Webapp

http://maven.QTgGuFJXRWapache.org

junit

junit

3.8.1

test

org.springframework

spring-webmvc

4.1.0.RELEASE

org.springframework

spring-web

4.1.0.RELEASE

&lhttp://t;groupId>com.fasterxml.jackson.core

jackson-databind

2.5.0

commons-bhttp://eanutils

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

/

QTgGuFJXRW

org.apache.tomcat.maven

tomcat7-maven-plugin

2.2

8088

/

UTF-8

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

4.spring-servlet.xml文件:

xmlns:context="http://springframework.org/schema/context"

xmlns:p="http://springframework.org/schema/p"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">

xmlns:context="http://springframework.org/schema/context"

xmlns:p="http://springframework.org/schema/p"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">

5.web.xml文件:

xmlns:xsi="http://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

xmlns:xsi="http://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错误

记录一下


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:解决微信内置浏览器返回上一页强制刷新问题方法
下一篇:java解析excel文件的方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~