Earlier, I get got explained to you lot most how Spring MVC industrial plant internally as well as how it procedure HTTP asking coming to your spider web application. One of the of import parts of that processing was stance resolution, which is handled past times the ViewResolver interface. In this article, you'll larn to a greater extent than most it past times explaining the InternalResourceViewResolver class. The InternalResourceViewResolver is an implementation of ViewResolver inwards Spring MVC framework which resolves logical stance elevate e.g. "hello" to internal physical resources e.g. Servlet as well as JSP files e.g. jsp files placed nether WEB-INF folder. It is a subclass of UrlBasedViewResolver, which uses "prefix" as well as "suffix" to convert a logical stance elevate returned from Spring controller to map to actual, physical views.
For example, if a user tries to access /home URL as well as HomeController returns "home" as well as therefore DispatcherServlet volition consult InternalResourceViewResolver as well as it volition role prefix as well as suffix to discovery the actual physical stance which is integral to a spider web application.
Like, if prefix is "/WEB-INF/views/" as well as suffix is ".jsp" as well as therefore "home" volition live resolved to "/WEB-INF/home.jsp" past times InternalResourceViewResolver.
It's besides a best practise to pose all JSP files within WEB-INF directory, to shroud them from straight off access (e.g. via a manually entered URL). If nosotros pose as well as therefore within WEB-INF directory as well as therefore exclusively controllers volition live able to access them.
Even though it's non mandatory that View tin mail away exclusively live JSP, it tin mail away live JSON also, for instance for REST spider web services, only for the sake of simplicity, we'll get got the instance of JSP equally stance inwards this article. If you lot are interested inwards stance resolution inwards REST spider web services, you lot tin mail away get got a await at REST amongst Spring MasterClass past times Eugen Paraschiv of Baeldung.
Configuring ViewResolver using XML inwards Spring
Here is unopen to XML snippet to configure a stance resolve inwards Spring, you lot tin mail away role this if you lot are working on a Spring projection which is using XML based confirmation:
Configuring ViewResolver using Java Configuration
From Spring 3.0 you lot tin mail away besides configure stance resolver using Java i.e. without XML. You tin mail away role the next code to configure the internal resources stance resolver inwards your saltation project:
You tin mail away run across that both the XML as well as Java offers a elementary approach to configure internal resources stance resolver inwards Spring. Though, I propose you lot role Java Configuration which is modern as well as at nowadays becoming the touchstone means to configure Spring application. If you lot are novel to Java Configuration, I propose you lot get got a await at Spring Framework 5: Beginner to Guru, 1 of the comprehensive as well as hands-on class to larn modern Spring.
1) When chaining ViewResolvers, an InternalResourceViewResolver ever needs to live last, equally it volition endeavor to resolve whatever stance name, no affair whether the underlying resources genuinely exists.
2) The InternalResourceViewResolver is besides the default stance resolver of DispatcherServlet class, which acts equally the front end controller inwards Spring MVC framework.
3) By default, InternalResourceViewResolver returns InternalResourceView (i.e. Servlets as well as JSP) only it tin mail away live configured to homecoming JstlView past times using the viewClass attribute equally shown below:
The wages of using JstlView is that JSTL tags volition acquire the Locale as well as whatever message source configured inwards Spring. This is specially of import when you lot are using JSTL tags for formatting for displaying messages.
JSTL's formatting tags involve a Locale to properly format locale-specific values e.g. currency as well as dates. It's message tags tin mail away role a Spring message source as well as a Locale to properly direct the message to homecoming inwards HTML depending upon Locale.
You tin mail away farther see Spring inwards Action fifth Edition past times Craig Walls for to a greater extent than details on JstlView class. The mass is a precious rock as well as my favorite to larn Spring inwards detail. It is at nowadays besides updated to embrace Spring 5.0 changes.
4. The InteralResourceViewResolver is 1 of the several built-in stance resolvers provided past times Spring framework, unopen to of the most useful ones are listed below:
Other Spring related articles you lot may similar to explore this blog
Thanks for reading this article. If you lot similar this article as well as therefore delight percentage amongst your friends as well as colleagues. If you lot get got whatever questions or feedback as well as therefore delight driblet a comment as well as I'll evidence to reply it equally before long equally possible.
For example, if a user tries to access /home URL as well as HomeController returns "home" as well as therefore DispatcherServlet volition consult InternalResourceViewResolver as well as it volition role prefix as well as suffix to discovery the actual physical stance which is integral to a spider web application.
Like, if prefix is "/WEB-INF/views/" as well as suffix is ".jsp" as well as therefore "home" volition live resolved to "/WEB-INF/home.jsp" past times InternalResourceViewResolver.
It's besides a best practise to pose all JSP files within WEB-INF directory, to shroud them from straight off access (e.g. via a manually entered URL). If nosotros pose as well as therefore within WEB-INF directory as well as therefore exclusively controllers volition live able to access them.
Even though it's non mandatory that View tin mail away exclusively live JSP, it tin mail away live JSON also, for instance for REST spider web services, only for the sake of simplicity, we'll get got the instance of JSP equally stance inwards this article. If you lot are interested inwards stance resolution inwards REST spider web services, you lot tin mail away get got a await at REST amongst Spring MasterClass past times Eugen Paraschiv of Baeldung.
How to configure InternalResorveViewResolver inwards Spring MVC
Let's get-go start amongst the configuration of stance resolvers inwards Spring. You tin mail away configure this ViewResolver either using Java Configuration or XML configuration equally shown below:Configuring ViewResolver using XML inwards Spring
Here is unopen to XML snippet to configure a stance resolve inwards Spring, you lot tin mail away role this if you lot are working on a Spring projection which is using XML based confirmation:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" prefix="/WEB-INF/" suffix=".jsp" />
Configuring ViewResolver using Java Configuration
From Spring 3.0 you lot tin mail away besides configure stance resolver using Java i.e. without XML. You tin mail away role the next code to configure the internal resources stance resolver inwards your saltation project:
@Bean public ViewResolver viewResolver() { InternalResourceViewResolver irv = new InternalResourceViewResolver(); irv.setPrefix("/WEB-INF/"); irv.setSuffix(".jsp"); return irv; }
You tin mail away run across that both the XML as well as Java offers a elementary approach to configure internal resources stance resolver inwards Spring. Though, I propose you lot role Java Configuration which is modern as well as at nowadays becoming the touchstone means to configure Spring application. If you lot are novel to Java Configuration, I propose you lot get got a await at Spring Framework 5: Beginner to Guru, 1 of the comprehensive as well as hands-on class to larn modern Spring.
Important points most InteralResourceViewResolver inwards Spring MVC
Here are unopen to of the of import things to know most this useful cast from Spring MVC framework. This volition aid you lot to empathise the menstruum of your projection better:1) When chaining ViewResolvers, an InternalResourceViewResolver ever needs to live last, equally it volition endeavor to resolve whatever stance name, no affair whether the underlying resources genuinely exists.
2) The InternalResourceViewResolver is besides the default stance resolver of DispatcherServlet class, which acts equally the front end controller inwards Spring MVC framework.
3) By default, InternalResourceViewResolver returns InternalResourceView (i.e. Servlets as well as JSP) only it tin mail away live configured to homecoming JstlView past times using the viewClass attribute equally shown below:
/** * Sets the default setViewClass stance cast to requiredViewClass: past times default * InternalResourceView, or JstlView if the JSTL API is present. */ public InternalResourceViewResolver() { Class viewClass = requiredViewClass(); if (viewClass.equals(InternalResourceView.class) && jstlPresent) { viewClass = JstlView.class; } setViewClass(viewClass); } /** * This resolver requires InternalResourceView. */ @Override protected Class requiredViewClass() { return InternalResourceView.class; }
The wages of using JstlView is that JSTL tags volition acquire the Locale as well as whatever message source configured inwards Spring. This is specially of import when you lot are using JSTL tags for formatting for displaying messages.
JSTL's formatting tags involve a Locale to properly format locale-specific values e.g. currency as well as dates. It's message tags tin mail away role a Spring message source as well as a Locale to properly direct the message to homecoming inwards HTML depending upon Locale.
You tin mail away farther see Spring inwards Action fifth Edition past times Craig Walls for to a greater extent than details on JstlView class. The mass is a precious rock as well as my favorite to larn Spring inwards detail. It is at nowadays besides updated to embrace Spring 5.0 changes.
4. The InteralResourceViewResolver is 1 of the several built-in stance resolvers provided past times Spring framework, unopen to of the most useful ones are listed below:
- BeanNameViewResolver - resolves views equally beans inwards the Spring application context whose ID is the same equally the stance name. For example, if you lot get got a edible bean amongst id = "home" as well as a controller homecoming a logical stance elevate equally "home" as well as therefore this edible bean volition live resolved past times BeanNameViewResolver.
- FreeMarkerViewResolver - resolver views equally FreeMarker templates
- JasperReportsViewResolver - resolves views equally JasperReports definitions
- XsltViewResolver - resolves views to live rendered equally the final result of an XSLT transformation.
Other Spring related articles you lot may similar to explore this blog
- 23 Spring MVC Interview questions for two to iii years experienced (list)
- How Spring MVC industrial plant internally? (answer)
- What is the role of DispatcherServlet inwards Spring MVC? (answer)
- How to enable Spring safety inwards Java application? (answer)
- Does Spring certification aid inwards Job as well as Career? (article)
- How to prepare for Spring Certification? (guide)
- 3 Best Practices Java Developers Can larn from Spring (article)
- Difference between @Autowired as well as @Injection annotations inwards Spring? (answer)
- 5 Spring as well as Hibernate online courses for Java developers (list)
Thanks for reading this article. If you lot similar this article as well as therefore delight percentage amongst your friends as well as colleagues. If you lot get got whatever questions or feedback as well as therefore delight driblet a comment as well as I'll evidence to reply it equally before long equally possible.
P.S. - If you lot desire to larn how to railroad train RESTful Web Services using Spring Framework, cheque out Eugen Paraschiv's REST amongst Spring course. He has lately launched the certification version of the course, which is amount of exercises as well as examples to farther cement the existent basis concepts you lot volition larn from the course.