Spring 引导 2.1.0 war 文件无法配置'context-path'否则 JSP 中的'include'不起作用



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

我在application.properties文件中配置了server.servlet.context-path=/marpt,这样应用程序就不能直接通过ip+端口访问
然后我使用java -jar marpt.war命令运行WAR,它成功启动
然后我在Chrome中打开正在运行的web应用程序,出现了一些问题:
1。索引登录页面正确显示
2.登录后,jspinclude不工作,两种类型的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>

应用程序属性

# 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>

应用程序结构:

应用程序结构

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

==============================供应==========================
我试图将太1.5.18降级,将属性修改为server.context-path=/marpt,问题仍然存在,与春季启动的版本无关。

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

====================================================
请参阅JB Nizet的评论

添加以下spring标记,以便使用@environment.getProperty('some.properties')获取属性值"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" />

最新更新