【问题标题】:Static Content with Spring使用 Spring 的静态内容
【发布时间】:2012-11-28 13:16:40
【问题描述】:

我正在使用

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

在我的 SpringMVC 应用程序中处理静态内容。这工作正常。我可以使用“/resources”url上传图像并在应用程序中检索它们。我想在jsp中使用jpeg作为背景图像,所以我只是将jpeg放到我工作区的资源文件夹中.但是,我的工作区文件夹中的任何地方都没有资源文件夹。 spring 在哪里存储这些图像?如何进入此文件夹以添加我的背景图片?

根据请求的 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);

【问题讨论】:

  • 你能发布你的web.xml吗?
  • 当然可以,但是为什么呢?它包含的只是调度程序 servlet 的映射。
  • 想要确保您没有资源 servlet 定义

标签: css spring model-view-controller


【解决方案1】:

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

它应该出现在 WebContent 中。如果它不存在,则 &lt;mvc:resource&gt; 不会用于提供静态内容。

如何进入此文件夹以添加我的背景图片?

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

【讨论】:

  • 如果没有使用 spring 如何访问我在我的网络应用程序中上传的图像?我确实尝试在此位置添加资源文件夹,但没有任何区别。
  • Spring 不决定您将 上传的 图像保存在哪里。它完全取决于您/代码来决定在哪里存储图像。 IMO 上传的图像将存储在数据库中。
  • 对,我正在使用 saveImage(context.getRealPath("/resources/" + person.getId() + ".jpg"), image);将它们保存到资源文件夹中,以便我的 jsp 可以访问它们。但是资源文件夹没有出现在我的工作区文件夹中。
  • 谢谢,但没有说明资源文件夹在fie系统中的位置。
猜你喜欢
  • 1970-01-01
  • 2018-02-10
  • 2013-06-12
  • 2011-10-14
  • 1970-01-01
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多