I am developing a website with Spring, and am trying to serve resources that are not .jsp files (.html for example)
right now i have commented out this part of my servlet configuration
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
And tried to return fromthe controller the full path to the resource.
@Controller
public class LandingPageController {
protected static Logger logger = Logger.getLogger(LandingPageController.class);
@RequestMapping({"/","/home"})
public String showHomePage(Map<String, Object> model) {
return "/WEB-INF/jsp/index.html";
}
}
the index.html file exists in that folder.
NOTE: when i change the index.html to index.jsp my server now serves the page correctly.
Thank you.
The initial problem is that the the configuration specifies a property
suffix=".jsp"
so the ViewResolver implementing class will add.jsp
to the end of the view name being returned from your method.However since you commented out the
InternalResourceViewResolver
then, depending on the rest of your application configuration, there might not be any other ViewResolver registered. You might find that nothing is working now.Since
.html
files are static and do not require processing by a servlet then it is more efficient, and simpler, to use an<mvc:resources/>
mapping. This requires Spring 3.0.4+.For example:
which would pass through all requests starting with
/static/
to thewebapp/static/
directory.So by putting
index.html
inwebapp/static/
and usingreturn "static/index.html";
from your method, Spring should find the view.You can still continue to use the same View resolver but set the suffix to empty.
Now your code can choose to return either index.html or index.jsp as shown in below sample -
I'd just add that you don't need to implement a controller method for that as you can use the view-controller tag (Spring 3) in the servlet configuration file:
change p:suffix=".jsp" value acordingly otherwise we can develope custom view resolver
http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/view/UrlBasedViewResolver.html
It sounds like you are trying to do something like this:
If that is the case, as previously mentioned, the most efficient way is to let the web server(not Spring) handle HTML requests as static resources. So you'll want the following:
Here is one way to accomplish that...
web.xml - Map servlet to root (/)
Spring JavaConfig
Additional Considerations
Step-1 in server's web.xml comment these two lines
Step-2 enter following code in application's web xml
Step-3 create a static controller class
Step-4 in the Spring configuration file change the suffix to .htm .htm
Step-5 Rename page as .htm file and store it in WEB-INF and build/start the server