处理器(action)方法的返回值
1.String:客户端资源的地址,自动拼接前后缀,还可以屏蔽自动拼接字符串,可以返回指定的路径
2.Object:返回json格式的对象,自动将对象或集合转为json。使用的jackson工具进行转换,必须添加jackson依赖,一般用于ajax请求
3.void:无返回值,一般用于ajax请求
4.基本数据类型:用于ajax请求
5.ModelAndView:返回数据和视图对象,现在用的很少
例:完成ajax请求访问服务器,返回学生集合
1.添加jackson依赖
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SpringMVC_004</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringMVC_004 Maven Webapp</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- 添加jackson依赖,用于ajax请求-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.在webapp目录新建js目录,添加jQuery函数库
jQuery函数库在网上下载后自己复制进js目录即可
3.在index.jsp页面上导入函数库
<%--
Created by IntelliJ IDEA.
User: HP
Date: 2022-06-27
Time: 10:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<%-- 导入jQuery函数库--%>
<script src="js/jquery-3.6.0.js"></script>
</head>
<body>
<br>
<br>
<br>
<a href="javascript:showStu()">访问服务器返回学生集合</a><br>
<div id="mydiv">等待服务器返回数据</div>
<script type="text/javascript">
function showStu(){
// 使用jQuery封装的ajax()方法发送请求
$.ajax({
url:"${pageContext.request.contextPath}/list.action",
type:"get",
dataType:"json",
success:function (stuList){
var s ="";
$.each(stuList,function (i,stu){
s+=stu.name+"----"+stu.age+"<br>";
});
// 回显数据
$("#mydiv").html(s);
}
});
}
</script>
</body>
</html>
4.在action上添加注解@ResponseBody,用来处理ajax请求
package com.ys.controller;
import com.ys.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@Controller
public class AjaxAction {
@RequestMapping("/list")
@ResponseBody//ajax支持
public List<Student> list(){
List<Student> list =new ArrayList<>();
Student student1=new Student("张三",22);
Student student2=new Student("李四",24);
Student student3=new Student("王五",26);
list.add(student1);