Spring Boot 2.1.0 War文件无法配置'context-path',否则jsp中的'include'无法正常工作

时间:2019-01-06 06:48:08

标签: spring-boot tomcat servlets war

我正在使用 Spring Boot 2.1.0 开发Web应用程序。我使用maven将代码打包到 WAR 文件中。

我在application.properties文件中配置了server.servlet.context-path=/marpt,以使应用程序无法直接通过ip + port访问。
然后,我使用java -jar marpt.war命令运行WAR,并且启动成功。
然后我在Chrome中打开正在运行的Web应用程序,抛出了一些问题:
1.正确显示索引登录页面;
2.登录后,jsp include,两种{inclue命令} <jsp:include page="${ctx}/home/sidebar" /><%@include file="../../pages/share/partial/header.jsp" %>以及任何其他引用均无效。

我使用JavaScript来提醒我在另一个jsp文件中定义的${ctx},并通过<%@ include file="../../pages/common/taglibs.jsp" %>将其包含在首页顶部,这很奇怪,可以包含taglibs.jsp。在其中设置<c:set var="ctx" value="${pageContext.request.contextPath}" />。在首页中,我这样做是:alert('${pageContext.request.contextPath}'),结果为 / marpt
我很困惑哪里错了。

我尝试打包jar文件,它也可以正确启动,但是页面根本无法在浏览器中打开。

以下是主要配置文件:
POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>ma-rpt</artifactId>
    <version>1.5.0</version>
    <packaging>war</packaging>

    <name>ma-rpt</name>
    <description>Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.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>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.6.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.4.0</version>
        </dependency>
        <!-- Shiro ehCache -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <!-- jstl支持 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <finalName>marpt</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>application.properties</include>
                    <include>properties/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/webapp</directory>
            </resource>
        </resources>
    </build>

</project>

application.properties

# Spring Public
server.port                         =8096
server.servlet.context-path         =/marpt
# Cache
spring.cache.type                   =ehcache
# Db
spring.datasource.driver-class-name =oracle.jdbc.driver.OracleDriver
spring.datasource.url               =jdbc:oracle:thin:@192.168.0.1:1521:marpt
spring.datasource.username          =admin
spring.datasource.password          =123456
# MyBatis
mybatis.config-location             =classpath:/configs/mybatis/mybatis.cfg.xml
mybatis.mapper-locations            =classpath*:/mybatis/**/*Mapper.xml
# Log
spring.output.ansi.enabled          =DETECT
logging.path                        =/data/home/www/marpt/logs
logging.file                        =ma-rpt
logging.level.*                     =DEBUG
#debug=false

rpt.project.name                    =ma-rpt
rpt.project.develop                 =true

SpringBootConfig.java

@SpringBootConfiguration
public class SpringBootConfig implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
        configurer.setUseRegisteredSuffixPatternMatch(true);
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(true)
                .favorParameter(true)
                .parameterName("format")
                .ignoreAcceptHeader(true)
                .defaultContentType(MediaType.TEXT_HTML)
                .mediaType("html", MediaType.TEXT_HTML)
                .mediaType("json", MediaType.APPLICATION_JSON)
                .mediaType("xls", MediaType.valueOf("application/vnd.ms-excel"))
                .mediaType("xlsx", MediaType.valueOf("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        Set<String> modelKeys=new HashSet<>();
        modelKeys.add("list");
        modelKeys.add("table");

        registry.jsp("/views/", ".jsp");
        registry.enableContentNegotiation(new MappingJackson2JsonView());

        XlsView xlsView=new XlsView();
        xlsView.setModelKeys(modelKeys);
        registry.enableContentNegotiation(xlsView);

        XlsxView xlsxView=new XlsxView();
        xlsxView.setModelKeys(modelKeys);
        registry.enableContentNegotiation(xlsxView);

    }
}

homepage.jsp的主要内容

<%@ page language="java" pageEncoding="UTF-8" errorPage="../error/error.jsp" %>
<%@ include file="../../pages/common/taglibs.jsp" %>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
        <title>Home</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>

        <link href="${ctx}/assets/global/plugin/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
        <script src="${ctx}/assets/global/plugin/jquery/jquery.min.js" type="text/javascript"></script>
    </head>
    <body >
        <div class="page-wrapper">
            <%@include file="../../pages/share/partial/header.jsp" %>
            <div id="pageContainer" class="page-container">
                <jsp:include page="${ctx}/home/sidebar" />
                <jsp:include page="${ctx}/home/content" />
            </div>
        </div>

        <script src="${ctx}/assets/global/plugin/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
        <script>
            $(function() {
                alert('${ctx}');  // result is '/marpt'
                alert('${pageContext.request.contextPath}');    // result is '/marpt' too
            });
        </script>
    </body>
</html>

应用程序结构:

application structure

因此,如果定义了server.servlet.context-path=/marpt配置,如何使<%@include...<jsp:include...在Spring Boot的jsp文件中工作?非常感谢!

========================供应====================== =====
我也尝试降级1.5.18,将属性修改为server.context-path=/marpt,问题仍然存在,与Spring Boot的版本无关。

包含文件包括一些html标记,js和一些绑定模型属性(例如$ {param.dt} ..),不映射到servlet 的JSP文件。如果我在静态资源的路径中配置它们,也会失败。

=========================找出===================== ======
查看JB Nizet的评论

1 个答案:

答案 0 :(得分:0)

添加以下spring标记,以便使用 @ environment.getProperty('some.property')获得属性值“ server.servlet.context-path”应该起作用。

<spring:eval expression="@environment.getProperty('server.servlet.context-path')" var="ctx" />
<link href="${ctx}/assets/global/plugin/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />