marilyn
marilyn

Reputation: 401

How does spring resolves the view when we return as String?

While Returning as model spring resolves the view in Dispatcher servlet but while returning as a string how does spring resolve the view

@RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
        DateFormat.LONG, locale);
        String formattedDate = dateFormat.format(date);
        model.addAttribute("serverTime", formattedDate);
        System.out.println("serverTime"+formattedDate);
            return "resources/home.html";
    }

here in the above code I return a string how does dispatcherservlet resolves the view from it.

Upvotes: 1

Views: 160

Answers (1)

dReAmEr
dReAmEr

Reputation: 7196

It's view resolver,who does a mapping between logical name and actual resource,not DispatcherServlet.

I think you would have below declaration(may be some different view resolver),in your configuration file.

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
      <property name="prefix">    
            <value>/WEB-INF/</value>    
        </property>    
        <property name="suffix">    
            <value>.jsp</value>    
        </property>    
    </bean>

So when you write return "home.jsp" inside your controller,it's view resolver job to map it with resource /WEB-INF/home.jsp and returns.View Resolver simply attach prefix and suffix to your logical name and does mapping to actual resource.

Upvotes: 1

Related Questions