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

Springboot配置suffix指定mvc视图的后缀方法

程序员文章站 2022-03-11 12:18:49
springboot配置suffix指定mvc视图后缀如下所示:spring:#配置mvc视图后缀 mvc: view: suffix: ".html"配置指定后缀之后访问welco...

springboot配置suffix指定mvc视图后缀

如下所示:

spring:
#配置mvc视图后缀
  mvc:
    view:
      suffix: ".html"

配置指定后缀之后

访问welcome.html页面时只需要写“welcome”即可。

@controller
public class democontroller {
    @getmapping("/a")
    public string demo(){
        return "welcome";
    }

运行结果:

Springboot配置suffix指定mvc视图的后缀方法

springboot配置mvc-controller请求的后缀名

1.启动类添加配置

package com.ias.oil.client.schedule; 
import com.ias.oil.model.oilservice;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.boot.web.servlet.servletregistrationbean;
import org.springframework.cloud.client.discovery.enablediscoveryclient;
import org.springframework.cloud.netflix.feign.enablefeignclients;
import org.springframework.context.annotation.bean;
import org.springframework.web.servlet.dispatcherservlet;
 
@enablediscoveryclient
@enablefeignclients({oilservice.package_for_service_schedule})
@springbootapplication
public class oilscheduleclientapplication {
    public static void main(string[] args) {
        springapplication.run(oilscheduleclientapplication.class, args);
    }
 
    /**
     * 设置匹配.action后缀的请求
     * @param dispatcherservlet
     * @return
     */
    @bean
    public servletregistrationbean servletregistrationbean(dispatcherservlet dispatcherservlet) {
        servletregistrationbean bean = new servletregistrationbean(dispatcherservlet);
        bean.addurlmappings("*.action");
        return bean;
    }
}

需要添加上面代码片段的此部分

/**
     * 设置匹配.action后缀的请求
     * @param dispatcherservlet
     * @return
     */
    @bean
    public servletregistrationbean servletregistrationbean(dispatcherservlet dispatcherservlet) {
        servletregistrationbean bean = new servletregistrationbean(dispatcherservlet);
        bean.addurlmappings("*.action");
        return bean;
    }

2.配置文件中添加配置

spring:
  mvc:
      ##设置匹配.action后缀的请求的配置
    pathmatch:
      use-suffix-pattern: false
      use-registered-suffix-pattern: true
      contentnegotiation:
        favor-path-extension: false

~~~~~完活

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。