使用JSP标签构建站点模板时移动文件后出现文件未找到异常
Hey there, let's tackle this frustrating issue where one JSP include works but the header include throws a FileNotFoundException after moving your files. Let's break down the most likely causes and fixes step by step:
1. Verify the File's Actual Location & Path Match
First things first—double-check that the file exists exactly where the error says it should. Head to your web app's deployment directory (e.g., Tomcat/webapps/your-app-name/) and confirm the path /user/components/layout/header.jsp is present. It's easy to miss a folder during migration (like forgetting to move the components directory) or mix up directory names (e.g., component instead of components).
2. Fix Path Resolution for Web App Context
A common gotcha with JSP's <jsp:include> is how absolute paths are interpreted:
- Paths starting with
/are relative to your web app's context root—but only if your app is deployed at the server's root (e.g., Tomcat'sROOTfolder). If your app is deployed under a subpath (like/my-app), the path/user/...will look for files at the server's root, not your app's root.
To fix this, use the context path to build a fully correct path:
<jsp:include page="${pageContext.request.contextPath}/user/components/layout/header.jsp" />
This ensures the path always points to the right location relative to your app's root, no matter how it's deployed.
3. Check Static vs Dynamic Include Differences
If you're mixing <%@ include %> (static include) and <jsp:include> (dynamic include), their path logic differs:
<%@ include file="/user/..." %>interprets/as the server's root directory, not your app's root.<jsp:include page="/user/..." %>interprets/as your web app's context root.
If your working include uses one type and the broken header uses the other, this mismatch could cause the error. Make sure both use consistent path logic appropriate for your deployment setup.
4. Clear Server Compilation Cache
JSPs are compiled into Java classes and cached by your server (e.g., Tomcat's work directory). After moving files, old cached classes might still reference the old paths. To fix this:
- Stop your server
- Delete all files in
Tomcat/work/Catalina/localhost/your-app-name/ - Restart the server to force JSPs to recompile with the new paths
5. Verify File System Permissions
If you're on a Linux/Unix server, ensure the user running your application server (e.g., the tomcat user) has read access to the header.jsp file and all parent directories. Use these commands to check and fix permissions:
# Check permissions ls -l /path/to/your-app/user/components/layout/header.jsp # Set read permissions for the file chmod 644 /path/to/your-app/user/components/layout/header.jsp # Set execute permissions for parent directories (required to traverse them) chmod 755 /path/to/your-app/user/components/layout/ chmod 755 /path/to/your-app/user/components/
Start with the first check (file existence)—it's the most common culprit. If that checks out, move through the other steps one by one, and you should get that header include working again.
内容的提问来源于stack exchange,提问作者Nguyễn Việt Hưng

