spring boot 2.1.0 war 文件无法在jsp 中配置'context-path' 或'include' 不起作用 [英] spring boot 2.1.0 war file cannot config 'context-path' or else 'include' in jsp not working

查看:26
本文介绍了spring boot 2.1.0 war 文件无法在jsp 中配置'context-path' 或'include' 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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.登陆后jspinclude不起作用,两种inclue命令,<%@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

<modelVersion>4.0.0</modelVersion><groupId>com.test</groupId><artifactId>map-rpt</artifactId><version>1.5.0</version><包装>战争</包装><name>map-rpt</name><description>Spring Boot</description><父母><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.0.RELEASE</version><relativePath/><!-- 从存储库中查找父级 --></父母><属性><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></属性><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></依赖><依赖><groupId>net.sf.ehcache</groupId><artifactId>ehcache-core</artifactId><version>2.6.11</version></依赖><依赖><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.4.0</version></依赖><!-- Shiro ehCache --><依赖><groupId>org.apache.shiro</groupId><artifactId>shiro-ehcache</artifactId><version>1.4.0</version></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><范围>提供</范围></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></依赖><依赖><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></依赖><依赖><groupId>org.springframework.session</groupId><artifactId>spring-session-core</artifactId></依赖><依赖><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></依赖><依赖><groupId>com.oracle</groupId><artifactId>ojdbc7</artifactId><version>12.1.0.2</version></依赖><依赖><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId><范围>提供</范围></依赖><依赖><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId></依赖><!-- jstlsupport --><依赖><groupId>javax.servlet</groupId><artifactId>jstl</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></依赖></依赖项><构建><finalName>marpt</finalName><插件><插件><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></插件></plugins><资源><资源><目录>src/main/resources</directory><filtering>true</filtering><包括><include>application.properties</include><include>properties/*.properties</include><include>**/*.xml</include><include>**/*.tld</include></包括></资源><资源><目录>src/main/webapp</directory></资源></资源></build></项目>

application.properties

# Spring 公开server.port =8096server.servlet.context-path =/marpt# 缓存spring.cache.type =ehcache# D bspring.datasource.driver-class-name =oracle.jdbc.driver.OracleDriverspring.datasource.url =jdbc:oracle:thin:@192.168.0.1:1521:marptspring.datasource.username = adminspring.datasource.password =123456# MyBatismybatis.config-location =classpath:/configs/mybatis/mybatis.cfg.xmlmybatis.mapper-locations =classpath*:/mybatis/**/*Mapper.xml# 日志spring.output.ansi.enabled =检测logging.path =/data/home/www/marpt/logslogging.file =ma-rptlogging.level.* =调试#调试=假rpt.project.name =ma-rptrpt.project.develop = 真

SpringBootConfig.java

@SpringBootConfiguration公共类 SpringBootConfig 实现 WebMvcConfigurer {@覆盖public void configurePathMatch(PathMatchConfigurer configurer) {configurer.setUseSuffixPatternMatch(false);configurer.setUseRegisteredSuffixPatternMatch(true);}@覆盖public void configureContentNegotiation(ContentNegotiationConfigurer 配置器) {configurer.favorPathExtension(真).favorParameter(真).parameterName("格式").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"));}@覆盖public void configureViewResolvers(ViewResolverRegistry 注册表){设置<字符串>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" %><%@包含文件=../../pages/common/taglibs.jsp"%><!DOCTYPE html><头><meta charset="utf-8"/><meta http-equiv="content-type" content="text/html;charset=UTF-8"/><title>首页</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><身体><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"/>

<script src="${ctx}/assets/global/plugin/bootstrap/js/bootstrap.min.js" type="text/javascript"></script><脚本>$(函数(){alert('${ctx}');//结果是 '/marpt'alert('${pageContext.request.contextPath}');//结果也是 '/marpt'});</html>

应用结构:

应用结构

所以,如果我定义了 server.servlet.context-path=/marpt 配置,我该如何制作 <%@include...><jsp:include... 在 Spring Boot 中处理 jsp 文件?非常感谢!

======================== 供应 ============================
我也试过降级1.5.18,修改属性为server.context-path=/marpt,问题依然存在,与spring boot的版本无关.

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

======================== 弄清楚 ==========================
JB Nizet 的评论

解决方案

添加以下 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"/>

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

解决方案

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

这篇关于spring boot 2.1.0 war 文件无法在jsp 中配置'context-path' 或'include' 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
服务器开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆