简体   繁体   English

在 JSP 页面中的 contextPath 为空,同时将其与 Spring 引导一起使用

[英]contextPath is empty in JSP page while using it with Spring Boot

I want to load CSS on the JSP page.我想在 JSP 页面上加载 CSS。 But I am getting empty pageContext.request.contextPath in JSP page.但是我在 JSP 页面中得到了空的 pageContext.request.contextPath。 I am using this approach because if I move the page around in different directories within the templates or JSP then I should not need to keep on making changes to paths of referred CSS or js files.我正在使用这种方法,因为如果我在模板或 JSP 内的不同目录中移动页面,那么我不需要继续更改引用的 CSS 或 js 文件的路径。

Jsp page is as below Jsp 页面如下

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix = "x" uri = "http://java.sun.com/jsp/jstl/xml" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@page isELIgnored="false"%>
<%@ page isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Home Page</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/default-theme.css" />
</head>
<body>
    <div class="container">
        <H2>Reached Home.</H2>
    </div>
</body>
</html>

application.properties file is as below application.properties 文件如下

spring.mvc.view.prefix=/templates/jsp/
spring.mvc.view.suffix=.jsp

I am using Spring version 2.6.0.我正在使用 Spring 版本 2.6.0。 Along with the following dependencies in pom.xml- spring-boot-starter-web spring-boot-starter-tomcat (scope: provided) tomcat-embed-jasper (scope: provided) jstl连同 pom.xml 中的以下依赖项-spring-boot-starter-web spring-boot-starter-tomcat(范围:提供) tomcat-embed-jasper(范围:提供)jstl

As per Spring Boot, pageContext is Implicit.根据 Spring 引导,pageContext 是隐式的。 Then what could be going wrong here?那么这里可能出了什么问题? Any help or suggestions would be appriciated.任何帮助或建议都将不胜感激。

check if remove <%@page isELIgnored="false"%> if not work test: request.getContextPath()检查是否删除 <%@page isELIgnored="false"%> 如果不工作测试:request.getContextPath()

after all see below link: pageContext.request.contextPath not working毕竟见下面的链接: pageContext.request.contextPath not working

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM