如何在 WebLogic 上使用 Spring 的 mvc:resources 提供静态资源? [英] How to serve static resources using Spring's mvc:resources on WebLogic?

查看:29
本文介绍了如何在 WebLogic 上使用 Spring 的 mvc:resources 提供静态资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个 Web 应用程序,它在 tomcat 上运行良好,但由于静态资源加载问题而无法在最新的 weblogic 上运行 - 基本上我们从/static/** 提供所有资源,并在 spring servlet xml 中进行了设置像这样的文件:

We have a web app that works beautifully on tomcat, but fails to run on a latest weblogic cause of static resource loading issues - basically we serve all resources from /static/** and have set this up in the spring servlet xml file like this:

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

这适用于 tomcat,但在 weblogic 上您只会看到一个丑陋的页面,因为无法找到静态目录中的所有 CSS/JS/jpg.

This works on tomcat, but on weblogic you simply see an ugly page as all CSS/JS/jpgs within the static directoty cannot be found.

我也玩过这个:

<mvc:default-servlet-handler />

我在 spring 配置的末尾和开头放置了一次,但没有结果...

I placed it once at the end of the spring config and at the beginning, but no result...

如何为我们的静态资源提供服务?

How to serve our static resources?

推荐答案

对于我的 Spring MVC,我有以下平台,这对于 CSS 或 JS 等静态资源非常有效.

For my Spring MVC I have below platform and that works quite well for static resources like CSS or JS.

平台

  • Spring MVC 4.2.0
  • 休眠 4.2.20
  • Weblogic 10.3.6
  • 日蚀

  1. 在/WebContent 文件夹中创建 resources 文件夹 &在/WebContent/WEB-INF 之外.所以我的应用程序结构如下所示.
  1. create resources folder inside /WebContent folder & outside /WebContent/WEB-INF. So my Application structure would be like below.

  1. 在前端控制器配置 XML 文件中,即 dispatcher-config.xml 应如下所示.

  1. In the front controller config XML file i.e. dispatcher-config.xml should be as below.

<?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


In above config file notice below 2 tags are important for static content i.e css

<mvc:annotation-driven />
<mvc:default-servlet-handler/>  

  1. 我已将 CSS 文件包含在我的 JSP 中,如下所示.

  1. I have included the CSS file in my JSP as below.

<%@ 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

Thanks - Hitesh

这篇关于如何在 WebLogic 上使用 Spring 的 mvc:resources 提供静态资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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