| SpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户 |
| SpringMVC视图的种类很多,默认有转发视图和重定向视图 |
| 当工程引入jstl的依赖,转发视图会自动转换为JstlView |
| 若使用的视图技术为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视 |
| 图解析器解析之后所得到的是ThymeleafView |
| # 前端控制器解析后,返回到success页面 |
| @RequestMapping("/testThymeleafView") |
| public String testThymeleafView(){ |
| return "success"; |
| } |
| |
| # http://localhost:8080/上下文路径/testThymeleafView |
| SpringMVC中默认的转发视图是InternalResourceView |
| SpringMVC中创建转发视图的情况: |
| 当控制器方法中所设置的视图名称以"forward:"为前缀时,创建InternalResourceView视图,此时的视 |
| 图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"forward:"去掉,剩余部 |
| 分作为最终路径通过转发的方式实现跳转 |
| @RequestMapping("/testForward") |
| public String testForward(){ |
| return "forward:/testThymeleafView"; |
| } |
| |
| # http: |
| SpringMVC中默认的重定向视图是RedirectView |
| 当控制器方法中所设置的视图名称以"redirect:"为前缀时,创建RedirectView视图,此时的视图名称不 |
| 会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"redirect:"去掉,剩余部分作为最 |
| 终路径通过重定向的方式实现跳转 |
| @RequestMapping("/testRedirect") |
| public String testRedirect(){ |
| return "redirect:/testThymeleafView"; |
| } |
| |
| # http: |
| 当控制器方法中,仅仅用来实现页面跳转,即只需要设置视图名称时,可以将处理器方法使用viewcontroller标签进行表示 |
- springmvc配置文件中配置如下

| # 表示访问/路径时就会经过前端控制器的解析,解析后找到指定路径下的index.html |
| # 同时需要配置mvc注解驱动,否则其他控制器中的请求映射将全部失效 |
| |
| <mvc:view-controller path="/" view-name="index"></mvc:view-controller> |
| |
| |
| <mvc:annotation-driven /> |
| # 控制层 |
| @Controller |
| public class JspController { |
| |
| @RequestMapping("/success") |
| public String success(){ |
| return "success"; |
| } |
| |
| } |
| |
| # springmvc配置文件 |
| <?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.atguigu.mvc.controller"></context:component-scan> |
| |
| <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> |
| <property name="prefix" value="/WEB-INF/templates/"></property> |
| <property name="suffix" value=".jsp"></property> |
| </bean> |
| |
| </beans> |
| |
| # success.jsp |
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| <html> |
| <head> |
| <title>Title</title> |
| </head> |
| <body> |
| 成功 |
| </body> |
| </html> |
| |
| # index.jsp |
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| <html> |
| <head> |
| <title>Title</title> |
| </head> |
| <body> |
| <h5>首页</h5> |
| <a href="${pageContext.request.contextPath}/success">success.jsp</a> |
| </body> |
| </html> |
| |
| # 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"> |
| |
| |
| <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> |
| <init-param> |
| <param-name>forceResponseEncoding</param-name> |
| <param-value>true</param-value> |
| </init-param> |
| </filter> |
| <filter-mapping> |
| <filter-name>CharacterEncodingFilter</filter-name> |
| <url-pattern>/*</url-pattern> |
| </filter-mapping> |
| |
| |
| <servlet> |
| <servlet-name>DispatcherServlet</servlet-name> |
| <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> |
| <init-param> |
| <param-name>contextConfigLocation</param-name> |
| <param-value>classpath:springMVC.xml</param-value> |
| </init-param> |
| <load-on-startup>1</load-on-startup> |
| </servlet> |
| <servlet-mapping> |
| <servlet-name>DispatcherServlet</servlet-name> |
| <url-pattern>/</url-pattern> |
| </servlet-mapping> |
| |
| </web-app> |
- 测试


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?