文章目录
前言
这一章描述SpringMvc对于响应各种各样的处理以及其中遇到的问题。
一、准备
1. JSP
<%--
Created by IntelliJ IDEA.
User: 张军国001
Date: 2024/5/1
Time: 10:19
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>
<span>${requestScope.message}</span>
<span>${sessionScope.JSESSIONID}</span>
</body>
</html>
2. User
package org.example.springmvc.params.entity;
import com.alibaba.fastjson2.annotation.JSONField;
import com.alibaba.fastjson2.annotation.JSONType;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Create by zjg on 2024/4/27
*/
@Getter
@Setter
@ToString
@JSONType(ignores = "relatives")
public class User {
private int id;
private String name;
@JSONField(format = "yyyy-MM-dd")
private Date birth;
private String[]alias;//外号
private List<String>hobbies;//爱好
private Map<String,String>relatives;//家人们
private Role role;//角色
private List<User> friends;//朋友们
}
二、基础案例
1.设置属性
http://localhost:8080/response01
http://localhost:8080/response02
http://localhost:8080/response03
package org.example.springmvc.params.controller;
import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;
/**
* Create by zjg on 2024/5/1
*/
@Controller
public class ResponseController {
String path="params/response";
String key="message";
//servlet
@RequestMapping(value = "response01")
public String response01(HttpServletRequest request){
request.setAttribute(key,"HttpServletRequest");
return path;
}
}
2.model
http://localhost:8080/response02
//model
@RequestMapping(value = "response02")
public String response03(Model model){
model.addAttribute(key,"Model");
return path;
}
3. map
http://localhost:8080/response03
//map
@RequestMapping(value = "response03")
public String response02(Map map){
map.put(key,"Map");
return path;
}
4.modelMap
http://localhost:8080/response04
//modelMap
@RequestMapping(value = "response04")
public String response04(ModelMap modelMap){
modelMap.addAttribute(key,"ModelMap");
return path;
}
5. modelAndView
http://localhost:8080/response0
//modelAndView
@RequestMapping(value = "response05")
public ModelAndView response05(){
ModelAndView mv=new ModelAndView(path);
mv.addObject(key,"ModelAndView");
return mv;
}
三、扩展案例
1. header
http://localhost:8080/response06
//header
@RequestMapping(value = "response06")
public String response06(HttpServletResponse response){
response.addHeader(key,"header");
return path;
}
2. cookie
http://localhost:8080/response07
//cookie
@RequestMapping(value = "response07")
public String response07(HttpServletResponse response){
response.addCookie(new Cookie("JSESSIONID","cookie"));
return path;
}
3.session
http://localhost:8080/response08
//session
@RequestMapping(value = "response08")
public String response08(HttpServletRequest request){
request.getSession().setAttribute("JSESSIONID","session");
return path;
}
4. json
http://localhost:8080/response09
//json
@RequestMapping(value = "response09")
@ResponseBody
public JSONObject response09(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("code",200);
jsonObject.put("data","");
jsonObject.put("message","请求成功");
return jsonObject;
}
json这里遇到了一个406问题,我最开始的案例配置了mvc:annotation-driven/,后面引入json时又实现了WebMvcConfigurer这里导致冲突,把我json的配置又给覆盖掉了,这里我把mvc标签给去掉了。
5. object
//object
@RequestMapping(value = "response10")
@ResponseBody
public User response10(){
User user=new User();
user.setId(1);
user.setName("爱丽丝");
user.setBirth(new Date());
return user;
}