web.xml:初始化配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 声明,注册springmvc的核心对象DispatcherServlet-->
<servlet>
<servlet-name>myweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 自定义springmvc读取的配置文件的位置-->
<init-param>
<!--springmvc的配置文件属性-->
<param-name>contextConfigLocation</param-name>
<!-- 指定自定义文件的位置-->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 在tomcat启动后,创建Servlet对象
load-on-startup:表示tomcat启动后创建对象的顺序,它的值是正数,数值越小 tomacat创建对象越早
-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 所有的*..do请求交给myweb中央处理器处理-->
<servlet-mapping>
<servlet-name>myweb</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--注册声明过滤器,解决post请求乱码问题-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--设置项目中使用的编码-->
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<!--强制请求对象(HttpServletRequest)使用encoding编码的值-->
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<!--强制应答对象(HttpServletResponse)使用encoding编码的值-->
<init-param>
<param-name>forceReponsetEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<!--
/*:表示强制所有的请求先通过过滤器处理
-->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
springmvc.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"
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">
<!-- 用了注解需要注解扫描器:声明组件扫描器:包扫描-->
<context:component-scan base-package="com.bjpowernode.controller"/>
<!-- 声明springmvc框架中的视图解析器,帮助开发人员设置视图文件的路径-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀:视图文件的路径-->
<property name="prefix" value="/WEB-INF/view/"/>
<!-- 后缀:视图文件的扩展名-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
控制器MyController:
package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
/**
*处理器方法返回ModelAndView,实现转发forward
* 语法:setViewName("forward:视图文件完整路径")
* forward特点:不和视图解析器一同使用,就当项目中没有视图解析器
*/
@RequestMapping(value = "/doForward.do")
public ModelAndView doForword(String name,Integer age){
ModelAndView mv=new ModelAndView();
mv.addObject("myname",name);
mv.addObject("myage",age);
//mv.setViewName("show");
//显示转发
//mv.setViewName("forward:/WEB-INF/view/show.jsp");
/*如果hello.jsp页面没有在视图解析器下的WEB-INF/view目录下,需要使用forword进行转发
不在受视图解析器的限制了,可以转发到其他任意的路径了
*/
mv.setViewName("forward:/hello1.jsp");
return mv;
}
/**
*处理器方法返回ModelAndView,实现重定向redirect
* 语法:setViewName("redirect:视图文件完整路径")
* redirect特点:不和视图解析器一同使用,就当项目中没有视图解析器
*
* 框架对重定向的操作:
* 1.框架会把Model中的简单数据类型,转为String使用,作为hello.jsp的get[请求参数]使用。
* 目的是在doRedirect.do和hello.jsp两次请求之间可以传递数据
*
* 2.在目标hello.jsp页面可以使用参数集合对象${param}获取请求参数值
* 例如:${param.myname}
*
* 3.重定向不能访问/WEN-INF下的资源
*/
//这里发起了两个request请求,作用域不同,所以在hello.jsp获取不到,需要更改为获取参数
@RequestMapping(value = "/doRedirect.do")
public ModelAndView doRedirect(String name,Integer age){
ModelAndView mv=new ModelAndView();
//数据放入到request的作用域
mv.addObject("myname",name);
mv.addObject("myage",age);
//重定向
mv.setViewName("redirect:/hello.jsp");
//重定向不能访问/WEB-INF的资源
//mv.setViewName("redirect:/WEB-INF/view/show.jsp");
return mv;
}
}
show.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/7
Time: 19:01
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>
<h3>/WEB-INF/view/show.jsp,forword显示转发</h3>
<h3>msg数据:${myname}</h3>
<h3>fun数据:${myage}</h3>
</body>
</html>
hello1.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/7
Time: 19:01
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>
<h3>hello.jsp,获取forword转发的数据</h3>
<h3>myname数据:${myname}</h3>
<h3>myage数据:${myage}</h3>
</body>
</html>
hello.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/7
Time: 19:01
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>
<h3>hello.jsp,获取redirect转发的数据</h3>
<h3>myname数据:${param.myname}</h3>
<h3>myage数据:${param.myage}</h3>
<h3>相当于取参数数据:<%=request.getParameter("myname")%></h3>
</body>
</html>
index.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/7
Time: 17:37
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>
<p>当出来方法返回ModelAndView实现forward</p>
<form action="doForward.do" method="post">
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="提交请求">
</form>
<br>
<p>当出来方法返回ModelAndView实现redirect</p>
<form action="doRedirect.do" method="post">
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="提交请求">
</form>
</body>
</html>
forward属于服务器端去请求资源,服务器端直接访问目标地址,并对目标地址的响应内容进行读取,再把读取到的内容发给浏览器,因此客户端的地址不变
Redirect告诉客户端,使用浏览器知道去请求哪一个地址,相当于客户端重新请求一遍,所以地址栏会变
forward的效率较高,因为跳转仅发生在服务器端,redirect效率相对较低,因为相当于又进行了一次请求