响应数据和结果视图
1. 返回值分类
这里的返回值指的是Controller中的方法的返回值。
1.1 String
该字符串为逻辑视图名,通过视图解析器解析为物理视图地址。
注:底层调用的是ModelAndView
1.1.1 实际开发中的应用
实体类
index.jsp
<%--
Created by IntelliJ IDEA.
User: 商务小本本
Date: 2022/3/3
Time: 23:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="test/testString">返回值为String</a>
</body>
</html>
Controller
package cn.itcast.controller;
import cn.itcast.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 商务小本本
*/
@RequestMapping("/test")
@Controller
public class ReturnValue {
@RequestMapping("/testString")
public String testString(Model model){
System.out.println("testString执行了。。。。");
//模拟从数据库中查出User对象
User user = new User();
user.setAge(30);
user.setPassword("dsfsgjfdshj");
user.setUsername("hdskfadsgfdsghk");
model.addAttribute("user", user);
return "success";
}
}
success.jsp
<%--
Created by IntelliJ IDEA.
User: 商务小本本
Date: 2022/3/3
Time: 23:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>入门成功!</h3>
${user.toString()}
</body>
</html>
1.2 void
当返回值为void时,会有一个默认的跳转页面
这个路径的构成
注:返回值为void,可以跳转页面也可以直接作出响应
1.2.1 手动跳转页面
请求转发
重定向
注:重定向是不能直接访问WEB-INF目录的,此外,重定向需要在路径上添加项目名称
1.2.2 直接响应
如果需要输出中文,进行如下处理
1.3 ModelAndView对象
需求:前端发送请求到控制器,控制器模拟查询数据库,将查询出的对象存入request域中,跳转页面,显示查询出的对象。
package cn.itcast.controller;
import cn.itcast.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 商务小本本
*/
@Controller
@RequestMapping("/test")
public class HelloController {
@RequestMapping("/testVoid")
public ModelAndView testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception{
ModelAndView modelAndView = new ModelAndView();
//模拟从数据库中查询出user对象
User user = new User();
user.setName("jasdjkasgdgjasdhkj");
//把user对象存储到modelAndView对象中,底层会把user对象存入request域对象中
modelAndView.addObject("user", user);
//跳转到的页面,利用视图解析器进行跳转
modelAndView.setViewName("success");
return modelAndView;
}
}
2. 使用forward、redirect关键字实现请求转发和重定向(SpringMVC框架提供的方式)
注:不支持视图解析器,路径写全
3. @ResponseBody响应json数据
1. 应用场景
前端发送Ajax异步请求,请求数据,后端通过json串的方式返回,也叫接口开发
2. 过滤静态资源
web.xml中对DispatcherServlet的配置会拦截所有资源,当页面中访问内部的js等静态资源时就会出现404
比如下面的示例:
jsp
<%--
Created by IntelliJ IDEA.
User: 商务小本本
Date: 2022/3/3
Time: 23:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="js/jquery-3.6.0.min.js"></script>
<script>
$(function () {
$("#btn").click(function () {
alert("hello");
});
});
</script>
</head>
<body>
<button id="btn">发送Ajax请求</button>
</body>
</html>
静态资源被拦截了
springmvc.xml中添加
<mvc:resources mapping="/js/**" location="/js/"/>
3. 发送Ajax请求
jsp
<%--
Created by IntelliJ IDEA.
User: 商务小本本
Date: 2022/3/3
Time: 23:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="js/jquery-3.6.0.min.js"></script>
<script>
$(function () {
$("#btn").click(function () {
$.ajax({
url:"test/testVoid",
contentType:"application/json;charset=UTF-8",
data:'{"name":"lisi", "gender":"nan", "money":"123", "date":"2022/11/11"}',
dataType:"json",
type:"post",
success:function (data) {
}
});
});
});
</script>
</head>
<body>
<button id="btn">发送Ajax请求</button>
</body>
</html>
关于ajax的几个属性
属性 | 功能 |
---|---|
url:"test/testVoid" | 请求后端的地址 |
contentType:"application/json;charset=UTF-8" | 发送数据至服务器时内容编码类型,此处为json的MIME类型和编码 |
data:'{"name":"lisi", "gender":"nan", "money":"123", "date":"2022/11/11"}' | 请求体携带的数据 |
dataType:"json" | 预期服务器返回的数据类型 |
type:"post" | 请求类型 |
success:function (data) {} | 回调函数 |
Controller
package cn.itcast.controller;
import cn.itcast.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 商务小本本
*/
@Controller
@RequestMapping("/test")
public class HelloController {
@RequestMapping("/testVoid")
public void testVoid(@RequestBody String body) throws Exception{
System.out.println(body);
}
}
4. 响应json格式数据
Controller中接收到前端传来的json串后,需要转成JavaBean对象,处理之后再转成json串传回客户端
这之间用到解析json的几个jar包,maven坐标如下:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
通过SpringMVC提供的@RequestBody注解及上述几个jar包,可实现Ajax异步请求的请求体json数据转为JavaBean对象。
通过SpringMVC提供的@ResponseBody注解及上述几个jar包,可实现方法返回的JavaBean对象转为json串,供Ajax的回调函数(success)解析使用。
jsp
<%--
Created by IntelliJ IDEA.
User: 商务小本本
Date: 2022/3/3
Time: 23:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="js/jquery-3.6.0.min.js"></script>
<script>
$(function () {
$("#btn").click(function () {
$.ajax({
url:"test/testVoid",
contentType:"application/json;charset=UTF-8",
data:'{"name":"lisi", "gender":"nan", "money":"123"}',
dataType:"json",
type:"post",
success:function (data) {
alert(data.name);
alert(data.gender);
alert(data.money);
}
});
});
});
</script>
</head>
<body>
<button id="btn">发送Ajax请求</button>
</body>
</html>
Controller
package cn.itcast.controller;
import cn.itcast.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 商务小本本
*/
@Controller
@RequestMapping("/test")
public class HelloController {
@RequestMapping("/testVoid")
public @ResponseBody User testVoid(@RequestBody User user) throws Exception{
System.out.println(user);
return user;
}
}
标签:SpringMVC,Controller,springframework,响应,json,视图,import,org,user
From: https://www.cnblogs.com/wzzzj/p/18038909