简体   繁体   English

解决jsp中css的路径

[英]solve the path to css in jsp

I'm using Spring MVC 3.0 to construct a rest-style url. 我正在使用Spring MVC 3.0构造一个Rest风格的URL。 Here is part of my code: 这是我的代码的一部分:

@RequestMapping(value = {"/", "/posts"}, method = RequestMethod.GET)
public String getNewestPosts(Model model, HttpServletRequest request)
        throws DataAccessException {
    return getPostsByPage(1, model, request);
}

@RequestMapping(value = "/posts/page/{page}", method = RequestMethod.GET)
public String getPostsByPage(@PathVariable long page, Model model,
        HttpServletRequest request) throws DataAccessException {
    // ... get the posts by page number
}

I wrote two methods. 我写了两种方法。 One handles the request from url "/posts" which means retrieving the first page of the posts , and the other one handles the request from url "/posts/page/{page}" which means retrieving the posts according to the path variable {page}. 一个处理来自网址“ / posts”的请求,这意味着检索帖子的第一页,另一个处理来自网址“ / posts / page / {page}”的请求,这意味着根据路径变量{页}。 And the problem is that all the two methods above point to the same view which is a jsp file, but they're in different paths("/posts, "/posts/page/xxx"). The css path (../style.css) can not adapt both of them. I try to solve it by using absolute css path(/style.css), which means the web application works only if the application is deployed on a root path("/"). I would appreciate it if you could help me. 问题是上述两种方法都指向同一视图(即一个jsp文件),但是它们位于不同的路径(“ / posts,” / posts / page / xxx“)中。css路径(../ style.css)不能同时使用它们。我尝试通过使用绝对CSS路径(/style.css)来解决它,这意味着Web应用程序仅在将应用程序部署在根路径(“ /”)上时才能工作。如果您能帮助我,我将不胜感激。

Use the <c:url> tag, which prepends the context path to an absolute URL. 使用<c:url>标记,该标记会将上下文路径添加到绝对URL。

<link rel="stylesheet" type="text/css" href="<c:url value="/style.css" />" />

or 要么

<c:url var="cssUrl" value="/style.css" />
<link rel="stylesheet" type="text/css" href="${cssUrl}" />

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

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