Why should I declare <mvc:resources> in my configuration file - spring-mvc

I'm having hard time understanding the purpose of <mvc:resources mapping="..." location=".."/>section in my configuration file.
my project structure...
--WebContent
-----META-INF
-----static
-----------images
------------js
------------css
-----WEB-INF
--------jsps
--------spring-configs
--------web.xml
I'm able to access files under images, js and css folders even when I don't specify this section in the spring servlet config file. So why do I need to specify this? What purpose does it serve exactly?
For the record, this is what I have in my config file.
<mvc:resources mapping="/static/**" location="/static/" />

You don't need it if that's where you put your resources. Everything at the root of your web application (WebContent), which isn't WEB-INF or META-INF is publicly available and the Servlet container can serve it to any client.
The <mvc:resources ... /> element is meant to serve resources which are within WEB-INF which is not available directly to clients. They must be served by your application.

Adding to what #Sotirios has answered, <mvc:resources/> can resolve static resources from Classpath resources ( for e.g., a jar file). This opens up the possibility of packaging your static resources in a self-contained jar module along with your business logic (although very few people use this approach in command based framework like Spring - this is more prevalent in Component based frameworks like JSF).
Apart from this there are other benefits of using this tag as mentioned here. I'm quoting
The cache-period property may be used to set far future expiration
headers (1 year is the recommendation of optimization tools such as
Page Speed and YSlow) so that they will be more efficiently utilized
by the client. The handler also properly evaluates the Last-Modified
header (if present) so that a 304 status code will be returned as
appropriate, avoiding unnecessary overhead for resources that are
already cached by the client

Related

Spring boot integration tests with additional weba application resource path

I have an integration test (annotated with #WebAppConfiguration) that needs some additional javascript/html files to be served by the application. I'd like to put those files under src/test/webapp but the content of that directory is not loaded.
I've seen that the WebAppConfiguration annotation allow to override the default src/main/webapp path, but I don't need to completely replace that path.
Is there a way to get what I need?
By default, Spring Boot will serve static files from a folder called /static (or /public or /resources or /META-INF/resources) on the classpath. Placing your additional JavaScript and HTML files in src/test/resources/static should make them available during your integration tests.

How to use Spring MVC resources static images in tapestry

I am developing an application using Tapestry 5.3.1
I have tried to use MVC resources mapping to cache images, But it doesn't works!
Here my coding:
Context xml configuration:
<mvc:resources mapping="/images/**" location="file:${catalina.home}/props/" cache-period="1234" />
<mvc:annotation-driven />
Index.tml:
<img src="/images/home.jpg" width="130" />
I have this home.jpg file in tomcat catalina home path props folder.
Application runs successfully, however the images are not loaded!!
Can anyone help me?
Actually, your best bet is the following:
<img src="${context:images/home.jpg}"/>
This is a standard <img> tag that has a dynamic attribute.
"context:" is a binding prefix; it decides how the remainder of the string in interpreted. Here, a path to a file underneath the context root.
What you'll see in the browser for the src attribute will vary depending on the version of Tapestry; it will be a URL that is routed through Tapestry and gets a far-future expires header (5.3) and an ETag (5.4). The URL will include a application-wide version number (5.3) or a checksum based on the file content (5.4).
Also, there are hooks in Tapestry to convert the URL into a reference to a CDN (Content Delivery Network).
In other words, we're telling the browser that it should cache the value and not ask for it again. That's very important for scalability and performance.
I don't know what the mvc:/Spring stuff is supposed to do, but it will likely not be as functional as Tapestry.
Try this :
<img src="${pageContext.request.contextPath}/images/home.jpg" width="130" />

Moving resources under WEB-INF

I have a web application that contains hundreds of HTML, JavaScript and image files. These files are located under the root directory:
my_root--
-- html
-- js
-- images
These folders contain some subfolders.
From a security reason I need to move all these resources under the WEB-INF folder so they will not be directly accessible.
Currently JSP and servlet files are already under the WEB-INF folder.
What is the easiest method for me to safely move all HTML/JavaScript/images folders under the WEB-INF without breaking all links/forwarding to resources in these folders and make sure these resources are not directly accessible?
I am using WebSphere and WebLogic servers.
What is the easiest method for me to safely move all html/js/images folders under the WEB-INF without breaking all links/forwarding to resources in these folders and make sure these resources are not directly accessible?
You're making a thiniking mistake here. HTML/JS/image (and CSS) resources need to be directly accessible anyway. For JSPs the story is different, some of them, if not all, need to be preprocessed by a servlet (e.g. to retrieve some list from DB for display in a table). If those JSPs were been accessed directly, then that servlet step would be skipped altogether, which is absolutely not what you want (the JSPs end up "empty"; without any data from the DB). That's why they should be hidden in /WEB-INF to prevent direct access without going through a preprocessing servlet first. Also, in case of servlet based MVC frameworks, this way the whole MVC framework process (collecting request parameters, converting/validating them, updating model values, invoking actions, etc) would be skipped.
Your concrete functional requirement is not exactly clear (the whole question makes at its own no sense; the answer is just "don't do that"), but if you actually want to restrict access to static resources which don't need to be preprocessed by a servlet at all to certain users only, then you need to implement an authentication/login system. You can utilize container managed authentication or homegrow a Filter for this.
You can go with a very simple tool like notepad++ and use the findAndReplace feature. Eclipse can also do this but it gets tricky to effectively find every reference.
Note that there are other ways to stop users from accessing your images. It is probably easier to just leave things where they are and instruct the websphere to stop serving these images from the images folder

Manage static Resources in spring mvc

I am building an application with Spring MVC and jquery for UI. The UI developer is designing the pages using jQuery without using any server (as it is not required). The pages are integrated by developers in the application.
The issue here is that the UI designer is using relative directory path and to integrate these pages, paths need to be prefixed with spring resource path. Even the JS and CSS files are referring to images with relative directory path. Every time the UI is update same process is repeated for integration.
What I want to know is there any better approach in this case so that
the relative path used by ui developer doesn't needs to be changed every time for integration.
The spring static resource loading can still be used. <mvc:resources mapping="/res/**" location="/resources/" />
The path reference in any js or css file can also work without any changes in it.
I do not want to do anything which tightly couple it with a server. Please help.
lookup util:constant in the spring documentation and see if that is helpful.

ASP.NET: change link to static asset dynamically

We are building a internal static asset server. Each environment (dev, staging, prod) has its own asset server, and the asset is reference throughout the web application (html, aspx, ascx, css, javascript, etc...)
To reference the correct asset server in the correct environment, one solution is to write a http module to intercept response before it gets to the client and change the URL according. I am just thinking that this might not be the most scalable solution since this http module is going to get executed for every request and basically parse the whole response (some are huge) before the client gets it.
I am also thinking to use a client side javascript to change the reference on client side, but this might not work as nicely has a http module.
Any thoughts? What's the industries best practice in ASP.NET?
I might create my own LinkToStaticAsset control. It would only accept the path relative to the static asset server of each asset. I would have it generate the full URL by including the base path from configuration.
If you want your site to function (even if only partially) without a Javascript dependency then you should keep this server-side.
An alternative approach to what you have mentioned above is to do this at the application level, i.e. have a library method which generates your static asset URL's and is configured to point at particular server(s) via your web.config.
It would go something like this:-
(In your App_Code folder or a referenced assembly)
public static class Util
{
public static string AssetUrl(string relativePath)
{
// returns asset server address from web config with relative path appended
}
}
(In web.config)
<appSettings>
<add key="AssetServerBaseUrl" value="http://foo.bar" />
...
</appSettings>
(In your aspx file)
<img src='<%= Util.AssetUrl("img/myimage.jpg") %>' ... />

Resources