【问题标题】:jsp access spring mvc resourcesjsp访问spring mvc资源
【发布时间】:2012-06-21 20:26:57
【问题描述】:

我花了最后几个小时试图弄清楚如何从 Spring mvc Web 应用程序中的 jsp 访问静态资源。

这是我的 beans 配置文件中的一部分:

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

现在在第一个 jsp 页面中,我位于 /css/style.css 的 css 文件可以像这样正常工作:

<link rel="stylesheet" href="css/style.css" type="text/css" />

但在第二个 jsp 页面中,我无法访问位于 /images/logo.gif 的此图像:

<img src="images/logo.gif" alt="anotherimage" />

有人能解释一下&lt;mvc:ressources /&gt; 是如何与 jsp 一起工作的吗?

编辑: 这是我的上下文

<?xml version="1.0" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

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


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"    value="com.mysql.jdbc.Driver" />
        <property name="url"                value="jdbc:mysql://localhost/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="mmapp.domain" />
        <property name="hibernateProperties">
            <value>
                hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
            </value>
        </property>
    </bean>

    <bean id="transactionManager"     class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="jdbcTemplate"     class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
        <constructor-arg ref="dataSource" />
    </bean>

    <bean id="simpledata" class="mmapp.util.SimpleDataProvider" />

    <tx:annotation-driven transaction-manager="transactionManager" />
    <context:annotation-config />
    <context:component-scan base-package="mmapp" />

    <mvc:default-servlet-handler/>
    <mvc:resources mapping="/images/**" location="/images/" /> 
</beans>

还有我的 web.xml

<?xml version="1.0" ?>

<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:shemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

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

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

</web-app>

【问题讨论】:

  • 根据我的阅读,不应该是 /images/logo.gif 吗?也许 css 目录是相对于当前目录的,并且允许以这种方式加载该文件。跨度>
  • 我试过了,还是不行。

标签: jsp spring-mvc resources


【解决方案1】:

访问资源最安全的方法是使用 jstl 核心库。至少,您可以确保在任何上下文中都生成了正确的 url。将以下代码行添加到您的 jsp 文件中:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

并将 img 标签更改为使用 c:url 标签:

<img src="<c:url val="/images/logo.gif" />" alt="anotherimage" />

确保在进行此更改后您的 src 属性看起来不错。您还必须将 jstl 库添加到您的项目中(这可能是一个好主意,因为它非常标准)。据我所知,您的 mvc:resources 映射看起来不错。

【讨论】:

    【解决方案2】:

    之后

    <mvc:default-servlet-handler/>
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    

    获取您的 css/图像:

    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css" type="text/css" />
    <img src="${pageContext.request.contextPath}/images/logo.gif" alt="anotherimage" />
    

    【讨论】:

    • 它给了我一个 404 错误请求的资源 () 不可用。
    • WebContent/images/logo.gif下真的有图片logo.gif吗?
    • 我可以像这样使用浏览器访问它http://localhost:8080/mmapp/images/logo.gif
    • 经过几次尝试后,这里是我所拥有的,404 错误尝试上面写的内容时,请求的资源()不可用。在评论&lt;mvc:resources 部分时,我显示了我的观点,当然没有图像..
    • 查看我的更新回复,我添加了 。如果仍然无法正常工作,请粘贴您的网络上下文
    【解决方案3】:

    我不知道这是否有意义,但这是我解决此问题的方法:

    首先我的控制器看起来像:

    @Controller
    @RequestMapping(value="/reports")
    public class ReportsController {
    
        @RequestMapping(value = "/reporting", method = RequestMethod.GET)
        public String onSubmit() {
            return "home" ;
        }
    }
    

    使用上面的所有配置列表,我只需将 home.jsp 中的 img 标签从 &lt;img src="images/logo.gif" /&gt; 更改为 &lt;img src="../images/logo.gif" /&gt;,如果我从我的第二个 @RequestMapping 中删除 value = "/reporting",那么一切正常&lt;img src="images/logo.gif" /&gt; 哪个有效!

    【讨论】:

      猜你喜欢
      • 2012-11-02
      • 1970-01-01
      • 2011-12-23
      • 2011-07-08
      • 2015-05-25
      • 1970-01-01
      • 2014-11-20
      • 2013-08-02
      • 1970-01-01
      相关资源
      最近更新 更多