在apache tomcat上部署war文件时不加载CSS和JS文件

时间:2016-12-16 18:45:28

标签: spring spring-mvc spring-boot

使用spring-boot-starter-web-mvc进行初始开发。当我在服务器上部署war文件时,它没有加载css和js文件,但是当我使用spring boot运行相同的代码时,它是工作正常。下面是加载js和css文件的目录结构和配置。任何人都可以让我知道如何解决问题?仅使用基于java的项目配置。在部署项目之前,我在doc中引用了8.1.1,并使用了与下面相同的pom文件配置。

Directory structure

配置文件 -

public class ViewWebAppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // configuration for assets/ static files
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Bean
    public ResourceBundleMessageSource messageSource() {
        ResourceBundleMessageSource rb = new ResourceBundleMessageSource();
        rb.setBasenames(new String[] { "validation" });
        return rb;
    }

}

pom.xml文件

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <log4j.version>2.7</log4j.version>
    </properties>

    <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
        </dependency>
         <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- Dependency for rendering jsp pages  -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>tomcat</groupId>
            <artifactId>jasper-compiler</artifactId>
            <version>5.5.23</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>tomcat</groupId>
            <artifactId>jasper-runtime</artifactId>
            <version>5.5.23</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>tomcat</groupId>
            <artifactId>jasper-compiler-jdt</artifactId>
            <version>5.5.23</version>
            <scope>provided</scope>
        </dependency>
        <!-- Dependency for rendering jsp pages  -->

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
    </dependencies>

使用spring-tags加载js和css,如下所示 -

<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<spring:url value="/resources/assets/js/custom.min.js" var="customJS" />
<script src="${customJS}"></script>

<link href="${customFieldAgent}" rel="stylesheet"/>
<spring:url value="/resources/assets/css/customfieldAgent.css" var="customFieldAgent" />

以下是服务器日志 -

  

17:03:10.014 [http-nio-8080-exec-7] DEBUG   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping    - 没找到[/ $ {starrrJS}]的处理程序方法17:03:10.014 [http-nio-8080-exec-7] WARN   org.springframework.web.servlet.PageNotFound - 找不到映射   在DispatcherServlet中带有URI [/ ui / $%7BstarrrJS%7D]的HTTP请求   名称'dispatcher'17:03:10.014 [http-nio-8080-exec-7] DEBUG   org.springframework.web.servlet.DispatcherServlet - 成功   完成请求17:03:10.018 [http-nio-8080-exec-4] DEBUG   org.springframework.web.servlet.DispatcherServlet - DispatcherServlet   名称'dispatcher'处理GET请求   [/ ui / $%7BcustomJS%7D] 17:03:10.018 [http-nio-8080-exec-4] DEBUG   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping    - 查找路径的处理程序方法/ $ {customJS} 17:03:10.019 [http-nio-8080-exec-4] DEBUG   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping    - 没找到[/ $ {customJS}]的处理程序方法17:03:10.019 [http-nio-8080-exec-4] WARN   org.springframework.web.servlet.PageNotFound - 找不到映射   在DispatcherServlet中带有URI [/ ui / $%7BcustomJS%7D]的HTTP请求   名称'dispatcher'17:03:10.019 [http-nio-8080-exec-4] DEBUG   org.springframework.web.servlet.DispatcherServlet - 成功   已完成的请求

3 个答案:

答案 0 :(得分:0)

尝试这种方式它对我有用。

项目结构:

src/main/webapp/static/[css,js,images]
src/main/webapp/WEB-INF/views/[for all .jsp pages]

<强>配置

@Configuration
@ComponentScan(basePackages = "parent package name where all packages reside")
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        registry.viewResolver(viewResolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // when static resources are inside resources folder under WEB-INF
        // registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");

        // when static resources are inside static folder under webapp
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    }
}

DispatcherServlet 知道如何打包......

import javax.servlet.Filter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { ApplicationConfiguration.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }     
}

现在在视图上调用这些......

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <link href="<c:url value='/static/css/application.css' />" rel="stylesheet"></link>
    </head>

在pom.xml中添加

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

希望这会对你有所帮助。

答案 1 :(得分:0)

在我的Spring Boot项目中,我尝试将WAR文件部署到TOMCAT中,但是它无法识别资源路径,它会抛出 404文件未找到错误,但是当我作为SpringBootApplication运行时,它工作正常,只有在作为WAR文件部署到TOMCAT时才会发出。所以最后我发现了这个问题,现在它完全适用于我的项目结构enter image description here

将这些行添加到 application.properties 文件

spring.resources.add-mappings=true
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/

我来自https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

它完美地运作......

答案 2 :(得分:0)

为此js,images,css或战争文件之外的图像或任何文件都可以显示,也可以通过使用 这是以下两种情况的共同点

spring.resources.add-mappings = true

对于要在spring boot war文件或spring的任何文件中进行访问或显示,您必须将它们放入application.properties文件或任何属性文件中

spring.resources.static-locations = classpath:/ META-INF / resources /,classpath:/ resources /,classpath:/ static /

对于外部访问文件或显示图像的外部文件,您可以输入

spring.resources.static-locations = file:/// C:/ xxxxxxxxx / xxxxxxxxx

AS说Vignesh R很好用吧