How to use Spring MVC resources static images in tapestry - spring-mvc

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" />

Related

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

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

Displaying an image from Local Network to JSF Web Application

I've some image file in my Local Network and I want to display them in the DataTable in JSF. I'm using JSF2.0 and Tomahawk 1.1.13. Below is the JSF code.
<t:dataTable newspaperColumns="3" value="#{startupBean.colorList}" newspaperOrientation="horizontal" var="colorBO">
<f:facet name="spacer">
<f:verbatim></f:verbatim>
</f:facet>
<h:column>
<h:graphicImage id="colors" alt="jsf-sun" url="#{colorBO.color_url}">
</h:graphicImage>
</h:column>
</t:dataTable>
I'm giving the path of the file as "\\root\sub\sub\xxx.jpg" in my backing bean. But when the JSF page renders, it displays a default icon. When I right click on the icon and check the properties, the below is what I get.
http://localhost:8080/projname//root/sub/sub/xxx.jpg
I tries using both backslash and forward slash in the path name. No change in the output though. I dont' know how the http part gets into the file path. I'm missing something for sure.
You're making a conceptual mistake here. It's the webbrowser who has to download the image separately by a valid and reachable URL once it encounters a <img> element, it's not the webserver who has to somehow magically embed the image from the local disk file system within the HTML page which is returned to the webbrowser.
You need to make the image available by a valid and reachable URL, so that the webbrowser can download it. A local disk file system path is surely not a valid and reachable URL. Even more, it would be a huge security hole if a server machine's local disk file system contents was automagically fully exposed into the world wide web.
One of the simplest ways of solving this problem is just placing the image in the public webcontent (there where your JSF files also are), so that it's available in the web.
Another way is to configure the server to publish the folder into the web so that it's available by a specific URL as well. It's unclear which server you're using, so here's just a Tomcat example:
<Context docBase="/root/sub/sub" path="/images" />
This way the image is available by http://localhost:8080/images/xxx.jpg and then this <h:graphicImage> approach should work:
<h:graphicImage src="/images/xxx.jpg" />
See also:
Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag
Simplest way to serve static data from outside the application server in a Java web application

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.

How does ASP.NET and IIS transform an <img src="localresource.jpg"> tag turn into an image file in the browser?

How does ASP.NET and IIS transform an <img src="localresource.jpg"> tag turn into an image file in the browser?
I am assuming that it locates the file based on the path and renders a FileStream. I don't see any handlers registered in web.config and my IIS6 configuration doesn't have anything about .jpg, .gif, .png, etc. extensions.
Edit:
Also, how can this process be customized?
The browser sends a request for /path/to/page/localresource.jpg.
IIS finds the file, looks up the MIME type for .jpg in its default MIME-type mapping, and serves the file.
Neither ASP.Net nor the .Net framework are involved. (unless you set them to be)

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