简体   繁体   English

如何使用Struts 2提供静态资源(图像,CSS等)?

[英]How to serve static resources (images, css, etc) using Struts 2?

I'm new to Struts 2, and I'm simply looking for a way to serve static resources (eg images, javascript files, css files, etc). 我是Struts 2的新手,我只是在寻找一种提供静态资源(例如图像,javascript文件,css文件等)的方法。 Eg every request starting with /assets should try to load the static resource being referred to. 例如,以/assets开头的每个请求都应尝试加载所引用的静态资源。

1) Where do I need to store my images/js files, etc for this to work? 1)我需要在哪里存储我的图像/ js文件等才能工作?

2) And how do I need to configure Struts so that it will load those files? 2)我该如何配置Struts以便加载这些文件?

The project will be run as a .WAR on Tomcat 7, the static resources will be included as part of the WAR 该项目将在Tomcat 7上以.WAR的形式运行,静态资源将作为WAR的一部分包含在内

Usually you have your web server serve those directly from the file system. 通常,您的Web服务器直接从文件系统提供服务。 That is, Struts2 isn't doing it itself. 也就是说,Struts2本身并没有这样做。

If you supply which one you are using, maybe we can help configure it. 如果您提供使用的是哪个,也许我们可以帮助您配置它。

For example, if you are using HTTPD (Apache) web server in front of your Tomcat7, there will be a folder from which HTTPD serves up files. 例如,如果您在Tomcat7的前面使用HTTPD(Apache)Web服务器,则将有一个文件夹供HTTPD提供文件。 Perhaps /var/www/html is that location for some system. 也许/ var / www / html是某个系统的那个位置。 Then you would just put your static content in /var/www/html/assets. 然后,您只需将静态内容放入/ var / www / html / assets。

Edit 编辑

If the static resources are to be in the war file, you just build the war and put those files in the root of the war file or in some otherwise-unused folder structure beginning in the root of the war. 如果要将静态资源存储在war文件中,则只需构建war,然后将这些文件放在war文件的根目录中,或放在war根目录中的其他未使用的文件夹结构中即可。

For example, if you want to reference an image at http://mysite.com/assets/my.png then you put it my.png in the folder /assets in the root of the war file. 例如,如果要引用http://mysite.com/assets/my.png上的图像,则将其my.png放置在war文件根目录下的/ assets文件夹中。

From any HTML page in the site, you would refer to that image file this way: 在网站的任何HTML页面中,您都可以通过以下方式引用该图像文件:

<img src="/assets/m.png" .../>

This uses the default path on the same web site. 这将使用同一网站上的默认路径。

(later answer) Sorry but, because a Java server is a middleware, you should not delegate resources management to Apache. (稍后回答)抱歉,由于Java服务器是中间件,因此您不应将资源管理委托给Apache。 And you should not put your resources in the root of your WAR. 而且,您不应将资源放在WAR的根源中。 This is not a standard architecture. 这不是标准体系结构。 Typically, a Java web project should look like (I use Maven -because it's great-, so I indicate it too) : 通常,一个Java Web项目应该看起来像(我使用Maven-因为它很棒-所以我也指出了它):

src
    -- main
          -- java                <= you put your java sources here
          -- resources           <= you put your configuration or i18n properties files here
          -- webapp              <= here is the "web application" code. The front-end...
                   -- img        <= your images
                   -- js         <= your JS
                   -- css        <= your css
                   -- jsp        <= your JSP
   -- test
          -- etc...
pom.xml

All you need now is to configure your Struts2 and your web.xml (follow tutorial, very simples). 现在,您所需要做的就是配置Struts2和web.xml(遵循教程,非常简单)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为静态资源(例如CSS,Javascript,图像等)设置Jetty服务器中的到期标头 - How to set expires headers in jetty server for static resources such as CSS, Javascript,images etc 如何使用 webflux 提供 static 资源但支持缓存 - How to serve static resources using webflux but with CACHE support 如何使本地JBOSS AS 7提供静态资源? - How to make a local JBOSS AS 7 serve static resources? 使用Struts 2提供静态文件(JavaScript) - Serve static files (JavaScript) with Struts 2 无法使用 vertx-web 从类路径提供 static 资源 - Unable to serve static resources from classpath using vertx-web 使用Spring从压缩文件中提供静态资源 - Serve static resources from within a compressed file using Spring 从apache而不是tomcat提供静态文件(js / css / images) - serve static files(js/css/images) from apache instead of tomcat 我的 spring 安全代码未使用 static 资源,如 css、js 和图像文件夹 - my spring security code is not using the static resources like css,js and images folder 如何从Vaadin / Spring应用程序提供静态资源? - How to serve static resources from a Vaadin/Spring application? 如何在JSP中引用静态资源(如CSS,图像,Java脚本)? - How to give reference to static resources like css,images,java script in JSP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM