我无法从jsp页面链接到样式表。我不知道什么是错的。 这是我的目录结构:
|
|-- WEB-INF
| |
| |-- jsp
| |-- index.jsp
|
|-- resources
|
|-- css
|-- style.css
我试过这个
<link rel="stylesheet" type="text/css" href="/resources/css/style.css" />
这是我的web.xml
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
请帮助我做错了什么。
提前致谢
答案 0 :(得分:1)
当您使用url“/”映射spring-servlet(Spring)时,所有请求都将重定向到Spring-servlet。包括.css&amp; .js和其他人。
虽然您找到了解决方案,但我为您提供了另一种替代方案。
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>static</servlet-name>
<servlet-class>StaticServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>static</servlet-name>
<url-pattern>/images/*</url-pattern>
<url-pattern>/css/*</url-pattern>
<url-pattern>/img/*</url-pattern>
<url-pattern>/js/*</url-pattern>
<url-pattern>/swf/*</url-pattern>
</servlet-mapping>
提供资源的静态Servlet
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
public class StaticServlet extends HttpServlet {
private static final long serialVersionUID = 8458501870440200891L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String resourcePath = req.getServletPath() + req.getPathInfo();
String realPath = getServletContext().getRealPath(resourcePath);
FileInputStream fileInputStream = new FileInputStream(realPath);
try {
IOUtils.copy(fileInputStream, resp.getOutputStream());
} finally {
fileInputStream.close();
}
}
}
答案 1 :(得分:0)
您的css表位于css目录下。试试href =“/ resources / css / style.css”
答案 2 :(得分:0)
我跟着this steps,像魅力一样工作
答案 3 :(得分:0)
在dispatcher-servlet.xml文件中,make-sure跟随可用
的xmlns:MVC = “http://www.springframework.org/schema/mvc”
的xsi:的schemaLocation =“http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd“
和
然后你应该可以通过href =“/ resources / css / style.css”来访问你的css