Spring Boot add HTML and JavaScript

后端 未结 7 832
天命终不由人
天命终不由人 2020-12-24 08:50

I\'m trying to run Spring boot app with some HTML5 and JavaScript and I have a problem.

I have project structure like this:

My Spring MVC Controller

7条回答
  •  失恋的感觉
    2020-12-24 09:24

    Set the document root directory which will be used by the web context to serve static files using ConfigurableEmbeddedServletContainer.setDocumentRoot(File documentRoot).

    Working example:

    package com.example.config;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
    import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
    import org.springframework.boot.web.servlet.ServletContextInitializer;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.env.Environment;
    import org.springframework.util.StringUtils;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import java.io.File;
    import java.nio.file.Paths;
    
    @Configuration
    public class WebConfigurer implements ServletContextInitializer, EmbeddedServletContainerCustomizer {
        private final Logger log = LoggerFactory.getLogger(WebConfigurer.class);
    
        private final Environment env;
        private static final String STATIC_ASSETS_FOLDER_PARAM = "static-assets-folder";
        private final String staticAssetsFolderPath;
    
        public WebConfigurer(Environment env, @Value("${" + STATIC_ASSETS_FOLDER_PARAM + ":}") String staticAssetsFolderPath) {
            this.env = env;
            this.staticAssetsFolderPath = staticAssetsFolderPath;
        }
    
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            if (env.getActiveProfiles().length > 0) {
                log.info("Web application configuration, profiles: {}", (Object[]) env.getActiveProfiles());
            }
            log.info(STATIC_ASSETS_FOLDER_PARAM + ": '{}'", staticAssetsFolderPath);
        }
    
        private void customizeDocumentRoot(ConfigurableEmbeddedServletContainer container) {
            if (!StringUtils.isEmpty(staticAssetsFolderPath)) {
                File docRoot;
                if (staticAssetsFolderPath.startsWith(File.separator)) {
                    docRoot = new File(staticAssetsFolderPath);
                } else {
                    final String workPath = Paths.get(".").toUri().normalize().getPath();
                    docRoot = new File(workPath + staticAssetsFolderPath);
                }
                if (docRoot.exists() && docRoot.isDirectory()) {
                    log.info("Custom location is used for static assets, document root folder: {}",
                            docRoot.getAbsolutePath());
                    container.setDocumentRoot(docRoot);
                } else {
                    log.warn("Custom document root folder {} doesn't exist, custom location for static assets was not used.",
                            docRoot.getAbsolutePath());
                }
            }
        }
    
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            customizeDocumentRoot(container);
        }
    
    }
    

    Now you can customize your app with command line or profiles (src/main/resources/application-myprofile.yml: static-assets-folder: myfolder):

    > java -jar demo-0.0.1-SNAPSHOT.jar --static-assets-folder="myfolder"
    > java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=myprofile
    

提交回复
热议问题