Spring无法找到资源

时间:2017-04-19 19:38:43

标签: java spring jsp spring-mvc servlets

我查看了所有回复,但似乎无法弄清楚我的问题。我无法将我的资源css文件导入jsp。我的资源位于main / webapp / resources /**.

以下是我的文件:

的web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">

<!-- For web context -->
<servlet>
    <servlet-name>geodispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/geomonitor-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>geodispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>logbackConfigLocation</param-name>
    <param-value>/WEB-INF/logback.xml</param-value>
</context-param>

geomonitor-servlet.xml中:

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

<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/"/>

<context:component-scan base-package="com.jmt.geomonitor.controller"/>
<context:component-scan base-package="com.jmt.geomonitor.integration"/>
<context:component-scan base-package="com.jmt.geomonitor.service"/>
<context:component-scan base-package="com.jmt.geomonitor.domain"/>

<!-- View resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<!-- Tiles configuration -->
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles/tiles-definitions.xml</value>
        </list>
    </property>
</bean>

的build.gradle:

apply plugin: 'java'

sourceCompatibility = 1.5

allprojects {

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.mockito', name: 'mockito-all', version: '1.8.4'
    compile group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.0'
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
    compile group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.1'
    compile group: 'javax.servlet', name: 'jstl', version: '1.2'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'

    //spring
    compile group: 'org.springframework', name: 'spring-core', version: '4.1.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-beans', version: '4.1.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc', version: '4.1.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-web', version: '4.1.6.RELEASE'

    //logger
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.7'
    compile group: 'ch.qos.logback', name: 'logback-core', version:'1.1.2'

    //apache commons
    compile group: 'commons-io', name: 'commons-io', version: '2.4'
}
 }

我的jsp中有以下行来导入资源:

<link href="/resources/css/styles.css" rel="stylesheet">

1 个答案:

答案 0 :(得分:0)

要在JSP页面中包含CSS或JS,您可以使用JSTL标记spring:url或Spring标记<link href="<c:url value="/resources/css/styles.css" />" rel="stylesheet"> <spring:url value="/resources/css/styles.css" var="mainCss" /> <link href="${mainCss}" rel="stylesheet" />

summary(ets(ts(c(100, 110, 120, 200, 300, 100, 200, 100, 120, 300), start = c(2009, 1), frequency = 12)))