E
E
Eugene2014-06-03 19:23:27
Java
Eugene, 2014-06-03 19:23:27

What is the correct way to set up a viewResolver in Spring?

Hello.
There is a "HelloWorld" Spring app for self-study purposes only. I got into the Spring doc, read how to set up the context and all that. Set up. Wrote a controller, looks like this:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/")
public class IndexController {

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView index() {
        Map<String, Object> params = new HashMap<>();
        params.put("message", "Hello, world!");
        return new ModelAndView("index", params);
    }
}

everything seems to be crystal clear
here, then we have this mvc-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:component-scan base-package="helloworld.controllers" resource-pattern="**/*Controller.class"/>

    <mvc:annotation-driven/>

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean id="webContentInterceptor"
                  class="org.springframework.web.servlet.mvc.WebContentInterceptor">
                <property name="cacheSeconds" value="0"/>
                <property name="useExpiresHeader" value="true"/>
                <property name="useCacheControlHeader" value="true"/>
                <property name="useCacheControlNoStore" value="true"/>
                  </bean>
        </mvc:interceptor>
    </mvc:interceptors>

    <util:constant static-field="org.springframework.core.Ordered.HIGHEST_PRECEDENCE" id="ORDERED_HIGHEST_PRECEDENCE"/>

    <bean id="handlerExceptionResolver" class="org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver">
        <property name="order" value="#{ORDERED_HIGHEST_PRECEDENCE}"/>
    </bean>
    <bean id="jsonMessageConverter"
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>

    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/>

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="webBindingInitializer">
            <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
                <property name="conversionService" ref="conversionService"/>
            </bean>
        </property>
        <property name="messageConverters">
            <list>
                <ref bean="jsonMessageConverter"/>
            </list>
        </property>
        <property name="order" value="#{ORDERED_HIGHEST_PRECEDENCE}"/>
    </bean>

    <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/WEB-INF/views/"/>
    </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.velocity.VelocityToolboxView"/>
        <property name="suffix" value=".vm"/>
        <property name="cache" value="false"/>
    </bean>
</beans>

accordingly, in WEB-INF/views/ there are Velocity templates
, I run gradle jettyRunWar, I follow the URL and see the following picture in the response from the server:
javax.servlet.ServletException: Could not resolve view with name '' in servlet with name 'mvc-dispatcher'
  org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1204)
  org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1005)
  org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:952)
  org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
  org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
  org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
  org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

those. I receive in response from the controller not a view, but only JSON.
Tell me, please, where did I go wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2014-06-07
@zolt85

The problem was that I was using the org.springframework.web class. portlet .ModelAndView instead
of org.springframework.web. servlet .ModelAndView in the controller class. Thanks to all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question