Saturday, April 20, 2024
HomeJavaHow View Resolver works in Spring MVC?

How View Resolver works in Spring MVC? [InternalResourceViewResolver Example]


Hiya guys, if you’re questioning what’s view resolver in Spring MVC and what function InternalViewResolver class performs in Spring MVC then you have got come to the proper place. Earlier, I’ve defined to you how Spring MVC works internally and the way it processes HTTP requests coming to your internet utility. One of many necessary elements of that processing was resolving views, which is dealt with by the ViewResolver interface, which is the V a part of MVC. They’re answerable for returning right views to the consumer, each within the Spring MVC utility in addition to on REST APIs. On this article, you will be taught extra about view resolvers in Spring MVC by explaining the InternalResourceViewResolver class. 
The InternalResourceViewResolver is an implementation of the ViewResolver interface within the Spring MVC framework which resolves logical view names like “hi there” to inside bodily sources like Servlet and JSP recordsdata like jsp recordsdata positioned beneath the WEB-INF folder.
It’s also a subclass of UrlBasedViewResolver, which makes use of “prefix” and “suffix” to transform a logical view identify returned from the Spring controller to map to precise, bodily views.

For instance, if a consumer tries to entry /residence URL and HomeController returns “residence” then DispatcherServlet will seek the advice of InternalResourceViewResolver and it’ll use prefix and suffix to search out the precise bodily view which is integral to an online utility.

Like, if prefix is “/WEB-INF/views/” and suffix is “.jsp” then “residence” will probably be resolved to “/WEB-INF/residence.jsp” by InternalResourceViewResolver.

It is also a  finest apply to place all JSP recordsdata contained in the WEB-INF listing, to cover them from direct entry (e.g. through a manually entered URL). If we put them contained in the WEB-INF listing then solely controllers will be capable of entry them.

Despite the fact that it isn’t necessary that View can solely be JSP, it may be JSON additionally, for instance for REST internet providers, however for the sake of simplicity, we’ll take the instance of JSP as a view on this article. If you’re desirous about view decision in REST internet providers, you’ll be able to check out REST with Spring MasterClass by Eugen Paraschiv of Baeldung.

How one can use InternalResourceViewResolver in Spring MVC?

Let’s first begin with the configuration of view resolvers in Spring MVC. You’ll be able to configure this ViewResolver both utilizing Java Configuration or XML configuration as proven beneath:

1. Configuring ViewResolver utilizing XML in Spring

Right here is a few XML snippet to configure a view resolver in Spring, you should use this if you’re engaged on a Spring undertaking which is utilizing XML based mostly affirmation:

<bean id="viewResolver"
    class="org.springframework.internet.servlet.view.InternalResourceViewResolver"
    prefix="/WEB-INF/" suffix=".jsp" /> 

2. Configuring ViewResolver utilizing Java Configuration

From Spring 3.0 you can too configure view resolver utilizing Java i.e. with out XML. You should utilize the next code to configure the inner useful resource view resolver in your spring undertaking:

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

    return irv;

  }

You’ll be able to see that each the XML and Java supply a easy method to configure inside useful resource view resolver in Spring. Although, I counsel you utilize Java Configuration which is fashionable and now turning into the usual option to configure Spring utility. If you’re new to Java Configuration, I counsel you check out Spring Framework: Newbie to Guru, one of many complete and hands-on programs to be taught fashionable Spring.

what is view resolvers in spring mvc

Necessary factors about InteralResourceViewResolver in Spring MVC

Listed here are a few of the necessary issues to learn about this convenient class from the Spring MVC framework. This may aid you to know the move of your undertaking higher:

1) When chaining ViewResolvers, an InternalResourceViewResolver all the time must be final, as it’ll try and resolve any view identify, regardless of whether or not the underlying useful resource really exists.

2) The InternalResourceViewResolver can also be the default view resolver of DispatcherServlet class, which acts because the entrance controller within the Spring MVC framework.

3) By default, InternalResourceViewResolver returns InternalResourceView (i.e. Servlets and JSP) however it may be configured to return JstlView by utilizing the viewClass attribute as proven beneath:

/**
   * Units the default setViewClass view class to requiredViewClass: by default
   * InternalResourceView, or JstlView if the JSTL API is current.
   */
  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 benefit of utilizing JstlView is that JSTL tags will get the Locale and any message supply configured in Spring. That is notably necessary if you find yourself utilizing JSTL tags for formatting for displaying messages.

JSTL’s formatting tags want a Locale to correctly format locale-specific values like foreign money and dates. Its message tags can use a Spring message supply and a Locale to correctly select the message to render in HTML relying upon Locale.

You’ll be able to additional see Spring in Motion fifth Version by Craig Partitions for extra particulars on JstlView class. The ebook is a gem and my favourite to be taught Spring intimately. It’s now additionally up to date to cowl Spring 5.0 adjustments.

how does view resolver work in Spring MVC framework

4. The InteralResourceViewResolver is likely one of the a number of built-in view resolvers supplied by the Spring framework, a few of the most helpful ones are listed beneath:

  • BeanNameViewResolver – resolves views as beans within the Spring utility context whose ID is similar because the view identify. For instance, you probably have a bean with id = “residence” and a controller returns a logical view identify as “residence” then this bean will probably be resolved by BeanNameViewResolver.
  • FreeMarkerViewResolver – resolver views as FreeMarker templates
  • JasperReportsViewResolver – resolves views as JasperReports definitions
  • XsltViewResolver – resolves views to be rendered as the results of an XSLT transformation. 

Bryan Hassen has additionally defined about various kinds of view resolvers in Spring on his traditional course Spring Framework: Spring MVC Fundamentals you probably have a Pluralsight membership then that is likely one of the finest sources.

5. An important good thing about utilizing ViewResolver in Spring MVC is that it decouples request-handling logic within the controller from the view-rendering of a view. In brief, the controller does not know something about which view know-how is used to render the view.

It simply returns a logical identify that might resolve to a JSP, FreeMarker template, Apache tiles, or every other view know-how. It additionally means you’ll be able to change the view layer with out altering the controller so long as the logical view identify is similar.

The idea of view decision in Spring MVC can also be crucial from each the Spring interview in addition to the Spring certification standpoint. If you’re getting ready for Spring certification, I counsel you undergo some questions shared by David Mayer’s Spring Mock exams to check your data of view decision idea in Spring MVC.

That is all about what’s view resolvers in spring, how they work, and what’s the function of InternalResourceViewResolver in Spring MVC or what’s the function of InternalResourceViewResolver.  View decision is likely one of the core MVC ideas and Spring MVC additionally works the identical. It is one of many helpful courses from Spring MVC and as a Java Spring developer, you need to be accustomed to it. 


Different Spring associated articles you could wish to discover this weblog

  • 23 Spring MVC Interview questions for two to three years skilled (record)
  • How Spring MVC works internally? (reply)
  • What’s using DispatcherServlet in Spring MVC? (reply)
  • How one can allow Spring safety in Java utility? (reply)
  • High 7 Programs to be taught Microservices in Java (programs)
  • 10 Free Programs to be taught Spring Framework for Newcomers (free programs)
  • Does Spring certification assist in Job and Profession? (article)
  • How one can put together for Spring Certification? (information)
  • 3 Finest Practices Java Builders Can be taught from Spring (article)
  • Distinction between @Autowired and @Injection annotations in Spring? (reply)
  • 5 Spring and Hibernate on-line programs for Java builders (record)
  • 15 Spring Boot Interview Questions for Java Builders (questions)
  • High 5 Programs to Study and Grasp Spring Cloud (programs)
  • 5 Programs to Study Spring Safety in depth (programs)
  • High 5 Spring Boot Annotations Java Builders ought to know (learn)
  • @SpringBootApplication vs @EnableAutoConfiguration? (reply)
  • 5 Spring Books Skilled Java Developer Ought to Learn (books)
  • 10 Superior Spring Boot Programs for Java builders (programs)
  • 10 Free Programs to be taught Spring Boot in-depth (free programs
  • High 5 Frameworks Java Developer Ought to Know (frameworks)
  • 7 Finest Spring Programs for Newcomers and Skilled (programs)
  • 10 Spring MVC annotations Java developer ought to be taught (annotations)
  • High 5 Spring Cloud annotations Java programmer ought to be taught (cloud)
  • 5 Programs to be taught Spring Cloud for Newcomers (programs)

Thanks for studying this text. In the event you like this rationalization of view resolvers in spring and inside view resolve instance then please share it with your mates and colleagues. When you have any questions or suggestions then please drop a remark and I am going to attempt to reply it as quickly as potential.

P. S. – If you wish to learn to develop RESTful Internet Providers utilizing Spring Framework, try Eugen Paraschiv’s REST with Spring course. He has just lately launched the certification model of the course, which is stuffed with workouts and examples to additional cement the actual world ideas you’ll be taught from the course.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments