Spring的静态内容

时间:2012-11-28 13:16:41

标签: css spring model-view-controller

我正在使用

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

在我的SpringMVC应用程序中处理静态内容。这很好用。我能够使用“/ resources”url上传图像并在应用程序中检索它们。我想在jsp中使用jpeg作为背景图像,所以我只是将jpeg放入工作空间的resources文件夹中。但是,我的工作区文件夹中没有任何资源文件夹。春天存储这些图像在哪里?如何到达此文件夹添加我的背景图像?

请求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_2_5.xsd" 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>SpringMVCTest</display-name>

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

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

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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<tx:annotation-driven /> 
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="/"></context:component-scan>

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

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxUploadSize="1000000" />

<bean id="personService" class="PersonServiceImpl" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/test" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" /> 
    <property name="packagesToScan" value="/" />
    <property name="hibernateProperties">
        <props> 
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

对保存图像方法的调用

saveImage(context.getRealPath("/resources/" + person.getId() + ".jpg"), image);

1 个答案:

答案 0 :(得分:0)

  

但是,我的工作区文件夹中没有任何资源文件夹

它应该出现在WebContent中。如果不存在,那么<mvc:resource>不会用于提供静态内容。

  

如何到达此文件夹添加背景图片?

在WebContent下创建资源文件夹并将图像放入其中。通过/resources/foo.jpeg访问图片。