Reputation: 1114
I spent several hours trying to use InternalResourceViewResolver
in order to append prefix and suffix to html views.
My views located under static/pages/
and by Spring docs, folder static
is considered to be one of defaults for static content. So, I could access profile page by pages/profile.html
. But what I really want to have is profile
instead of pages/profile.html
.
I've tried several answers, but that does not work, like:
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("pages/");
resolver.setSuffix(".html");
return resolver;
}
and adding
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
Still does not work properly. By adding any suffixes or prefixes I could not found page on any path. I am starting to get 404 on pages/profile.html
, but it also does not appear on other urls.
Upvotes: 0
Views: 2313
Reputation: 4476
Just need add your own custom configuration like this
@Configuration
public class WebMvcConfig {
@Bean
public InternalResourceViewResolver defaultViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/jsp");
resolver.setSuffix(".jsp");
return resolver;
}
}
Then you can inspect all your beans via "http://localhost:8080/beans"
And you can verfity is it using the custom configured bean:
{
"bean": "defaultViewResolver",
"scope": "singleton",
"type": "org.springframework.web.servlet.view.InternalResourceViewResolver",
"resource": "class path resource [io/cloudhuang/web/WebMvcConfig.class]",
"dependencies": [ ]
}
But the eaiest way should be config it in the application.properties
spring.mvc.view.prefix=
spring.mvc.view.suffix=
For application.yaml
spring:
mvc:
view:
prefix: templates/
suffix: .jsp
Upvotes: 2
Reputation: 321
Using Spring Boot you actually don't need to declare your own InternalResourceViewResolver. Boot declares it for you, and you can just add a couple of properties to your application.properties file. E.g. in your case these would be:
spring.mvc.view.prefix=/jsp
spring.mvc.view.suffix=.jsp
Upvotes: 0