SpringMVC的默认组件都是在DispatcherServlet.properties配置文件中配置的;
spring-webmvc->org/springframewrok/web/servlet/DispatcherServlet.properties,该文件中配置了默认的视图解析器


打开org.springframework.web.servlet.view.UrlBasedViewResolver翻看该解析器源码,可以看到该解析器的默认设置

 	public static final String REDIRECT_URL_PREFIX = "redirect:";//重定向前缀
public static final String FORWARD_URL_PREFIX = "forward:"; //转发前缀
    private String prefix = "";	//视图名称前缀
private String suffix = ""; //视图名称后缀

我们来覆写视图解析器(spring-webmvc核心jar包一定得导入)
1.在webapp下面新建/jsp/success.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>success</h1>
</body>
</html>

2.在web.xml中配置web核心控制器及监听器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0"> <!-- 配置springMVC的核心配置控制器DispatcherServlet-->
<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:spring-mvc.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> <!-- 配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>

3.编写UserController类

@Controller
public class UserController { @RequestMapping("/report")
public String save(){
return "success";
}
}

4.编写spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
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 http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 扫描controller包下得类-->
<context:component-scan base-package="com.hao.controller"/> <!-- 配置内部资源视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

这一步配置视图解析器主要是为了步骤3中的return“success”;如果你不配置那步骤三就要写成return “/jsp/success.jsp";当你配置了之后,前端控制器会帮你拼装起来;
默认是转发,如果要写成重定向需要写为return ”redirect:/success“;

最新文章

  1. python笔记7:日期和时间
  2. Android setTag方法的key问题
  3. ios基础篇(十六)——UIWebView的基本使用
  4. Object-C 基础笔记3---属性
  5. php面向对象(一) 初窥
  6. nslookup返回信息说明
  7. OC:基础学习
  8. [zz] pgpool-II load balancing from FAQ
  9. mvc分层的原理
  10. 借助树的概率dp(期望)+数学-好题-hdu-4035-Maze
  11. (转)js jquery.qrcode生成二维码 带logo 支持中文
  12. 201521123080《Java程序设计》第2周学习总结
  13. linux下ftp命令的安装与使用
  14. ●POJ 1741 Tree
  15. thinkphp封装方法添加跨域请求
  16. unzip解压war包并覆盖
  17. Find out where to contain the smartforms
  18. 【手记】解决excel无法设置单元格颜色且界面怪异+桌面图标文字老有色块等问题
  19. 使用Let&#39;s Encrypted HPPTS你的网站
  20. storm杂谈之Why use netty as transport instead of zeromq

热门文章

  1. linux 环境变量设置(临时 + 永久)
  2. 2.3 C++STL vector容器详解
  3. VBS文件无限循环解决办法
  4. info sharp Are you trying to install as a root or sudo user? Try again with the --unsafe-perm flag
  5. 在西电使用校内Linux 开源软件镜像
  6. Windows10 1809版本Windows自动更新服务无法禁用问题解决方案
  7. [SPDK/NVMe存储技术分析]015 - 理解内存注册(Memory Registration)
  8. Mac安装和配置Maven 及其第二次启动报错问题解决
  9. 深入浅出SpringBoot
  10. jvm-learning-双亲委派机制