SSM框架自学(八)——SpringMVC如何返回JSON格式的数据
【SpringMVC】JSON以及Fastjson和Jackson的使用
springmvc 中 注解驱动:<mvc:annotation-driven>的作用和使用
解决 No converter found for return value of type: class java.util.ArrayList的问题
关于No converter found for return value of type: class java.util.ArrayList
使用jstl标签
导入jstl依赖(pom.xml)
pom.xml
<?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>springmvc2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springmvc2 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!--springmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
</dependencies>
<build>
<finalName>springmvc2</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
users.jsp页面中引入核心标签库
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
users.jsp中使用<c:foreach>标签
users.jsp
<%--
Created by IntelliJ IDEA.
User: U100926
Date: 2022/09/01
Time: 17:04
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>JSTL</title>
</head>
<body>
<table cellpadding=0 cellspacing=0 border=1>
<thead>
<tr>
<th>ID</th>
<th>UserName</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<%--
静态页面
<tr>
<td>1001</td>
<td>zhangsan</td>
<td>张三</td>
<td>18</td>
</tr>
--%>
<%--动态页面,jstl--%>
<c:forEach items = "${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.userName}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
</tr>
</tbody>
</table>
</body>
</html>
TestController7.java
package com.deng.controller;
import com.deng.pojo.User;
import com.deng.pojo.UserVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.List;
//jstl的用法
@Controller
public class TestController7 {
@RequestMapping(value = "show23")
public String test23(Model model) {
List<User> userList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setAge(15+i);
user.setId(1+i);
user.setUserName("zhangsan"+i);
user.setName("zhang"+i);
userList.add(user);
}
model.addAttribute("userList", userList);
return "users";
}
}
User.java
package com.deng.pojo;
import java.util.Arrays;
public class User {
private Integer id;
private String userName;
private String name;
private Integer age;
private boolean isMarry;
private Double income;
private String[] interests;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public boolean isMarry() {
return isMarry;
}
public void setMarry(boolean isMarry) {
this.isMarry = isMarry;
}
public Double getIncome() {
return income;
}
public void setIncome(Double income) {
this.income = income;
}
public String[] getInterests() {
return interests;
}
public void setInterests(String[] interests) {
this.interests = interests;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", isMarry=" + isMarry
+ ", income=" + income + ", interests="
+ Arrays.toString(interests) + "]";
}
}
返回JSON格式的数据
在实际开发过程中,json是最为常见的一种方式,所以springmvc提供了一种更为简便的方式传递数据。
-
@ResponseBody 是把Controller方法返回值转化为JSON,称为序列化
-
@RequestBody 是把接收到的JSON数据转化为Pojo对象,称为反序列化
-
@RestController: 将当前处理器中所有方法的返回值都转换成json数据…
在Pom.xml中引入jackson依赖,参考父工程:
springmvc.xml中追加
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
springmvc.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: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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描组件,将加上@Controller注解的类作为SpringMVC的控制层 -->
<context:component-scan base-package="com.deng.controller"></context:component-scan>
<!--
配置视图解析器
作用:将prefix + 视图名称 + suffix 确定最终要跳转的页面
-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
@ResponseBody
- 当一个处理请求的方法标记为@ResponseBody时,表示该方法需要输出其他视图(json、xml),springmvc通过默认的json转化器转化输出
TestController8.java
package com.deng.controller;
import com.deng.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@ResponseBody//将该方法返回值转为json字符串
@Controller
public class TestController8 {
@RequestMapping(value = "show24")
public List<User> test24(Model model) {
List<User> userList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setAge(10+i);
user.setId(1+i);
user.setUserName("lisi"+i);
user.setName("li"+i);
userList.add(user);
}
return userList;
}
@RequestMapping(value = "show25")
public User test25(Integer id) {
User user = new User();
user.setName("张三");
user.setAge(18);
return user;
}
}