What Is The Component Subdivision Of Internalresourceviewresolver Inwards Jump Mvc?
Sunday, January 28, 2018
Add Comment
Earlier, I cause got explained to you lot virtually 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 thought resolution, which is handled past times the ViewResolver interface. In this article, you'll acquire to a greater extent than virtually it past times explaining the InternalResourceViewResolver class. The InternalResourceViewResolver is an implementation of ViewResolver inward Spring MVC framework which resolves logical thought lift 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 thought lift 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 thence DispatcherServlet volition consult InternalResourceViewResolver as well as it volition utilisation prefix as well as suffix to detect the actual physical thought which is integral to a spider web application.
Like, if prefix is "/WEB-INF/views/" as well as suffix is ".jsp" as well as thence "home" volition live resolved to "/WEB-INF/home.jsp" past times InternalResourceViewResolver.
It's besides a best practise to seat all JSP files within WEB-INF directory, to cover them from straight access (e.g. via a manually entered URL). If nosotros seat as well as thence within WEB-INF directory as well as thence exclusively controllers volition live able to access them.
Even though it's non mandatory that View tin exclusively live JSP, it tin live JSON also, for instance for REST spider web services, only for the sake of simplicity, we'll cause got the instance of JSP equally thought inward this article. If you lot are interested inward thought resolution inward REST spider web services, you lot tin cause got a hold back at REST alongside Spring MasterClass past times Eugen Paraschiv of Baeldung.
Configuring ViewResolver using XML inward Spring
Here is around XML snippet to configure a thought resolve inward Spring, you lot tin utilisation 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 besides configure thought resolver using Java i.e. without XML. You tin utilisation the next code to configure the internal resources thought resolver inward your jump project:
You tin run into that both the XML as well as Java offers a uncomplicated approach to configure internal resources thought resolver inward Spring. Though, I propose you lot utilisation Java Configuration which is modern as well as right away becoming the measure means to configure Spring application. If you lot are novel to Java Configuration, I propose you lot cause got a hold back at Spring Framework 5: Beginner to Guru, ane of the comprehensive as well as hands-on class to acquire modern Spring.
1) When chaining ViewResolvers, an InternalResourceViewResolver e'er needs to live last, equally it volition bear witness to resolve whatever thought name, no thing whether the underlying resources truly exists.
2) The InternalResourceViewResolver is besides the default thought resolver of DispatcherServlet class, which acts equally the front end controller inward Spring MVC framework.
3) By default, InternalResourceViewResolver returns InternalResourceView (i.e. Servlets as well as JSP) only it tin live configured to homecoming JstlView past times using the viewClass attribute equally shown below:
The payoff of using JstlView is that JSTL tags volition acquire the Locale as well as whatever message source configured inward Spring. This is especially of import when you lot are using JSTL tags for formatting for displaying messages.
JSTL's formatting tags necessitate a Locale to properly format locale-specific values e.g. currency as well as dates. It's message tags tin utilisation a Spring message source as well as a Locale to properly conduct the message to homecoming inward HTML depending upon Locale.
You tin farther see Spring inward Action fifth Edition past times Craig Walls for to a greater extent than details on JstlView class. The majority is a jewel as well as my favorite to acquire Spring inward detail. It is right away besides updated to encompass Spring 5.0 changes.
4. The InteralResourceViewResolver is ane of the several built-in thought resolvers provided past times Spring framework, around 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 thence delight part alongside your friends as well as colleagues. If you lot cause got whatever questions or feedback as well as thence delight drib a comment as well as I'll bear witness to respond it equally shortly equally possible.
For example, if a user tries to access /home URL as well as HomeController returns "home" as well as thence DispatcherServlet volition consult InternalResourceViewResolver as well as it volition utilisation prefix as well as suffix to detect the actual physical thought which is integral to a spider web application.
Like, if prefix is "/WEB-INF/views/" as well as suffix is ".jsp" as well as thence "home" volition live resolved to "/WEB-INF/home.jsp" past times InternalResourceViewResolver.
It's besides a best practise to seat all JSP files within WEB-INF directory, to cover them from straight access (e.g. via a manually entered URL). If nosotros seat as well as thence within WEB-INF directory as well as thence exclusively controllers volition live able to access them.
Even though it's non mandatory that View tin exclusively live JSP, it tin live JSON also, for instance for REST spider web services, only for the sake of simplicity, we'll cause got the instance of JSP equally thought inward this article. If you lot are interested inward thought resolution inward REST spider web services, you lot tin cause got a hold back at REST alongside Spring MasterClass past times Eugen Paraschiv of Baeldung.
How to configure InternalResorveViewResolver inward Spring MVC
Let's kickoff start alongside the configuration of thought resolvers inward Spring. You tin configure this ViewResolver either using Java Configuration or XML configuration equally shown below:Configuring ViewResolver using XML inward Spring
Here is around XML snippet to configure a thought resolve inward Spring, you lot tin utilisation 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 besides configure thought resolver using Java i.e. without XML. You tin utilisation the next code to configure the internal resources thought resolver inward your jump project:
@Bean public ViewResolver viewResolver() { InternalResourceViewResolver irv = new InternalResourceViewResolver(); irv.setPrefix("/WEB-INF/"); irv.setSuffix(".jsp"); return irv; }
You tin run into that both the XML as well as Java offers a uncomplicated approach to configure internal resources thought resolver inward Spring. Though, I propose you lot utilisation Java Configuration which is modern as well as right away becoming the measure means to configure Spring application. If you lot are novel to Java Configuration, I propose you lot cause got a hold back at Spring Framework 5: Beginner to Guru, ane of the comprehensive as well as hands-on class to acquire modern Spring.
Important points virtually InteralResourceViewResolver inward Spring MVC
Here are around of the of import things to know virtually this useful shape from Spring MVC framework. This volition assistance you lot to sympathise the menses of your projection better:1) When chaining ViewResolvers, an InternalResourceViewResolver e'er needs to live last, equally it volition bear witness to resolve whatever thought name, no thing whether the underlying resources truly exists.
2) The InternalResourceViewResolver is besides the default thought resolver of DispatcherServlet class, which acts equally the front end controller inward Spring MVC framework.
3) By default, InternalResourceViewResolver returns InternalResourceView (i.e. Servlets as well as JSP) only it tin live configured to homecoming JstlView past times using the viewClass attribute equally shown below:
/** * Sets the default setViewClass thought shape 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 payoff of using JstlView is that JSTL tags volition acquire the Locale as well as whatever message source configured inward Spring. This is especially of import when you lot are using JSTL tags for formatting for displaying messages.
JSTL's formatting tags necessitate a Locale to properly format locale-specific values e.g. currency as well as dates. It's message tags tin utilisation a Spring message source as well as a Locale to properly conduct the message to homecoming inward HTML depending upon Locale.
You tin farther see Spring inward Action fifth Edition past times Craig Walls for to a greater extent than details on JstlView class. The majority is a jewel as well as my favorite to acquire Spring inward detail. It is right away besides updated to encompass Spring 5.0 changes.
4. The InteralResourceViewResolver is ane of the several built-in thought resolvers provided past times Spring framework, around of the most useful ones are listed below:
- BeanNameViewResolver - resolves views equally beans inward the Spring application context whose ID is the same equally the thought name. For example, if you lot cause got a edible bean alongside id = "home" as well as a controller homecoming a logical thought lift equally "home" as well as thence 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 resultant of an XSLT transformation.
Other Spring related articles you lot may similar to explore this blog
- 23 Spring MVC Interview questions for two to three years experienced (list)
- How Spring MVC industrial plant internally? (answer)
- What is the utilisation of DispatcherServlet inward Spring MVC? (answer)
- How to enable Spring safety inward Java application? (answer)
- Does Spring certification assistance inward Job as well as Career? (article)
- How to prepare for Spring Certification? (guide)
- 3 Best Practices Java Developers Can acquire from Spring (article)
- Difference between @Autowired as well as @Injection annotations inward 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 thence delight part alongside your friends as well as colleagues. If you lot cause got whatever questions or feedback as well as thence delight drib a comment as well as I'll bear witness to respond it equally shortly equally possible.
P.S. - If you lot desire to acquire how to educate RESTful Web Services using Spring Framework, banking concern gibe out Eugen Paraschiv's REST alongside Spring course. He has latterly 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 acquire from the course.
0 Response to "What Is The Component Subdivision Of Internalresourceviewresolver Inwards Jump Mvc?"
Post a Comment