SpringMVC-组件分析之视图解析器(prefix,suffix)

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“;

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Thecoastlines

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值