Spring Boot static content url mapping - java

Is anybody here who know how to create a mapping file from static directory to respond for certain url in Spring Boot?
For example, I have file in directory /resource in Spring Boot structure
resources/static/html/index.html
and I want it to respond to url
/index
No catalogue path, no .html postfix

File index.html is served as default HTML file from certain directory. So if you want to serve it from http://domain:port/index URL, you should place it into resources/static/index/index.html.

As I research more, url mapping is possible only if You use template engine like Thymeleaf and then application will became context aware.

Use Thymeleaf and then add your .html files to resources/templates/ and they will be discovered automatically.

Related

Using HTML,JS and CSS in spring mvc application

I dont want to use JSP and instead use HTML,CSS and JAVASCRPIT .
I dont't want to use any front end technology like AngularJSbut only only AJAX for communicating with backend. I have only one page as index.html and few .js and .css files. But I am not able to configure spring mvc to use these html and other js and css files.
Can somebody help me with the project structure and the configurations for it.
Thanks.

Spring Boot - Proper location for html files

I am new to Spring Boot and I am trying to add a simple html page for my project
At the moment, my project's structure is the following:
Having read the following:
https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content
and other answers in StackOverflow, I'd expect to see the contents of index.html when I visit http://localhost:8080/
What could I be missing?
As there is no webapp in SpringBoot project , we can place the html files in either in
src/main/resources/resources/index.html
src/main/resources/static/index.html
src/main/resources/public/index.html
Remember the above is in highest to lowest precedence.
To check your file run the main class in eclipse and go to http://localhost:8080/index.html
First of all the correct way to serve files in Spring Boot is described in
cannot find html pages with spring boot
After adding the directories required, I tried /services/login.html where "services" is the mapping for my Dispatcher servlet.
So what happens here is that Spring Boot will only answer to requests sent to /services/* even if these requests are pointing to something other than a controller.
I guess that this makes sense in the world of Spring Boot but when I tried in the same in a Spring MVC project (no Spring Boot) I was able to access simple html files that I had simply added into my WEB-INF directory

Struts2 or spring open file in WEB-inf

I am trying to do first web app with Struts2. How to write data to text file, where it could be placed? I'm using Struts2 with Spring (Tomcat7 and eclipse).
You can write data to a text file anywhere on your file system as long as you have its full path and write privileges, regardless of Struts2 or Spring.

include css inside a spring mvc java project

How is this achieved, I have everything mapped from / to the dispatcher Servlet and the css is in the webapp folder. when clicking on the css link href in the source I get a tomcat error.
Do I need to create a #RequestMapping for css?
Css are served without the need of dispatcher (unless you configure it to serve css). Just put them in some folder in your webapp dir and reference them as /YourAppContext/path-to-your-css in your pages.
For example in typical (non-maven) webapp it's inside WebContent/css
Assuming your app's context name is MyApp and you want to reference WebContent/css/main.css
you should put something like this in jsp:
<c:url value="/css/main.css" />
or uglier using
/MyApp/css/main.css
as your css url
CSS are client files, not server files. All client files (CSS, Javascript, etc.) have nothing to do with Spring MVC, which acts on servlets (on the server). SO you add your CSS exactly the same way you would if you weren't using Spring MVC.

Remap image requests in a web application (tomcat) with spring for an absolute path

i'm trying to find a way remap a path to an absolute one in order to retrieve images stored in the server filesystem, i'm also using spring mvc but the <mvc:resource> can't map absolute paths, and i can't find a nice way to do this with spring controllers
i.e. I need to map all the image requests from /images/** to /root/var/img/**, in this way when a client browser try to load an image of the page, every images stays in the path above mentioned
You can create a new context with docBase referencing to your folder (/root/var/img) It should look like this:
<Context path=”/images” docBase=”/root/var/img/” ... >
</Context>
Refer to Tomcat context configuration documentation for more details (e.g., for Tomcat 6: Apache Tomcat Configuration Reference: The Context Container).
Introduction to this document lists possible places where context elements can be defined.
EDIT
As I mentioned in comments, Spring-specific way to do this without creating any new context seems to be using RedirectView (link to Spring v2.5 JavaDoc). According to JavaDoc:
"View that redirects to an absolute, context relative, or current
request relative URL, by default exposing all model attributes as HTTP
query parameters."
EDIT2
It seems I've misunderstood RedirectView purpose, which is a good old URL redirection. Thus, answer by #Jens should be more appropriate (+1). I still wonder if there's an existing standard solution in Spring (what I originally thought RedirectView would do..) It's not like there's much code to write, but still :)
EDIT3
After reading more on this topic, I found out that <mvc:resources> is able to do this (i.e., mapping resource using not only relative path, or classpath), you just need to correctly configure location using file: prefix . In fact, there's already answer on SO that explains exactly what you need to do: Spring : serving static resources outside context root
not sure if I am getting that right but I think the common way to do that is to set up a controller which is mapped on the requests to /images/:
#RequestMapping(value ="/images/")
public void fetchImage(#RequestParam String id, HttpServletResponse response)
Then the controller can load the requested image from a configured directory like "/root/var/img" and write it the the OutputStream of the response. E.g:
org.apache.commons.io.IOUtils.copy(fileInputStram, response.getOutputStream())
In addition you have to set the correct mime type etc. in the response.
Jens

Categories

Resources