File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Spring MVC and access to static html files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring MVC and access to static html files" Watch "Spring MVC and access to static html files" New topic
Author

Spring MVC and access to static html files

Tom Nilabar
Greenhorn

Joined: Nov 27, 2012
Posts: 7
Hello.
I want to create website based on spring mvc, so my question is: where to locate static html files in webapp structure and what annotations should be placed in servlet-context.xml in order to access static html files without controller evolvement? How can I do this?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"what annotations should be placed in servlet-context.xml"

None. Annotations don't go in xml.

But in xml you can use the mvc namespace and they have a tag <mvc:controller> which takes a URL path and a static file as attributes, so that you don't have to write a Controller class. It was built with this model in mind. It is what I do in my web application. I have no jsp pages rendered on the server. Just html, css and javascript files because the requests are all REST calls from the browser pages to my backend Controllers.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Tom Nilabar
Greenhorn

Joined: Nov 27, 2012
Posts: 7
Thanks for reply.
But I didn't find <mvc:controller> tag. Did you mean <mvc:view-controller>? Also, I used this tag, mapped url to specific view and now when I'm trying to access an address for my static page like "localhost/appname/hello/" I'm getting an 404 error.
Here is my servlet-context.xml contents:
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:view-controller path="/hello" view-name="static"/>

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
</bean>
In <mvc:view-controller> "path" attribute I typed URL and in "view-name" attribute I typed name of file, so I thought that it would be work. How can I fix it?
2. And second question. I want my html files to be located outside of WEB-INF folder, because I want to keep adding html files (contents of website). How can I manage this?
Tom Nilabar
Greenhorn

Joined: Nov 27, 2012
Posts: 7
In addition to previous post. My main goal is to build website based on Spring MVC. So I want to have jsp-files, such as submission forms or pages with a dynamic content along with static html pages. Static html pages are needed for content of site such as news, articles etc.
Is it possible to control jsp-pages, which are located in WEB-INF\views for example, with controllers and access static html pages (also, css, javascript, images) without controller invocation.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Yes, here's what i have in my application

<mvc:view-controller path="/" view-name="index"/>

So that will go to my index.jsp file in my WEB-INF/views directory, because of the InternalViewResolver prepending "WEB-INF/views/" and appending ".jsp"

Mark
Tom Nilabar
Greenhorn

Joined: Nov 27, 2012
Posts: 7
Mark Spritzler wrote:Yes, here's what i have in my application

<mvc:view-controller path="/" view-name="index"/>

So that will go to my index.jsp file in my WEB-INF/views directory, because of the InternalViewResolver prepending "WEB-INF/views/" and appending ".jsp"

Mark


Mark, it's clear for .jsp files, but I want to access static .html files. Also, I don't want to locate all new .html files in "WEB-INF/views" folder, because these .html files will be not created by programmers. I want to put them under "myWebapp/pages" folder. Is there another approach than editing "servlet-context" file and putting there <mvc:view-controller> tag? Because it will be wrong to edit this file after adding few more static .html files (for example, new articles or news).

Thanks in advance.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2051
    
  22

Why are you even putting these files inside the webapp? If they are going to be authored by people outside of your development, drop them into a HTTP server (like Apache) that fronts your web container. Serving the files through Apache is going to be much more lightweight
Tom Nilabar
Greenhorn

Joined: Nov 27, 2012
Posts: 7
Hi, Jayesh. I forgot to write magic word - I am newbie in web development).
So I thought in order to create website by Spring MVC all my files, including content of site, have to be located in this webapp folder. But after your words I have another question - could I mix
content files, that will be served by Apache, with webapp? If you have an example, could you send me link or source?
Thanks for your reply.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2051
    
  22

sorry for the jargon I threw at you.

Yes, the recommended way of making a website that can be accessed by large number of users is to host the static content in a http server, and host the dynamic content in a web container like Tomcat. You can look at This for an explanation. It's not very easy to do for a beginner. If this is like a college project, you might want to forget I said anything

Most people do put their static content inside the webapp, and that's fine for most webapps. Web servers for the most part do a decent job of serving static content that usually it's not a problem. Also, if the static content is used in the dynamic pages, you will have to make sure apache is configured in each developer and qa machine, which creates a big hassle. So, to make it simple, most projects start with hosting static content in the webapp, and move it out to the http server when the load becomes very high

I was just thinking that in your case since these static pages are not developed by developers, and they are out of the way of the dynamic content, it might be easier for you to not bundle it with the dynamic content




Tom Nilabar
Greenhorn

Joined: Nov 27, 2012
Posts: 7
Jayesh, thank you for your link.
Now I'm trying to follow your advice - make Apache serve static html and Tomcat - dynamic pages.
There is one more question - is it possible to load content of static html into .jsp pages?
Tom Nilabar
Greenhorn

Joined: Nov 27, 2012
Posts: 7
I tried your link and started Apache web server in mod_jk mode.
Could you give me one more advice? I am planning to use one navbar in a website. So will it be located somewhere in {$APACHE_HOME}/htdocs folder and included in all jsps via <c:import> tag?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Tom Nilabar wrote:Jayesh, thank you for your link.
Now I'm trying to follow your advice - make Apache serve static html and Tomcat - dynamic pages.
There is one more question - is it possible to load content of static html into .jsp pages?


Yes, you can put static pages in your webapp. You don't always have to front it with a web server. It is a popular approach to do this though.

For static pages, your css and js files are also static pages that are being served through Tomcat and your webapp. So static pages would be the same. Especially with

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/" mapping="/resources/**"/>

Then in pages I have

<link href="/resources/styles/style.css" rel="stylesheet">

I can change that to be html static page instead.
<link href="/resources/static/staticHatml.html" rel="html">

Not sure what you put in rel="" but it should work.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring MVC and access to static html files
 
Similar Threads
direct access to html
XML files needed for developing using Spring MVC and Hibernate?
struts2 Vs spring mvc
Link from jsp to html
Display messages from controller on top of JSP page