Remove page url prefix and suffix in spring MVC
I created a controller using spring mvc:
@Controller
public class MyController {
@RequestMapping("/")
public String showHome() {
return "/WEB-INF/pages/home.jsp";
}
@RequestMapping("/users")
public String showUser() {
return "/WEB-INF/pages/users.jsp";
}
}
I want to avoid writing this / WEB-INF / pages in every method and controller, is there a way to write just the jsp page name (with or without folder) and the application returned the correct page?
+3
jdev
source
to share
1 answer
Use Spring MVC InternalResourceViewResolver
by adding this to your Spring config file:
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
and return "home" to the controller
In your annotation driven config InternalResourceViewResolver
should be set in a config class that extends WebMvcConfigurerAdapter
like this:
@Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
+5
Safwan Hijazi
source
to share