我无法在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
我找不到错误。感谢!!!
答案 0 :(得分:0)
您必须在spring配置中配置一个ressource处理程序,以使其与您的项目结构匹配
<!-- 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" />
答案 1 :(得分:0)
我认为它从resources
文件夹中获取webapp
。
您可以将resources
从src/main/resources/
更改为src/main/webapp/resources/
并尝试是否有效。
答案 2 :(得分:0)
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/>