tilesConfigurer:java.lang.IllegalArgumentException:没有ServletContext

时间:2015-08-30 18:54:36

标签: spring tiles

更新

我似乎做了一些独特的错误,因为我找不到这个问题的参考。

我正在尝试将瓷砖整合到现存的春天百里香/ webflow / hibernate应用程序中。

一切正常,直到我将tilesConfigurer添加到我的applicationContext:



<!-- Configures the Tiles layout system-->
	


<bean id="tilesConfigurer" class="org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer">
	<property name="definitions">
		<list>
			<value>/WEB-INF/**/views.xml</value>
		</list>
	</property>
</bean>	
&#13;
&#13;
&#13;

添加配置程序后,我的测试失败,但应用程序仍在tomcat8中运行

这是一个典型的测试类:

package jake.prototype2.test.service;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import jake.prototype2.model.business.Business;
import jake.prototype2.service.user.BusinessService;
import jake.prototype2.test.testrunner.TestSS;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:AssessmentAdminTL-servlet.xml")
public class BusinessServiceTest
{
@Autowired
private BusinessService businessService;

@Test
public void testGetBusinesses() {
    TestSS.getLogger().info("testGetBusinesses");
    List <Business> blist=businessService.getBusinesses();
    assertTrue(blist.size()>0);
}

@Test
public void testGetBusiness() {
    TestSS.getLogger().info("testGetBusinesses");
    Business b=null;
    try
    {
        b=businessService.getBusiness(TestSS.getBusiness().getId());
    }
    catch(Exception e)
    {
        TestSS.getLogger().error(e.getMessage(),e);
        fail();
    }
     assertFalse(b==null);
}
}

资源也在applicationContext中指定:

&#13;
&#13;
<context:component-scan base-package="jake.prototype2" />
	<mvc:resources mapping="/style/**" location="/style/" />
	<mvc:resources mapping="/images/**" location="/images/" />
	<mvc:resources mapping="/views/**" location="/WEB-INF/views/" />
	<mvc:annotation-driven />
&#13;
&#13;
&#13;

web.xml指定默认的servlet路径:

&#13;
&#13;
 
   <servlet>
      <servlet-name>AssessmentAdminTL</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
 
   <servlet-mapping>
    <servlet-name>AssessmentAdminTL</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
&#13;
&#13;
&#13;

我在应用初始化时收到以下异常:

  

引起:org.springframework.beans.factory.BeanCreationException:创建名称为&#39; tilesConfigurer&#39;在URL [file:/ C:/ Users / jake / workspace / _ >中定义   pt2 / WebContent / WEB-INF / config / views_applicationContext.xml]:调用init方法失败;嵌套异常是java.lang.IllegalArgumentException:无法解析S.   没有ServletContext的ervletContextResource           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)           at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303)           在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)           在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)           在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)           at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)           在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)           在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)           at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125)           在org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(Abs

同样,我注意到,在插入tilesconfigurer代码之前,一切正常。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

来自@ M.Deinum的很多帮助,答案是我错误地调用了上下文。

我的测试类注释应该如下:

@WebAppConfiguration
@ContextHierarchy({
@ContextConfiguration(locations = { "classpath:AssessmentAdminTL-servlet.xml" })
})

然而,虽然这解决了这里指定的问题,但我仍然有与tilesconfigurer相关的其他路径问题(可能是由于我缺少模拟上下文?)

简而言之,这是一个非常具体的问题的非常具体的答案。这不是全部故事。