dispatcher.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" xmlns:tx="http://www.springframework.org/schema/tx"
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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--设置注解扫描的路径 -->
<context:component-scan base-package="com.baizhiedu"/>
<!--引入springMVC的核心功能-->
<mvc:annotation-driven />
<!--视图解析器配置: 控制器方法返回的结果的前缀和后缀-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
View3Controller:
package com.baizhiedu;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@RequestMapping("/view3")
//@SessionAttributes(value = "name") //通过注解,让springMVC知道在Model存放的数据不是request作用域,而是Session作用域
@SessionAttributes(value = {"name","sex"})
public class View3Controller {
@RequestMapping("/view1")
public String view1(Model model){
model.addAttribute("sex","female");
model.addAttribute("age",10); //没有往Session中添加,默认存在Request作用域里面
model.addAttribute("name","xioajr");
return "result2";
}
}
result2.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/6
Time: 10:13
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
<h1>session attribute is ${sessionScope.name}</h1>
<h1>request is attribute is ${requestScope.age}</h1>
</body>
</html>
result2.jsp:书写代码看看能否获取?
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/6
Time: 10:13
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
<h1>session attribute is ${sessionScope.name}</h1>
<%-- Model往Session中存储的数据,也会在Request中存储--%>
<h1>request attribute is ${requestScope.name}</h1>
<h1>request is attribute is ${requestScope.age}</h1>
</body>
</html>
View3Controller:
package com.baizhiedu;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
@Controller
@RequestMapping("/view3")
//@SessionAttributes(value = "name") //通过注解,让springMVC知道在Model存放的数据不是request作用域,而是Session作用域
@SessionAttributes(value = {"name","sex"})
public class View3Controller {
@RequestMapping("/view1")
public String view1(Model model){
model.addAttribute("sex","female");
model.addAttribute("age",10); //没有往Session中添加,默认存在Request作用域里面
model.addAttribute("name","xioajr");
//return "result2";
return "forward:/view3/view2";
}
@RequestMapping("/view2")
public String view2(SessionStatus sessionStatus){
if (!sessionStatus.isComplete()){
sessionStatus.setComplete();//清空Session
}
return "result2";
}
}