What Is The Part Of Internalresourceviewresolver Inward Saltation Mvc? Interview Question
Earlier, I stimulate got explained to you lot close 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 sentiment resolution, which is handled past times the ViewResolver interface. In this article, you'll larn to a greater extent than close it past times explaining the InternalResourceViewResolver class. The InternalResourceViewResolver is an implementation of ViewResolver inward Spring MVC framework which resolves logical sentiment 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 sentiment 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" thus DispatcherServlet volition consult InternalResourceViewResolver as well as it volition role prefix as well as suffix to respect the actual physical sentiment which is integral to a spider web application.
Like, if prefix is "/WEB-INF/views/" as well as suffix is ".jsp" thus "home" volition live resolved to "/WEB-INF/home.jsp" past times InternalResourceViewResolver.
It's every bit good a best exercise to set all JSP files within WEB-INF directory, to cover them from straight access (e.g. via a manually entered URL). If nosotros set thus within WEB-INF directory thus entirely controllers volition live able to access them.
Even though it's non mandatory that View tin dismiss entirely live JSP, it tin dismiss live JSON also, for instance for REST spider web services, precisely for the sake of simplicity, we'll stimulate got the instance of JSP every bit sentiment inward this article. If you lot are interested inward sentiment resolution inward REST spider web services, you lot tin dismiss stimulate got a await at REST alongside Spring MasterClass past times Eugen Paraschiv of Baeldung.
Configuring ViewResolver using XML inward Spring
Here is unopen to XML snippet to configure a sentiment resolve inward Spring, you lot tin dismiss 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 dismiss every bit good configure sentiment resolver using Java i.e. without XML. You tin dismiss role the next code to configure the internal resources sentiment resolver inward your boundary project:
You tin dismiss come across that both the XML as well as Java offers a uncomplicated approach to configure internal resources sentiment resolver inward Spring. Though, I propose you lot role Java Configuration which is modern as well as directly becoming the touchstone agency to configure Spring application. If you lot are novel to Java Configuration, I propose you lot stimulate got a await at Spring Framework 5: Beginner to Guru, ane of the comprehensive as well as hands-on course of report to larn modern Spring.
1) When chaining ViewResolvers, an InternalResourceViewResolver e'er needs to live last, every bit it volition effort to resolve whatever sentiment name, no thing whether the underlying resources genuinely exists.
2) The InternalResourceViewResolver is every bit good the default sentiment resolver of DispatcherServlet class, which acts every bit the forepart controller inward Spring MVC framework.
3) By default, InternalResourceViewResolver returns InternalResourceView (i.e. Servlets as well as JSP) precisely it tin dismiss live configured to homecoming JstlView past times using the viewClass attribute every bit shown below:
The wages of using JstlView is that JSTL tags volition acquire the Locale as well as whatever message source configured inward Spring. This is peculiarly of import when you lot are using JSTL tags for formatting for displaying messages.
JSTL's formatting tags demand a Locale to properly format locale-specific values e.g. currency as well as dates. It's message tags tin dismiss role a Spring message source as well as a Locale to properly direct the message to homecoming inward HTML depending upon Locale.
You tin dismiss farther see Spring inward Action fifth Edition past times Craig Walls for to a greater extent than details on JstlView class. The mass is a jewel as well as my favorite to larn Spring inward detail. It is directly every bit good updated to comprehend Spring 5.0 changes.
4. The InteralResourceViewResolver is ane of the several built-in sentiment 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 thus delight part alongside your friends as well as colleagues. If you lot stimulate got whatever questions or feedback thus delight drib a comment as well as I'll examine to reply it every bit presently every bit possible.
For example, if a user tries to access /home URL as well as HomeController returns "home" thus DispatcherServlet volition consult InternalResourceViewResolver as well as it volition role prefix as well as suffix to respect the actual physical sentiment which is integral to a spider web application.
Like, if prefix is "/WEB-INF/views/" as well as suffix is ".jsp" thus "home" volition live resolved to "/WEB-INF/home.jsp" past times InternalResourceViewResolver.
It's every bit good a best exercise to set all JSP files within WEB-INF directory, to cover them from straight access (e.g. via a manually entered URL). If nosotros set thus within WEB-INF directory thus entirely controllers volition live able to access them.
Even though it's non mandatory that View tin dismiss entirely live JSP, it tin dismiss live JSON also, for instance for REST spider web services, precisely for the sake of simplicity, we'll stimulate got the instance of JSP every bit sentiment inward this article. If you lot are interested inward sentiment resolution inward REST spider web services, you lot tin dismiss stimulate got a await at REST alongside Spring MasterClass past times Eugen Paraschiv of Baeldung.
How to configure InternalResorveViewResolver inward Spring MVC
Let's root start alongside the configuration of sentiment resolvers inward Spring. You tin dismiss configure this ViewResolver either using Java Configuration or XML configuration every bit shown below:Configuring ViewResolver using XML inward Spring
Here is unopen to XML snippet to configure a sentiment resolve inward Spring, you lot tin dismiss 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 dismiss every bit good configure sentiment resolver using Java i.e. without XML. You tin dismiss role the next code to configure the internal resources sentiment resolver inward your boundary project:
@Bean public ViewResolver viewResolver() { InternalResourceViewResolver irv = new InternalResourceViewResolver(); irv.setPrefix("/WEB-INF/"); irv.setSuffix(".jsp"); return irv; }
You tin dismiss come across that both the XML as well as Java offers a uncomplicated approach to configure internal resources sentiment resolver inward Spring. Though, I propose you lot role Java Configuration which is modern as well as directly becoming the touchstone agency to configure Spring application. If you lot are novel to Java Configuration, I propose you lot stimulate got a await at Spring Framework 5: Beginner to Guru, ane of the comprehensive as well as hands-on course of report to larn modern Spring.
Important points close InteralResourceViewResolver inward Spring MVC
Here are unopen to of the of import things to know close this useful shape from Spring MVC framework. This volition aid you lot to empathise the menses of your projection better:1) When chaining ViewResolvers, an InternalResourceViewResolver e'er needs to live last, every bit it volition effort to resolve whatever sentiment name, no thing whether the underlying resources genuinely exists.
2) The InternalResourceViewResolver is every bit good the default sentiment resolver of DispatcherServlet class, which acts every bit the forepart controller inward Spring MVC framework.
3) By default, InternalResourceViewResolver returns InternalResourceView (i.e. Servlets as well as JSP) precisely it tin dismiss live configured to homecoming JstlView past times using the viewClass attribute every bit shown below:
/** * Sets the default setViewClass sentiment 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 wages of using JstlView is that JSTL tags volition acquire the Locale as well as whatever message source configured inward Spring. This is peculiarly of import when you lot are using JSTL tags for formatting for displaying messages.
JSTL's formatting tags demand a Locale to properly format locale-specific values e.g. currency as well as dates. It's message tags tin dismiss role a Spring message source as well as a Locale to properly direct the message to homecoming inward HTML depending upon Locale.
You tin dismiss farther see Spring inward Action fifth Edition past times Craig Walls for to a greater extent than details on JstlView class. The mass is a jewel as well as my favorite to larn Spring inward detail. It is directly every bit good updated to comprehend Spring 5.0 changes.
4. The InteralResourceViewResolver is ane of the several built-in sentiment resolvers provided past times Spring framework, unopen to of the most useful ones are listed below:
- BeanNameViewResolver - resolves views every bit beans inward the Spring application context whose ID is the same every bit the sentiment name. For example, if you lot stimulate got a edible bean alongside id = "home" as well as a controller homecoming a logical sentiment lift every bit "home" thus this edible bean volition live resolved past times BeanNameViewResolver.
- FreeMarkerViewResolver - resolver views every bit FreeMarker templates
- JasperReportsViewResolver - resolves views every bit JasperReports definitions
- XsltViewResolver - resolves views to live rendered every bit the effect of an XSLT transformation.
Other Spring related articles you lot may similar to explore this blog
- 23 Spring MVC Interview questions for ii to iii years experienced (list)
- How Spring MVC industrial plant internally? (answer)
- What is the role of DispatcherServlet inward Spring MVC? (answer)
- How to enable Spring safety inward Java application? (answer)
- Does Spring certification aid inward 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 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 thus delight part alongside your friends as well as colleagues. If you lot stimulate got whatever questions or feedback thus delight drib a comment as well as I'll examine to reply it every bit presently every bit possible.
P.S. - If you lot desire to larn how to prepare RESTful Web Services using Spring Framework, banking company friction match out Eugen Paraschiv's REST alongside Spring course. He has late launched the certification version of the course, which is amount of exercises as well as examples to farther cement the existent globe concepts you lot volition larn from the course.


0 Response to "What Is The Part Of Internalresourceviewresolver Inward Saltation Mvc? Interview Question"
Post a Comment