【问题标题】:I can show images in a SPRING 3 MVC project我可以在 SPRING 3 MVC 项目中显示图像
【发布时间】:2013-01-18 13:43:15
【问题描述】:

我无法在 Spring 3 MVC 项目中显示图像。我没有找到错误并且已经阅读了很多教程。我的代码是下一个:

调度程序-servlet

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

<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="com.barrancofv" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

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

    <bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />
    </bean>

    <bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="es"/>
    </bean>

    <bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <ref bean="localeChangeInterceptor" />
    </property>
    </bean>    

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>mymessages</value>
            <value>messages</value>
        </list>
    </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
    <list>
        <value>com.barrancofv.model.Empleado</value>
        <value>com.barrancofv.model.Documento</value>
    </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        </props>
    </property>
    </bean>

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

web.xml

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

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

<listener>
<listener-class>
    org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
    /WEB-INF/dispatcher-servlet.xml,
    /WEB-INF/spring-security.xml
</param-value>
</context-param>

<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
    org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Security.xml

<http use-expressions="true">
    <!-- intercept-url seguridad de flujo de paginas -->
    <intercept-url pattern="/welcome" access="ROLE_USER" />
    <intercept-url pattern="/welcome/*" access="ROLE_USER" />
    <intercept-url pattern="/welcome/*/" access="ROLE_USER" />
    <intercept-url pattern="/welcome/*/*" access="ROLE_USER" />
    <intercept-url pattern="/welcome/*/*/" access="ROLE_USER" />
    <intercept-url pattern="/welcome/*/*/*" access="ROLE_USER" />
    <intercept-url pattern="/welcome/*/*/*/" access="ROLE_USER" />
    <intercept-url pattern="/welcome/*/*/*/*" access="ROLE_USER" />
    <intercept-url pattern="/resources/**" access="permitAll" />
    <form-login login-page="/login" default-target-url="/welcome" authentication-failure-url="/loginfailed" />
    <logout logout-success-url="/logout" />
</http>

在我的 login.jsp 中

为了放置我的图像,我使用这个:

<img src="<c:url value="/resources/img/a.jpg" alt="" />

我的结构是:

src/main/resources/images/a.jpg

我没有找到错误。谢谢!!!

【问题讨论】:

  • 我不确定您的图片是否在正确的位置。你的 WEB-INF 目录在哪里?
  • 我的WEB-INF目录是--> src/main/webapp/WEB-INF 我使用maven来创建我的项目。

标签: css image spring jsp spring-mvc


【解决方案1】:

您必须在 spring 配置中配置资源处理程序,以便它与您的项目结构相匹配

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources 
        mapping="/resources/** 
        location="/images/, classpath:/META-INF/web-resources/" />

您需要添加安全配置,以便无需登录即可访问此图像(在登录页面上显示它们)。

 <security:intercept-url pattern="/resources/**" access="permitAll" />

【讨论】:

  • 谢谢你,我已经配置了我的安全性,我已经编辑了帖子,我已经放置了 security.xml 和我的结构,因为我的项目已经不起作用了。
【解决方案2】:

我认为它从webapp 文件夹中获取resources

您能否将 resourcessrc/main/resources/ 更改为 src/main/webapp/resources/ 并尝试是否可行。

【讨论】:

    【解决方案3】:

    Usha 指出的问题部分正确。您需要将资源移动到 src/main/webapp/resources 或在 pom 中添加 src/main/resources 作为 webResource,这与常规资源不同。

    其次,您不会在 JSP 中引用 /resources/images/...,因为您的 Web 资源将被放置在 webapp 的根目录中,而不是在 /resources 目录中。

    最后,您将 DispatcherServlet 映射到根上下文 (/)。为了提供资源,您应该配置default-servlet-handler。您可能还想阅读该文档之前的部分,因为它讨论了服务资源。

    在 xml 配置中应该很简单

    <mvc:default-servlet-handler/>
    

    【讨论】:

    • 此时还有一个问题,项目的 i18n 不适用于新的更改 :(
    • 听起来像是一个完全独立的问题。也许你应该问另一个问题。
    猜你喜欢
    • 2013-06-20
    • 2011-08-28
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    相关资源
    最近更新 更多