2011-11-07 171 views
0

我們有一個在tomcat上運行得很好的web應用程序,但是無法運行靜態資源加載問題的最新weblogic原因 - 基本上我們提供了來自/ static/**的所有資源,並且已經在spring servlet xml文件像這樣:如何使用Spring的mvc提供靜態資源:WebLogic上的資源?

<mvc:resources mapping="/static/**" location="/static/" /> 

這tomcat的工作,但在WebLogic上,你只看到一個醜陋的頁面,靜態directoty內的所有CSS/JS/JPG文件無法找到。

我打了這一點,太:

<mvc:default-servlet-handler /> 

我把它一次在Spring配置的結束和開始,但沒有結果......

如何爲我們的靜態資源?

+0

視圖源是否顯示正確的URL映射到實際的css/jpgs? – JoseK

回答

0

一開始我會檢查2件事情。首先,因爲我不是Spring用戶,所以我不確定spring使用什麼方法來查找和加載文件,如果它使用getRealPath,則可以嘗試在weblogic中啓用此方法。

在管理控制檯中,單擊域名,Web應用程序和向下翻頁(最後一個選項),您會發現一個複選框,即使您的應用程序打包在.war或.ear文件中,也可以使getRealPath正常工作。您需要重新啓動域的服務器才能使此配置生效。

如果這不能解決您的問題,您可以嘗試使用weblogic.xml文件映射靜態文件。

你將不得不使用像weblogic的標籤:

<wls:virtual-directory-mapping> 
<wls:local-path>/var/docs/weblogic</wls:local-path> 
<wls:url-pattern>/static/*</wls:url-pattern> 
</wls:virtual-directory-mapping> 

在上面的例子中,你將不得不在fisic盤路的/ var /文檔/ weblogic的一個文件,假設它被命名爲myfile.css被加載正確的完整url:http://seudominio.com/static/myfile.css或使用相對路徑/static/myfile.css

我相信其中一種方法可以幫助你。

關於。

0

對於我的Spring MVC,我有下面的平臺,對於像CSS或JS這樣的靜態資源來說工作得很好。

平臺

  • Spring MVC的4.2.0
  • 休眠4.2.20
  • 的Weblogic 10.3.6
  • 的Eclipse

    1. 中創建資源文件夾/ WebConten t文件夾& outside/WebContent/WEB-INF。所以我的應用程序結構如下所示。

My Spring MVC Application Structure

  • 在前端控制器配置XML文件即調度-config.xml中應該是如下。

    <?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation=" 
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> 
    
    <!-- default page to show when app starts --> 
    <mvc:view-controller path="/" view-name="Home"/> 
    
    <!-- essentially sets you your Spring context to allow for dispatching requests to Controllers --> 
    <mvc:annotation-driven /> 
    
    <!-- used to load static resources like css, js etc... --> 
    <mvc:default-servlet-handler/> 
    
    <!-- automatically wire values into properties, methods, and constructors. --> 
    <context:annotation-config/> 
    
    <!-- scan for components like @Controller, @Repository, @Service, @Component etc...--> 
    <context:component-scan base-package="au.com.snh" /> 
    
    <!-- spring view resolver bean --> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/views/" /> 
        <property name="suffix" value=".jsp" /> 
    </bean> 
    
    <!-- load database properties file --> 
    <context:property-placeholder location="classpath:database.properties"/> 
    
    <!-- declare beans --> 
    <bean id="regionDao" class="au.com.snh.dao.RegionDaoImpl" /> 
    <bean id="regionService" class="au.com.snh.service.RegionServiceImpl" /> 
    
    <!-- declare datasource bean --> 
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="${db.driver}" /> 
        <property name="url" value="${db.url}" /> 
        <property name="username" value="${db.user}" /> 
        <property name="password" value="${db.pwd}" /> 
    </bean> 
    
    <!-- hibernate --> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource" /> 
        <property name="packagesToScan" value="au.com.snh.model" /> 
        <property name="hibernateProperties"> 
         <props> 
          <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
          <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
          <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
          <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
         </props> 
        </property> 
    </bean> 
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
        <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
    
    <tx:annotation-driven transaction-manager="transactionManager"/> 
    
    <!-- resource bundles --> 
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
        <property name="basename" value="/WEB-INF/propertybundle/common"/>  
    </bean> 
    


  • 在上面的配置文件通知低於2個標記是靜態內容即CSS

    <mvc:annotation-driven /> 
    <mvc:default-servlet-handler/> 
    
  • 我重要已經將CSS文件包含在我的JSP中,如下所示。

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <html> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
        <title>Welcome to the World of Spring MVC</title> 
        <link rel="stylesheet" href="/${initParam.appRootPath}/resources/css/main.css"> 
    </head> 
    <body> 
        <center> 
         <h1>Welcome to the World of Spring MVC 4.2 & Hibernate 4.2 </h1> 
        </center> 
    </body> 
    </html> 
    
  • 僅供參考,我也曾在這裏包括我的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>SpringMVCHibernateProject</display-name> 
    
         <!-- global variables --> 
         <context-param> 
         <param-name>appRootPath</param-name> 
         <param-value>SpringMVCHibernateProject</param-value> 
         </context-param> 
    
         <!-- front controller --> 
         <servlet> 
         <servlet-name>spring-dispatcher</servlet-name> 
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
         <init-param> 
          <param-name>contextConfigLocation</param-name> 
          <param-value>/WEB-INF/dispatcher-config.xml</param-value> 
         </init-param> 
         <load-on-startup>1</load-on-startup> 
         </servlet> 
    
         <servlet-mapping> 
         <servlet-name>spring-dispatcher</servlet-name> 
         <url-pattern>/</url-pattern> 
         </servlet-mapping> 
    
    
        </web-app> 
    

    希望我的職務將是有益的。

    謝謝 - Hitesh