简体   繁体   中英

Spring mvc empty servlet name in exception

I have the next problem in spring mvc.

Here is my method in controller I want to test :

    @Controller
@RequestMapping("/groups")
public class GroupController {
    @Autowired
    private GroupService groupService;

    @RequestMapping(method = RequestMethod.GET)
      public String getGroups() {
        return "groups";
    }

Spring config file

<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"
   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">

<context:component-scan base-package="com.softserveinc.ita"/>
<mvc:annotation-driven />
<mvc:resources location="/WEB-INF/css/" mapping="css/**"/>
<mvc:resources location="/WEB-INF/js/" mapping="js/**"/>
<mvc:resources location="/WEB-INF/images/" mapping="images/**"/>
<mvc:resources location="/WEB-INF/template/" mapping="template/**"/>

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="suffix" value=".ftl"/>
</bean>
<bean id="jsonUtil" class="com.softserveinc.ita.utils.impl.JsonUtilGsonImpl"/>

Web XML

<web-app version="2.4"
     xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Test

      @Test
public void testGetGroupsAndExpectIsOk() throws Exception {
    mockMvc.perform(
            get("/groups")
    )
            .andExpect(status().isOk())
            .andExpect(view().name("groups"));
}

I have more methods in controller and more tests, but only 1 doesnt work. I have the following exception :

javax.servlet.ServletException: Could not resolve view with name 'groups' in servlet with name ''

Why does servlet name is empty? When i run my application, getGroups() method in cotroller works correct, but doesnt pass the test.

And when I change my view resolver from ftl to jsp

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

It works perfect with this view resolver.

I do not see how you are loading your spring config file. Please load the spring config file using the web.xml and this may resolve your issue.

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>your spring config file location</param-value>
</init-param>    
<load-on-startup>1</load-on-startup>
</servlet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM