我在Eclipse中有一个Spring项目。Maven将其打包为WAR,然后通过Eclipse将其部署到Tomcat。我遇到的问题是,/images、/styles和/webapp文件夹中的/js文件夹没有通过。页面将生成,Spring功能工作(调用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>some-project-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>正如您在上面看到的,我尝试在样式之前放置一个/,而没有将它们放在"js“部分。我查看了页面的源代码,并点击了这两个页面的链接,Tomcat回来说它也找不到。
如果我查看部署文件夹(/opt/apache-tomcat-8.0.32/webapps/some-project),,就会看到以下文件夹:
META-INF 网络-INF 图片 js 样式
下面是我在Eclipse中的webapp文件夹:

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

发布于 2016-05-04 01:11:18
在xml弹簧配置方面,我没有多少经验。但是,当我查看一个项目时,我发现我的资源配置如下。请注意,我的文件结构也与您的类似。
<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>发布于 2016-05-04 01:14:57
我认为您的web.xml配置有问题。
<servlet-mapping>
<servlet-name>some-project</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>这意味着您不能访问静态资源。有两种解决办法:
<url-pattern>/*</url-pattern>发布于 2016-05-04 01:42:23
在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" />

https://stackoverflow.com/questions/37016678
复制相似问题