View5Controller:
package com.baizhiedu;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/view5")
public class View5Controller {
@RequestMapping("/view1")
public ModelAndView view1(){
ModelAndView modelAndView=new ModelAndView();
ModelMap modelMap=modelAndView.getModelMap();
modelMap.addAttribute("name","xiaohei");
//跳转
modelAndView.setViewName("result4");
return modelAndView;
}
}
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>
result4.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/6
Time: 17:21
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>${requestScope.name}</h1>
</body>
</html>