Vje*_*tje 4 css java jsp spring-mvc web-inf
我已经部署了一个 web 应用程序,localhost:8080/Project
其中 Project 是上下文名称。webapp 正确地将用户重定向到 index.jsp。
但是找不到这个 index.jsp 中的 css 文件(错误 404)。
Index.jsp 包含此参考:
<html lang="en">
<head>
<link href="../css/style.css" rel="stylesheet">
</head>
</html>
Run Code Online (Sandbox Code Playgroud)
我的项目在 WEB-INF 文件夹中有以下结构:
WEB-INF/views/index.jsp
WEB-INF/css/style.css
Run Code Online (Sandbox Code Playgroud)
我也尝试通过 url 直接调用 css 文件,localhost:8080/Project/css/style.css
但这也不起作用。
我按照建议将 css 文件夹上一级(与 WEB-INF 处于同一级别)。问题仍然存在。我是否必须排除/css
以 this开头的网址@Controller
?或者,也许我走错了路......
@Controller
@RequestMapping("/")
public class IndexController {
@RequestMapping(method = RequestMethod.GET)
public String sayHello(ModelMap model) {
return "index";
}
}
Run Code Online (Sandbox Code Playgroud)
浏览器加载localhost:8080/Project
然后继续加载../css/style.css
相对于该 URL,因此尝试加载localhost:8080/css/style.css
显然不起作用。
您不应将 CSS、JS 和图像等 Web 资源放在下面,/WEB-INF
因为它们无法从该位置访问。而是使用类似的东西
WEB-INF/views/index.jsp
css/style.css
Run Code Online (Sandbox Code Playgroud)
并在 JSP 中写
<link href="css/style.css" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4927 次 |
最近记录: |