如何在Spring MVC项目的“jsp”中使用“css”?

时间:2014-03-10 23:32:37

标签: java css spring jsp spring-mvc

我正在开发一个spring MVC项目,我试图通过引用它来设计我的JSP页面来使用css样式表。但不知怎的,一旦我在控制器中按下我的方法,我的css文件就不会被拾取..

下面是我的JSP文件 -

<html>
<head>
<link rel="stylesheet" type="text/css" href="test.css" />
</head>

<body>

<h1>All header 1 elements will be red</h1>
<h2>All header 2 elements will be blue</h2>
<p>All text in paragraphs will be green.</p>

</body>
</html>

以下是我的test.css文件 -

h1 {color:red;}
h2 {color:blue;}
p {color:green;}    

以下是我在控制器类中的方法 -

@RequestMapping(value = "testing", method = RequestMethod.GET)
public Map<String, String> testing() {

    final Map<String, String> model = new LinkedHashMap<String, String>();

    return model;
}

目录结构是这样的 -

webapp/
|-- resources/
|   +-- css/
|           test.css
+- WEB-INF/
  +-- views/
        testing.jsp

但不知怎的,它对我不起作用..我在这里做错了吗?

更新: -

这是我的web.xml文件 -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>testweb</display-name>
  <listener>
    <listener-class>com.host.webres.resource.env.EbayResourceRuntimeListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>index</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/testweb/*</url-pattern>
  </servlet-mapping>
</web-app>

以下是我的context.xml文件 -

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!--  Allow proxys -->
    <aop:aspectj-autoproxy />

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven>
            <mvc:message-converters>
              <!--  Support AJAX processing with progressive rendering. Overrides HttpOutputMessage with RaptorResponseWriter -->
            <beans:bean class="com.host.terr.kernel.filter.RaptorJacksonHttpMessageConverter"/>                 
            </mvc:message-converters>
    </mvc:annotation-driven>

    <context:component-scan base-package="com.host.terr.config" />
    <context:component-scan base-package="com.host.personalization.bullseye.zookeeper.p13nzook.controller" />       

    <!-- Handles HTTP GET requests by efficiently serving up static resources 
         in the corresponding directory -->
    <resources mapping="/js/**" location="/js/" />
    <resources mapping="/css/**" location="/css/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
</beans:beans>

这是我在我的控制台上获得的异常,但浏览器显示没有任何css的数据 -

404 /testweb/test.css Not Found /testweb/test.css Not Found }, Correlations : {} }

5 个答案:

答案 0 :(得分:0)

在你的servlet-context.xml文件中,你已经设置了你的资源目录。在你的情况下,改变了

<resources mapping="/resources/**" location="/resources/"/>

并在您的jsp页面中,如下所示,

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

我希望它会起作用

答案 1 :(得分:0)

您需要提供上下文路径。

 <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/test.css" />

What does this expression language ${pageContext.request.contextPath} exactly do in JSP EL?

答案 2 :(得分:0)

您需要将<base/>设置为以下内容......

<c:set var="req" value="${pageContext.request}" />

<c:set var="url"> ${req.requestURL} </c:set>
<c:set var="uri" value="${req.requestURI}" />

<head>
    <title></title>
    <base href="${fn:substring(url, 0, fn:length(url) - fn:length(uri))}${req.contextPath}/" />

然后您可以导入任何JS或CSS,如下所示..

<script src="assets/js/angular/angular.js"></script>

答案 3 :(得分:0)

也许通过在WEB-Content中的特定文件夹中添加.css文件,您可以使用属性href = "yourFolder/yourFile.css 事实上,所有&#34; web&#34;视图之类的资源应该存在(即jsp,css。)。 您还可以通过添加以下内容来确定您的链接(在.jsp文件中)是样式表:rel="stylesheet"

你的链接看起来像这样:

答案 4 :(得分:-2)

此css资源问题

请尝试以下

 <link rel="stylesheet" type="text/css" href="css/test.css" />

如果由bowaser加载,您可以看到此资源。