【问题标题】:Spring 3 MVC and Tiles 2 - can't display static resources when controller @RequestMapping has URI templateSpring 3 MVC 和 Tiles 2 - 当控制器 @RequestMapping 具有 URI 模板时无法显示静态资源
【发布时间】:2011-12-03 08:29:12
【问题描述】:

我正在使用 Tiles 2、Spring 3 MVC、Tomcat ver.7 和 Eclipse(Springsource 工具套件)。我希望有人能帮忙。

css和图片不是由控制器处理方法“displayPropertyPage”返回的平铺视图呈现,其@RequestMapping有一个URI模板(@RequestMapping(value = "/getproperty/{propertyID}",方法 = RequestMethod.GET))。

我正在使用 mvc:resourcesmvc:default-servlet-handler 标签,因此默认 servlet 为静态资源请求提供服务。我还检查了这个平铺视图生成的 html 脚本,它确实有 css 条目。

控制器处理程序方法返回的其他视图具有简单的路径,例如 (@RequestMapping(value = "/propertylistings", method = RequestMethod.GET)) 显示所有静态资源,包括 css、图片和jquery就好了。

我注意到浏览器上空白图片的“属性信息”的 URL 为 http://localhost:8080/realtyguide/getproperty/resources/images-homes/ pic1.jpg 应该是什么时候 只是 http://localhost:8080/realtyguide/resources/images-homes/pic1.jpg。该 URL 正在从处理程序的 RequestMapping 注释中获取路径“/getproperty”。

图片位于“images-homes”文件夹下。

我的目录结构是:

    • 主要
      • 网络应用
        • 资源
          • 图片之家
          • CSS
        • WEB-INF

这是我的控制器。返回的视图是一个 tile 定义。

@Controller
public class PropertyPageController {

    private MasterTableService masterTableService;

    @Autowired
    public PropertyPageController(MasterTableService masterTableService) {
        this.masterTableService = masterTableService;
    }

    @RequestMapping(value = "/getproperty/{propertyID}", method = RequestMethod.GET)
    public String displayPropertyPage(@PathVariable("propertyID") String propertyID, Model model) {

        model.addAttribute("mastertable", masterTableService.findByID(propertyID));

        return "propertyinfo.tiledef";
    }

}

这是我的 Spring 应用程序 servlet 配置:

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <context:annotation-config />

    <!-- Scans within the base package of the application for @Components to configure as beans -->
    <context:component-scan base-package="com.springproject.realtyguide" />

    <!-- Enables the Spring MVC @Controller programming model -->    
    <mvc:annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
    <mvc:resources mapping="/resources/**" location="/resources/"/>   

    <!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource requests to the container's default Servlet -->
    <mvc:default-servlet-handler/>      


    <!-- Bean to provide Internationalization  -->
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="WEB-INF/i18n/messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="classpath:META-INF/spring/database.properties" />

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
        p:password="${jdbc.password}" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:META-INF/hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <!-- Enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven />

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

    <!-- __________ BEAN ENTRIES FOR TILES 2 -->

    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/layouts/tiles.xml</value>
            </list>
        </property>
    </bean>

    <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver" >
        <property name="order" value="0"/> 
        <property name="viewClass"> 
            <value>org.springframework.web.servlet.view.tiles2.TilesView </value>
        </property>
        <property name="requestContextAttribute" value="requestContext"/>
        <property name="viewNames" value="*.tiledef"/>
    </bean>

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

    <!-- __________ END OF BEAN ENTRIES FOR TILES 2 -->

    <!-- Resolves localized <theme_name>.properties files in the classpath to allow for theme support -->
    <bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
        <property name="basenamePrefix" value="theme-" />
    </bean>

    <bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">  
        <property name="defaultThemeName" value="standard" />
    </bean>

</beans>

这是我的 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>Realty Guide</display-name>

      <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
      </context-param>

      <!-- Creates the Spring Container shared by all Servlets and Filters -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

      <!-- Handles Spring requests -->
      <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>

</web-app>

我已经在谷歌上搜索了几天,但找不到解决方案。

感谢您提供的任何帮助。

【问题讨论】:

    标签: spring model-view-controller tiles


    【解决方案1】:

    这是您在视图中引用资源的方式。如果您在视图中将资源称为:

    resources/images-homes/pic1.jpg
    

    它将附加到当前控制器的 URL。如果你使用:

    /resources/images-homes/pic1.jpg
    

    然后它将引用 Web 服务器根目录,而不包括您的应用程序上下文,假设它不是以 root 身份运行的。

    您需要更改资源链接。我假设您正在使用 JSP 来呈现视图。如果是这种情况,请使用核心 JSTL 库中的 c:url 来提供对资源的正确引用:

    之前

    <img src="resources/images-homes/pic1.jpg"/>
    

    之后

    <img src="<c:url value='/resources/images-homes/pic1.jpg'/>"/>
    

    【讨论】:

    • 谢谢 skanjo。您对使用 c:url 为图片的建议非常有效!只需要你多提一个建议。我的jsp文件中的css是这样的:href="" type="text/css" />。如何使用 spring:theme 在 href 上应用 c:url?我真的很想在这个应用上保留主题。
    • 我明白了。我只是使用 c:set 和 c:url 作为主题。 &lt;c:set var="cssUrl"&gt;&lt;spring:theme code='styleSheet'/&gt;&lt;/c:set&gt; &lt;link rel="stylesheet" type="text/css" href="&lt;c:url value='${cssUrl}'/&gt;" /&gt;
    猜你喜欢
    • 2016-04-13
    • 2020-10-14
    • 2013-02-07
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多