无法在spring web mvc中将.jsp与.css链接

时间:2015-01-03 14:19:18

标签: java css spring jsp spring-mvc

我已经阅读了我在这个问题上发现的大部分相关帖子,但我找不到任何有效的解决方案。 基本上我的jsp文件似乎无法找到css文件。

以下是我的文件: jsp页面:

 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Search for flight</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link href="${pageContext.request.contextPath}/WEB-INF/pages/theme.css" rel="stylesheet" type="text/css" >
    </head>

css页面:

#title
{
    font-family: "Times New Roman";
    color: aqua;
}

MVC-调度-servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.mkyong.common.controller" />

    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

的web.xml:

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

控制器工作正常,所以在发布时我找不到任何实际用途。

jsp文件在浏览器中可见,但样式完全不存在。

提前谢谢!

4 个答案:

答案 0 :(得分:3)

您需要在dispatcher-servlet文件中声明您的资源

<mvc:resources mapping="/resources/**" location="/resources/css/" />

使用url mapping / resources / **的任何请求都将直接查找/ resources / css /

现在在jsp文件中你需要包含你的css文件:

<link href="<c:url value="/resources/css/your file.css" />" rel="stylesheet">

完整示例https://github.com/abdotalaat/SpringMVCCSS

答案 1 :(得分:1)

我建议在弹簧配置文件(mvc-dispatcher-servlet.xml)中添加以下行。

<mvc:resources mapping="/pages/*.css" location="/pages/"/>
<mvc:default-servlet-handler />

答案 2 :(得分:0)

所以我解决了我的问题。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

添加此依赖项后,剩下的几乎完美无缺。 谢谢你的解释!我对现在的工作原理有了更好的了解。

答案 3 :(得分:0)

我有同样的问题: -检查是否在pom.xml中添加了JSTL并在jsp文件中声明 -检查webapp文件夹内的资源文件夹 -检查href您的链接CSS正确