Spring MVC项目缺少图像&在Eclipse中部署到Tomcat时的样式

时间:2016-05-04 00:49:10

标签: java eclipse spring-mvc

我在Eclipse中有一个Spring MVC项目。 Maven将其打包为WAR,然后通过Eclipse将其部署到Tomcat。我遇到的问题是/ webapp文件夹中的/images/styles/js文件夹未通过。该页面将生成并且Spring MVC功能正常工作(调用Controller等)。但是,没有任何样式会出现。我非常感谢任何有助于此工作的帮助。

的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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>Some Project</display-name>
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>some-project.root</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

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

    <servlet-mapping>
        <servlet-name>some-project</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
</web-app>

一些项目-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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-4.0.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="my.com.project.controllers" />

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="cacheSeconds" value="60" />
    </bean>

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

    <!-- Need this to handle multipart files -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

    <bean class="my.com.project.validators.SomeValidator" />
    <bean class="my.com.project.validators.OtherValidator" />
</beans>

pom.xml(部分内容

...
<packaging>war</packaging>
...
<!-- The Build -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <warName>some-project</warName>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
...
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.2.5.RELEASE</version>
</dependency>
<!-- using Hibernate 4.3.11.Final -->

JSP

<link rel="stylesheet" type="text/css" media="all" href="/styles/screen.css" />
<link rel="stylesheet" type="text/css" media="print" href="/styles/print.css" />
<link rel="stylesheet" type="text/css" href="/styles/jquery/jquery-ui.css">
<link rel="stylesheet" type="text/css" href="/styles/roper-center.css">
<link rel="stylesheet" type="text/css" href="/styles/font-awesome-4.5.0/css/font-awesome.css">

<script src="js/modernizr.js"></script>
<script src="js/framework/iws.js"></script>
<script src="js/sorttable.js"></script>
<script src="js/jquery/jquery-1.12.2.min.js"></script>
<script src="js/jquery/external/jquery/jquery.js"></script>
<script src="js/jquery/jquery-ui.js"></script>
<script src="js/jquery/jquery.autotab.js"></script>

正如您在上面所看到的,我尝试在样式之前添加/并且没有将它们放在&#34; js&#34;部分。我查看了页面的页面源,并点击了两者的链接,Tomcat回来说它找不到。

如果我查看部署文件夹(/opt/apache-tomcat-8.0.32/webapps/some-project),我会看到以下文件夹:

  

META-INF
  WEB-INF
  图像
  js
  样式

这是我在Eclipse中的webapp文件夹:

webapps folder list

以防万一,这是我的部署程序集

enter image description here

3 个答案:

答案 0 :(得分:1)

对于xml spring配置,我没有太多经验。但是,当我查看一个项目时,我发现我的资源有以下配置。注意我的文件结构也和你的一样。

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

我在同一个文件中声明我的InternalResourceViewResolver。希望它有所帮助。

<小时/> 完整的文件:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    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.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="my.app.package" />

    <!-- Internal (final) resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
        <property name="order">
            <value>4</value>
        </property>
    </bean>

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

</beans>

答案 1 :(得分:0)

我认为您的web.xml配置存在问题。

<servlet-mapping>
   <servlet-name>some-project</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>

这意味着您无法访问静态资源。 两个解决方案:

  1. <url-pattern>/*</url-pattern>

  2. <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.css</url-pattern>  
    </servlet-mapping>  
    
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.gif</url-pattern>  
    
    </servlet-mapping>  
    
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.jpg</url-pattern>  
    </servlet-mapping>  
    
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.js</url-pattern>  
    </servlet-mapping> 
    

答案 2 :(得分:0)

在WebContent下面有一个名为resources的文件夹,里面有不同的文件夹作为脚本(对于js文件),图像和css。

在some-project-servlet.xml中添加以下内容以获取spring以识别您的资源。

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

如果你已经没有它,也不要忘记获取mvc命名空间。

然后在jsp中包含资源,如下所示。

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

<link href="${pageContext.request.contextPath}/resources/script/jquery.js" rel="script" type="text/javascript" />

enter image description here