【第9章】spring-mvc响应结果处理

本文详细介绍了在SpringMVC中如何处理HTTP响应,包括设置属性、使用Model、Map、ModelMap、ModelAndView,以及扩展案例如设置header、cookie、session和JSON数据的返回。还提及了JSONType注解和JSON对象处理的注意事项。
摘要由CSDN通过智能技术生成


前言

这一章描述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;
}

总结

回到顶部

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值