spring boot 2.1.0 war file cannot config 'context-path' or else 'include' in jsp not working

丶灬走出姿态 提交于 2019-12-13 21:24:33

问题


I'm using Spring Boot 2.1.0 to develop a web application. I package the codes into a WAR file with maven.

I configured server.servlet.context-path=/marpt in application.properties file so that application can not access by ip+port directly.
Then I run the WAR using java -jar marpt.war command, and it is successed start.
Then I open the running web application in Chrome, there're some problems throw out:
1. The index login page shows corrcetly;
2. After login, jsp include not working, both of two types of inclue command, <jsp:include page="${ctx}/home/sidebar" /> and <%@include file="../../pages/share/partial/header.jsp" %> and any other references.

I use javascript to alert the ${ctx} which I defined in another jsp file, and include it in top of homepage by <%@ include file="../../pages/common/taglibs.jsp" %>, it's very strange, the taglibs.jsp can be included. In it I set <c:set var="ctx" value="${pageContext.request.contextPath}" />. In home page I do this: alert('${pageContext.request.contextPath}'), the result is /marpt.
I'm confused where is wrong.

I try to package the jar file, it's also start correctly, but pages cannot open in browser at all.

Below are main configuration files:
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);

    }
}

Main of 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>

The application structure:

application structure

So, if I define the server.servlet.context-path=/marpt configure, how can I make <%@include... or <jsp:include... working in jsp file in Spring Boot? Thanks very much!

======================== supply ===========================
I tried to downgrade too 1.5.18, modified properties to server.context-path=/marpt, the problem is still exist, not relate to the version of spring boot.

The include files are some html tags, js, and some bind model attributes(such as ${param.dt}..), JSP files which are NOT mapping to servlet. If I configure them in a static resources' path, also failure.

======================== figure out ===========================
see JB Nizet's comment


回答1:


Add the following spring tag in order to get property value "server.servlet.context-path" using @environment.getProperty('some.property') should work.

<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" />


来源:https://stackoverflow.com/questions/54059281/spring-boot-2-1-0-war-file-cannot-config-context-path-or-else-include-in-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!