jsp访问spring mvc资源 [英] jsp access spring mvc resources

查看:61
本文介绍了jsp访问spring mvc资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have spent the last hours trying to figure out how to access static ressources from a jsp in Spring mvc web application.

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

Here is a part from my beans config file:

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

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

now in the first jsp page my css file located at /css/style.css works fine like this:

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

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

but in the second jsp page i can't access this image located at /images/logo.gif:

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

有人可以解释<mvc:ressources />如何与jsp一起使用吗?

can someone explain how the <mvc:ressources /> works with 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

AND my 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>

推荐答案

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

The safest way to access resources is to use the jstl core library. At the very least, you can be sure that the correct url is generated in any context. Add the following line of code to your jsp file:

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

并更改img标签以使用c:url标签:

and change the img tag to use the c:url tag:

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

在进行此更改后,请确保您的src属性看起来不错.您还必须将jstl库添加到您的项目中(这很不错,因为它很标准).据我所知,您的mvc:resources映射看起来不错.

Make sure your src attribute looks good after you make this change. You'll also have to add the jstl library to your project (probably a good idea since it's pretty standard). Your mvc:resources mapping looks good as far as I can tell.

这篇关于jsp访问spring mvc资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆