springboot整合vuejs(二)

目录

首先 从其他系统过来一个请求,我验证一下token,
通过了那就跳到首页,不然就滚回去,

结构

springboot整合vuejs(二)_第1张图片
springboot 我使用的是war包发布,放在tomcat中,所以需要 一个springbootStarApplication的类,

public class SpringBootStartApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(Application.class);
    }
}

使用了thymeleaf

由于thymeleaf语法过于严格,所以我需要将这个因素排除掉
导入 nekohtml + webjars

<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        
        <dependency>
            <groupId>net.sourceforge.nekohtmlgroupId>
            <artifactId>nekohtmlartifactId>
            <version>1.9.22version>
        dependency>
        <dependency>
            <groupId>org.webjarsgroupId>
            <artifactId>vueartifactId>
            <version>2.1.3version>
        dependency>

然后在配置文件中配置一下松散的html格式,和静态资源的路径

spring.thymeleaf.mode =LEGACYHTML5
spring.thymeleaf.cache=false
spring.thymeleaf.content-type=text/html
spring.mvc.static-path-pattern=/**
server.context-path=/
server.tomcat.uri-encoding=utf-8
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/

vue的路径配置

由于是前端 使用 npm run build 之后给我的文件,恰好 npm 会将文件 目录转化成
springboot整合vuejs(二)_第2张图片
恰好可以在 resource目录下 将 index.html放入static中,
他的html文件中的代码是
这里写图片描述
需要服务器地址和静态资源前缀,

springboot的MVC配置

需要重写 WebMvcConfigurerAdapter

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter { 

    // 当前激活的配置文件
    @Value("${spring.profiles.active}")
    private String env;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }

你可能感兴趣的:(springBoot基础)