【问题标题】:Spring MVC seperate assets from applications war fileSpring MVC 从应用程序war文件中分离资产
【发布时间】:2016-08-14 23:08:36
【问题描述】:

我正在 tomcat apache 服务器上使用 java spring 和 jsp 编写一个名为“webdemo”的简单 Web 应用程序。我的目标是将资产(jsp,图像,css,js)与项目war文件分开并将它们存储在tomcat的类路径中,我将其设置为tomact的“common”文件夹。

webdemo-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>


    <import resource="externalizationContext.xml"/> 

    <mvc:annotation-driven/>

    <context:component-scan base-package="com.mckesson.voucher"/>

    <mvc:resources mapping="/images/**" location="classpath:application-assets/webdemo/images/" />
    <mvc:resources mapping="/scripts/**" location="classpath:application-assets/webdemo/scripts/" />

    <bean id="couponBean" class="com.mckesson.voucher.model.CouponBean" scope="session">
        <aop:scoped-proxy />
    </bean>



    <util:properties id="voucherProperties"
        location="classpath:application-config/webdemo/externalization/webdemo.properties" />
    <util:properties id="voucherEnvProperties"
        location="classpath:application-config/webdemo/logging/loggingTags.properties" />
    <util:properties id="voucherDBEnvProperties"
        location="classpath:application-config/datasource/dataSource.properties" />


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="classpath:application-assets/webdemo/" />
    <property name="suffix" value=".jsp" />
</bean>


</beans>

web.xml

   <!-- General description of your web application -->
      <display-name>McKesson webdemo</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dbContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>webdemo</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>      
    <servlet-mapping>
        <servlet-name>webdemo</servlet-name>
        <url-pattern>/home.html</url-pattern>
    </servlet-mapping>  
    <servlet-mapping>
        <servlet-name>webdemo</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

      <welcome-file-list>
        <welcome-file>home.html</welcome-file>
    </welcome-file-list>


      <!-- Define the default session timeout for your application,
            in minutes.  From a servlet or JSP page, you can modify
            the timeout for a particular session dynamically by using
            HttpSession.getMaxInactiveInterval(). -->
      <session-config>
            <session-timeout>30</session-timeout><!-- 30 minutes -->
      </session-config>
</web-app>

我们在 tomcat 上托管了近 300 个 web 项目,最终目标是将所有资产分别放在 tomcat 的公共文件夹下的每个 web 项目的单个位置。

我们将不胜感激。

【问题讨论】:

    标签: java spring jsp tomcat servlets


    【解决方案1】:

    这实际上是一个好主意,但你做错了。您需要了解的一件事是您不想分离 jsp。把它留在春天。您想将 js、css 和图像分开,因为它们是静态内容。在企业环境中执行此操作的常用方法是在 Tomcat 服务器前面安装一个 Web 服务器,例如 Apache。您将静态内容放在 Apache 服务器上并配置该服务器以将静态内容传送到浏览器,同时将动态内容的调用发送到 Tomcat 并通过 Apache 服务器进行中继。这减少了 Tomcat 服务器上的负载。您也许可以在一台服务器上完成所有这些操作,但我还没有尝试过。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 2018-11-03
      相关资源
      最近更新 更多