SpringMvc常用操作
V哥官网:http://www.vgxit.com
本文对应视频教程:http://www.vgxit.com/course/24
1,概述
介绍一些SSM的常用的API,包括页面重定向,Session,Cookie,Header的操作。
2,URL重定向
有的时候,我们处理完了一个逻辑之后,我们需要跳转到另外一个页面,这个时候,我们就可以使用URL重定向。
@GetMapping("/test-redi1")
public ModelAndView testRedi1() {
log.info("test-redi1");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("redirect:./all");
return modelAndView;
}
@GetMapping("/test-redi2")
public String testRedi2() {
log.info("test-redi2");
return "redirect:./all";
}
你上面介绍的,都是跳转到本项目的另外一个页面,但是,如果有的时候,我是要重定向到百度,淘宝。
@GetMapping("/test-redi2")
public String testRedi2() {
log.info("test-redi2");
return "redirect:http://www.taobao.com";
}
3,SpringMvc操作Session
我们在SpringMvc中要操作Session,当然我们可以使用HttpSession通过参数传入方法的方式。但是这样来做其实又使用到了ServletApi。那么,SpringMvc有没有帮我们提供操作Session的方法呢?但是V哥觉得SpringMvc提供的操作Session的方法,不是那么太好用。不过我们也来看看。
1,首先我们创建对应的Controller:
package com.vgixt.learn.springmvc.ssm.controller;
import com.vgixt.learn.springmvc.ssm.po.User;
import com.vgixt.learn.springmvc.ssm.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/session")
/**
* 下面的注解只能做用在类上面,表示完成对应的逻辑之后,我们可以添加ModelAndView中的数据到Session
* names表示哪些名字的数据可以保存到session
* types表示哪些类型的数据可以保存到session
*/
@SessionAttributes(names = {"id"}, types = {User.class})
public class SessionController {
@Autowired
private IUserService userService;
@GetMapping("/set-session/{id}")
public ModelAndView setSession(@PathVariable("id") int id) {
User user = userService.getById(id);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("id", id);
modelAndView.addObject("name", "李一桐");
modelAndView.addObject("user", user);
modelAndView.setViewName("session/set-session");
return modelAndView;
}
}
2,在对应的jsp里面读取session数据:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2021/8/17
Time: 0:00
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>
session中的值:
<p>用户的name: ${sessionScope.user.name}</p>
<p>session的name:${sessionScope.name}</p>
<p>session的id:${sessionScope.id}</p>
</body>
</html>
4,SpringMvc读取Session
1,创建对应的jsp:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2021/8/17
Time: 0:13
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>
<%
session.setAttribute("name", "李一桐");
response.sendRedirect("../../session/read-session");
%>
</body>
</html>
2,创建对应的Controller:
@GetMapping(value = "/read-session")
public @ResponseBody String readSession(@SessionAttribute("name") String name) {
return "name in session is " + name;
}
5,解决@Response返回String中文乱码的问题:
直接在mapping里面加上对应的produces属性就好了
@GetMapping(value = "/read-session", produces = {"text/plain;charset=utf-8", "text/html;charset=utf-8", "application/json;charset=utf-8"})
public @ResponseBody String readSession(@SessionAttribute("name") String name) {
return "name in session is " + name;
}
6,读取Header和Cookie
@GetMapping("/header-and-cookie")
public @ResponseBody String headAndCookie(
@RequestHeader(value = "test", required = false, defaultValue = "test") String test,
@CookieValue(value = "JESSIONID", required = true, defaultValue = "testjessionid") String jessionId//如果defaultValue不存在,如果又没传,那么报错
) {
return "header value is " + test + ",cookie value is " + jessionId;
}