programing

컨텍스트 루트 이름을 포함하지 않고 상대 경로를 사용하는 방법은 무엇입니까?

nasanasas 2020. 10. 18. 18:20
반응형

컨텍스트 루트 이름을 포함하지 않고 상대 경로를 사용하는 방법은 무엇입니까?


내 정적 파일 (CSS, JS)을 작업하려면 /AppName/templates/style/main.css. 상대 경로를 작성할 수있는 해결책이 style/main.css있습니까?


실제 관심사가 webapp 컨텍스트 ( "AppName"부분)의 동적 성인 경우 HttpServletRequest#getContextPath().

<head>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/templates/style/main.css" />
    <script src="${pageContext.request.contextPath}/templates/js/main.js"></script>
    <script>var base = "${pageContext.request.contextPath}";</script>
</head>
<body>
    <a href="${pageContext.request.contextPath}/pages/foo.jsp">link</a>
</body>

당신은 당신이 반복 할 필요가 없습니다 있도록 모든 상대 링크에 대한 기본 경로를 설정하려는 경우 ${pageContext.request.contextPath}모든 상대 링크를 사용하는 <base>태그를. 다음은 JSTL 함수의 도움을받은 예입니다 .

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<head>
    <c:set var="url">${pageContext.request.requestURL}</c:set>
    <base href="${fn:substring(url, 0, fn:length(url) - fn:length(pageContext.request.requestURI))}${pageContext.request.contextPath}/" />
    <link rel="stylesheet" href="templates/style/main.css" />
    <script src="templates/js/main.js"></script>
    <script>var base = document.getElementsByTagName("base")[0].href;</script>
</head>
<body>
    <a href="pages/foo.jsp">link</a>
</body>

이렇게하면 모든 상대 링크 (즉 , 또는 구성표로 시작 하지 않음/ )가 <base>.

이것은 어떤 식 으로든 Tomcat과 특별히 관련이 없습니다. HTTP / HTML 기본과 관련이 있습니다. 다른 모든 웹 서버에서 동일한 문제가 발생합니다.

또한보십시오:


응용 프로그램 컨텍스트 상대 경로와 함께 <c:url>-tag사용하십시오 .

value매개 변수가로 시작 /, 다음 태그는 응용 프로그램 상대 URL로 취급하고 URL로 응용 프로그램 이름을 추가합니다. 예:

jsp :

<c:url value="/templates/style/main.css" var="mainCssUrl" />`
<link rel="stylesheet" href="${mainCssUrl}" />
...
<c:url value="/home" var="homeUrl" />`
<a href="${homeUrl}">home link</a>

도메인 상대 URL이있는이 html이됩니다.

<link rel="stylesheet" href="/AppName/templates/style/main.css" />
...
<a href="/AppName/home">home link</a>

Tomcat의 $ cwd 인 일부 디렉토리에서 tomcat을 시작합니다. 이 $ cwd에 상대적인 경로를 지정할 수 있습니다.

당신이 가지고 있다고 가정

home
- tomcat
 |_bin
- cssStore
 |_file.css

And suppose you start tomcat from ~/tomcat, using the command "bin/startup.sh".

~/tomcat becomes the home directory ($cwd) for tomcat

You can access "../cssStore/file.css" from class files in your servlet now

Hope that helps, - M.S.


Instead using entire link we can make as below (solution concerns jsp files)

With JSTL we can make it like: To link resource like css, js:

     <link rel="stylesheet" href="${pageContext.request.contextPath}/style/sample.css" />
     <script src="${pageContext.request.contextPath}/js/sample.js"></script>   

To simply make a link:

     <a id=".." class=".." href="${pageContext.request.contextPath}/jsp/sample.jsp">....</a>

It's worth to get familiar with tags

   <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

There is also jsp method to do it like below, but better way like above:

   <link rel="stylesheet" href="<%=request.getContextPath()%>/style/sample.css" />
   <script type="text/javascript" src="<%=request.getContextPath()%>/js/sample.js"></script>

To simply make a link:

   <a id=".." class=".." href="<%=request.getContextPath()%>/jsp/sample.jsp">....</a>

This could be done simpler:

<base href="${pageContext.request.contextPath}/"/>

All URL will be formed without unnecessary domain:port but with application context.


This is a derivative of @Ralph suggestion that I've been using. Add the c:url to the top of your JSP.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:url value="/" var="root" />

Then just reference the root variable in your page:

<link rel="stylesheet" href="${root}templates/style/main.css">

참고URL : https://stackoverflow.com/questions/4764405/how-to-use-relative-paths-without-including-the-context-root-name

반응형