【springmvc (五)】使用ajax
  VdPUlrWoxIxR 2023年11月02日 41 0


ajax是js中与服务器进行交互的手段,是一种异步请求方式,使用面还是很广的。下面介绍在springmvc中如何使用ajax。

1.环境搭建:

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/config/spring.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>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

启动spring容器,并且指定了spring的配置文件和spring mvc的配置文件位置。都在web-inf下的config目录下。


spring。xml:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
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"
xmlns:p="http://www.springframework.org/schema/p">
<!-- 定义一个bean -->
</beans>

因为这里不需要在容器定义任意bean,所已空。


spring-mvc。xml:

<?xml version="1.0" encoding="UTF-8"?>  
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<mvc:annotation-driven />
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.t"/>
<!-- 视图解释类 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
<mvc:resources location="/WEB-INF/js/" mapping="/resource/js/**" />
</beans>

这里要注意


<mvc:annotation-driven />

必须写,会启动一些spring的类来处理json数据。

还有就是静态资源要配置,否则会被拦截。

扫描包的位置自行设定。

index。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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="/resource/js/jquery.min.js"></script>
<title>Insert title here</title>
</head>
<body>


<h3 ><p>success jquery</p></h3>


<script type="text/javascript">
$(document).ready(function(){
<span style="white-space:pre"> </span>alert("json");
<span style="white-space:pre"> </span>$.ajax( {
url : "/json",
dataType:"json",
data:{"id":"ididid"},
success : function(msg) {
console.log(msg);
for(item in msg){
<span style="white-space:pre"> </span> alert(msg[item]);
}
}
});
});
</script>
</body>
</html>

controller:

package com.t;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AjaxController {

@RequestMapping(value="/index.htm")
public String index(){
return "index";
}
@RequestMapping(value="/json")
public @ResponseBody List<Integer> json(String id){
System.out.println(id);
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
return list;
}
}

接受一个ajax参数同时返回一个list。


需要引入的jar:

spring的一堆包,commons-logging包,jackson的包,jstl的包。


最后来个项目目录截图:

【springmvc (五)】使用ajax_spring

项目源码:

​http://pan.baidu.com/s/1kUkxpiB​

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
VdPUlrWoxIxR