开发者

Spring 3 MVC and Tiles 2 - can't display static resources when controller @RequestMapping has URI template

I am using Tiles 2, Spring 3 MVC, Tomcat ver.7 and Eclipse (Springsource Tool Suite). I hope somebody can help.

The css and pictures are not rendered by the tile view that is returned by the controller handler method "displayPropertyPage" whose @RequestMapping has a URI template ( @RequestMapping(value = "/getproperty/{propertyID}", method = RequestMethod.GET) ).

I am using the mvc:resources and mvc:default-servlet-handler tags so the default servlet serves requests for static resources. I also checked the html script generated by this tile view and it does have the css entry.

The other views returned by controller handler methods with a simple path such as ( @RequestMapping(value = "/propertylistings", method = RequestMethod.GET) ) display all static resources including css, pictures and jquery just fine.

I noticed that the 'properties info' of the blank picture on the browser has a URL of http://localhost:8080/realtyguide/getproperty/resources/images-homes/pic1.jpg when it should be just http://localhost:8080/realtyguide/resources/images-homes/pic1.jpg. The URL is picking up the path "/getproperty" from the handler's RequestMapping annotation.

The pictures are under the folder 'images-homes'.

My directory structure is:

  • src
    • main
      • webapp
        • resources
          • images-homes
          • css
        • WEB-INF

Here is my controller. The view returned is a tile definition.

@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";
    }

}

Here is my Spring application servlet configuration:

<?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=开发者_Python百科"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>

Here is my 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>

I have been googling this for several days and can't find a solution.

Thanks for any help you can give.


It's the way you are referring to your resources in your views. If you refer to a resource in your view as:

resources/images-homes/pic1.jpg

it will be appended to the current controller URL. If you use:

/resources/images-homes/pic1.jpg

then it will refer to the web server root and not include your application context, assuming it is not running as root.

You need to change your resource links. I assume you are using JSP to render views. If that is the case then use c:url from the core JSTL library to provide the correct reference to your resource:

before

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

after

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜