JavaEE(Glassfish)环境下页面跳转异常及JS/CSS加载问题
Troubleshooting Your 404 Redirect & JS Loading Issues
Hey there, let's walk through fixing these two pesky issues one by one. I've run into similar stuff before, so here are the most likely fixes to check:
1. Fixing the Misbehaving 404 Page (Redirecting to Root Instead)
This usually boils down to server configuration or routing rules overriding the default 404 behavior. Here's what to check:
- Verify your
web.xmlerror page setup: Make sure you've explicitly mapped 404 errors to your custom page. Add this snippet if it's missing:<error-page> <error-code>404</error-code> <location>/404.html</location> <!-- Replace with your actual 404 page path --> </error-page> - Check for rogue filters or redirect rules: If you're using a framework like Spring MVC, double-check interceptors or view controllers—sometimes a misconfigured rule will catch all unknown requests and redirect them to the root. For example, a wildcard route that's set to redirect instead of throwing a 404.
- Test the 404 page directly: Visit
http://localhost:8080/XXXX/404.html(or your actual 404 page URL) to confirm the page itself loads correctly. If it doesn't, fix that first before worrying about the redirect. - Check server-wide config: For Tomcat users, peek into
conf/web.xmlin your Tomcat installation—sometimes the default error page settings get modified, causing unexpected redirects.
2. Fixing the test.js Loading Exception
Static resource issues are almost always path-related or blocked by framework rules. Let's troubleshoot:
- Validate the path: The relative path
src="test.js"will look for the file at the same level as your current page. If your page is athttp://localhost:8080/XXXX/some/subpage, it'll try to loadhttp://localhost:8080/XXXX/some/test.jsinstead of the root. Try using an absolute path to rule this out:<script type="text/javascript" src="/XXXX/test.js"></script> - Check browser dev tools: Open your browser's Network tab and look for the
test.jsrequest. What's the status code? If it's 404, the file isn't where you think it is. If it's 500, there's a server-side issue serving static files. - Unblock static resources (framework-specific): If you're using Spring MVC, you need to add
<mvc:default-servlet-handler/>to your context config—otherwise, the framework will treat static JS files as controller requests and return 404. - Double-check the JS file: Even simple code can have typos (like missing semicolons or mismatched brackets). Load the JS file directly in your browser (
http://localhost:8080/XXXX/test.js) to make sure it renders correctly, no server errors.
内容的提问来源于stack exchange,提问作者etienne31
相关产品推荐
相关产品推荐

