What Is The Utilization Of Internalresourceviewresolver Inwards Jump Mvc? Interview Question

Earlier, I get got explained to yous nearly how Spring MVC industrial plant internally together with 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 larn to a greater extent than nearly it past times explaining the InternalResourceViewResolver class. The InternalResourceViewResolver is an implementation of ViewResolver inwards Spring MVC framework which resolves logical thought get upwards e.g. "hello" to internal physical resources e.g. Servlet together with JSP files e.g. jsp files placed nether WEB-INF folder. It is a subclass of UrlBasedViewResolver, which uses "prefix" together with "suffix" to convert a logical thought get upwards returned from Spring controller to map to actual, physical views.


For example, if a user tries to access /home URL together with HomeController returns "home" thence DispatcherServlet volition consult InternalResourceViewResolver together with it volition purpose prefix together with suffix to respect the actual physical thought which is integral to a spider web application.

Like, if prefix is "/WEB-INF/views/" together with suffix is ".jsp" thence "home" volition live resolved to "/WEB-INF/home.jsp" past times InternalResourceViewResolver.

It's likewise a  best do to pose all JSP files within WEB-INF directory, to enshroud them from straight access (e.g. via a manually entered URL). If nosotros pose thence within WEB-INF directory thence exclusively controllers volition live able to access them.

Even though it's non mandatory that View tin post away exclusively live JSP, it tin post away live JSON also, for instance for REST spider web services, merely for the sake of simplicity, we'll get got the instance of JSP every bit thought inwards this article. If yous are interested inwards thought resolution inwards REST spider web services, yous tin post away get got a hold off at REST alongside Spring MasterClass past times Eugen Paraschiv of Baeldung.



How to configure InternalResorveViewResolver inwards Spring MVC

Let's get-go start alongside the configuration of thought resolvers inwards Spring. You tin post away configure this ViewResolver either using Java Configuration or XML configuration every bit shown below:


Configuring ViewResolver using XML inwards Spring
Here is unopen to XML snippet to configure a thought resolve inwards Spring, yous tin post away purpose this if yous 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 yous tin post away likewise configure thought resolver using Java i.e. without XML. You tin post away purpose the next code to configure the internal resources thought resolver inwards your jump project:

@Bean   public ViewResolver viewResolver() {     InternalResourceViewResolver irv = new InternalResourceViewResolver();     irv.setPrefix("/WEB-INF/");     irv.setSuffix(".jsp");      return irv;    }

You tin post away come across that both the XML together with Java offers a unproblematic approach to configure internal resources thought resolver inwards Spring. Though, I advise yous purpose Java Configuration which is modern together with at i time becoming the criterion agency to configure Spring application. If yous are novel to Java Configuration, I advise yous get got a hold off at Spring Framework 5: Beginner to Guru, i of the comprehensive together with hands-on course of report to larn modern Spring.

 together with how it procedure HTTP asking coming to your spider web application What is the Role of InternalResourceViewResolver inwards Spring MVC? Interview Question




Important points nearly InteralResourceViewResolver inwards Spring MVC

Here are unopen to of the of import things to know nearly this useful shape from Spring MVC framework. This volition assistance yous to sympathise the menstruum of your projection better:

1) When chaining ViewResolvers, an InternalResourceViewResolver ever needs to live last, every bit it volition endeavour to resolve whatever thought name, no thing whether the underlying resources genuinely exists.

2) The InternalResourceViewResolver is likewise the default thought resolver of DispatcherServlet class, which acts every bit the forepart controller inwards Spring MVC framework.

3) By default, InternalResourceViewResolver returns InternalResourceView (i.e. Servlets together with JSP) merely it tin post away live configured to homecoming JstlView past times using the viewClass attribute every bit 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 together with whatever message source configured inwards Spring. This is especially of import when yous 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 together with dates. It's message tags tin post away purpose a Spring message source together with a Locale to properly pick out the message to homecoming inwards HTML depending upon Locale.

You tin post away farther see Spring inwards Action fifth Edition past times Craig Walls for to a greater extent than details on JstlView class. The majority is a jewel together with my favorite to larn Spring inwards detail. It is at i time likewise updated to encompass Spring 5.0 changes.

 together with how it procedure HTTP asking coming to your spider web application What is the Role of InternalResourceViewResolver inwards Spring MVC? Interview Question



4. The InteralResourceViewResolver is i of the several built-in thought resolvers provided past times Spring framework, unopen to of the most useful ones are listed below:
  • BeanNameViewResolver - resolves views every bit beans inwards the Spring application context whose ID is the same every bit the thought name. For example, if yous get got a edible bean alongside id = "home" together with a controller homecoming a logical thought get upwards every bit "home" thence 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 number of an XSLT transformation. 
Bryan Hassen's has likewise explained nearly dissimilar types of thought resolvers inwards Spring on his classic course  5 Free Spring Core together with Spring Boot Courses for Java Developers


Other Spring related articles yous 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 purpose of DispatcherServlet inwards Spring MVC? (answer)
  • How to enable Spring safety inwards Java application? (answer)
  • Does Spring certification assistance inwards Job together with Career? (article)
  • How to prepare for Spring Certification? (guide)
  • 3 Best Practices Java Developers Can larn from Spring (article)
  • Difference between @Autowired together with @Injection annotations inwards Spring? (answer)
  • 5 Spring together with Hibernate online courses for Java developers (list)

Thanks for reading this article. If yous similar this article thence delight percentage alongside your friends together with colleagues. If yous get got whatever questions or feedback thence delight drib a comment together with I'll testify to reply it every bit shortly every bit possible.

P.S. - If yous desire to larn how to prepare RESTful Web Services using Spring Framework, cheque out Eugen Paraschiv's REST alongside Spring course. He has late launched the certification version of the course, which is amount of exercises together with examples to farther cement the existent the world concepts yous volition larn from the course.

Belum ada Komentar untuk "What Is The Utilization Of Internalresourceviewresolver Inwards Jump Mvc? Interview Question"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel