我正在 tomcat apache 服务器上使用 java spring 和 jsp 编写一个名为“webdemo”的简单 Web 应用程序。我的目标是将 Assets (jsp、图像、css、js)与项目 war 文件分开,并将它们存储在 tomcat 的类路径中,我将其设置为 tomact 的“公共(public)”文件夹。
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>
网络.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 项目,最终目标是将所有 Assets 分别放在 tomcat 公共(public)文件夹下的每个 Web 项目的单个位置。
如有任何帮助,我们将不胜感激。
最佳答案
这实际上是个好主意,但您做错了。您需要了解的一件事是您不想分离 jsp。把它留在 Spring 。你想把js、css、images分开,因为那些是静态内容。在企业环境中执行此操作的常见方法是在您的 Tomcat 服务器前面安装一个 Web 服务器,例如 Apache。您将静态内容放在 Apache 服务器上并配置该服务器以将静态内容传送到浏览器,而对动态内容的调用将发送到 Tomcat 并通过 Apache 服务器中继。这减少了 Tomcat 服务器上的负载。您也许可以在一台服务器上完成所有这些操作,但我还没有尝试过。
关于java - Spring MVC 从应用程序 war 文件中分离 Assets ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38947414/