欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-17 21:22:09
...

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文件
SpringMVC-组件分析之视图解析器(prefix,suffix)

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