1

我在 spring mvc 中有下一个问题。

这是我要测试的控制器中的方法:

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

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

弹簧配置文件

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

网络 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
public void testGetGroupsAndExpectIsOk() throws Exception {
    mockMvc.perform(
            get("/groups")
    )
            .andExpect(status().isOk())
            .andExpect(view().name("groups"));
}

我在控制器中有更多方法和更多测试,但只有 1 不起作用。我有以下例外:

javax.servlet.ServletException:无法在名称为“”的 servlet 中解析名称为“组”的视图

为什么servlet名称为空?当我运行我的应用程序时,控制器中的 getGroups() 方法工作正常,但没有通过测试。

当我将视图解析器从 ftl 更改为 jsp

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

它与此视图解析器完美配合。

4

1 回答 1

0

我看不到你是如何加载你的 spring 配置文件的。请使用 web.xml 加载 spring 配置文件,这可能会解决您的问题。

<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>
于 2014-07-03T14:48:35.343 回答