I have a folder in Spring MVC src/main/webapp/WEB-INF/views
that contains my view templates. I want to move it to src/main/java/views
(or some other folder outside of src/main/webapp
). How do I configure that in Spring MVC?
I see a file src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml
that contains
<beans:property name="prefix" value="/WEB-INF/views/" />
It's obvious from looking at that that I can change it to
<beans:property name="prefix" value="/views/" />
which would put my views in src/main/webapp/views
, but I don't see any way to move views
out of the src/webapp
folder.
In other words, it's ultra clear that value="/"
is equivalent to src/main/webapp/
, so it seems like I'm limited to putting my view folder inside src/main/webapp.
How in the world do I set my view folder to be outside of src/main/webapp
, for example in src/main/foobar
!?! No matter what I search on Google/Bing I can't find anything.
EDIT: I tried to use the /../
to achieve it:
<beans:property name="prefix" value="/../java/edu/csus/csc191/views/" />
but that results in a error that says
SEVERE: Servlet.service() for servlet [appServlet] in context with
path [/hp-dsat] threw exception [Request processing failed; nested
exception is org.thymeleaf.exceptions.TemplateInputException: Error
resolving template "v2_upload", template might not exist or might not
be accessible by any of the configured Template Resolvers] with root
cause
The said template does exist in src/main/webapp/../java/views
which is equivalent to src/main/java/views
Normally on application build to war it will look for components in src/main/webapp, You can change this using maven custom project structure.
How to configure custom maven project structure
It is better to keep the view components in WEB-INF because it is not publicly accessible.
As you are using Thymeleaf this is easy to achive. I'm not sure if there is a similar solution for Jsps.
All you need to do is change (or add) a template resolver. Thymeleaf currently supports directly the ServletContextTemplateResolver and a ClassLoaderTemplateResolver (and if I remember correctly also a FilepathTemplateResolver).
So what you want in your case is the ClassLoaderTemplateResolver declared in the TemplateEngine:
<beans:bean id="classLoaderTemplateResolver"
class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
<beans:property name="prefix" value="views/" />
<beans:property name="suffix" value=".html" />
<beans:property name="templateMode" value="HTML5" />
<beans:property name="characterEncoding" value="UTF-8" />
<beans:property name="cacheable" value="${thymeleaf.template.cacheable}" />
<beans:property name="order" value="0" />
</beans:bean>
<beans:bean class="org.thymeleaf.spring3.dialect.SpringStandardDialect"
id="thymeleafSpringDialect" />
<beans:bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<beans:property name="templateResolvers">
<beans:set>
<beans:ref bean="classLoaderTemplateResolver" />
</beans:set>
</beans:property>
</beans:bean>
I think, you can not do it, because putting views out of webapp folder means, you are putting your web application component out of the application root, because src/main/webapp is the root of your web application.
Web servers always deploy only web application components within web application root. If you put views out of webapp, then web server will not deploy it.
Let me correct if I am wrong.