相关问题:
http://www.iteye.com/problems/95405
http://www.iteye.com/problems/74168 (该问题给出的方案是使用classpath)
原因分析:
比如你的maven工程是如下所示,包含很多子module:
test
test-core
test-web
如果在test-web中测试springmvc的项目时,会发现当前工作目录仍然是test 而不是test-web;所以问题就是出在这。
接下来提供几个方案:
1、绝对路径方式:
@WebAppConfiguration(value = "file:E:\\test\\test-web\\src\\main\\webapp")
或
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("file:E:\\lion\\lion-web\\src\\main\\webapp")
缺点很明显,如果需要在win/linux上切换 很痛苦
2、类路径方式:
@WebAppConfiguration(value = "classpath:template")
或
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("classpath:template");
缺点也是比较明显,需要复制两份模板。
3、通过获取webapp目录路径的方式:
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
String path = FreeMarkerConfig.class.getClassLoader().getResource(".").toString().substring(6);
String webappPath = path.replace("/target/test-classes/", "");
webappPath = webappPath.replace(wac.getServletContext().getRealPath(""), "");
webappPath = webappPath + "/src/main/webapp";
configurer.setTemplateLoaderPath("file:" + webappPath);
思路就是:先获取当前的测试类编译到的路径,然后往上获取到根,再拼上src/main/webapp即可拿到目录。
所以3更通用些。
如果你只需要使用maven test 运行:更好的方案是:
1、在test-web的pom.xml中添加
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <systemProperties combine.children="append"> <property> <name>modulePath</name> <value>${project.basedir}</value> </property> </systemProperties> </configuration> </plugin>
project.basedir就是当前模块的目录。
然后在测试类中通过:System.getProperty("modulePath"); 即可拿到该路径,但是如果在如idea集成环境中不好用。
还一种方案是如在idea中使用(具体没有测试):
n Run->Edit configuration->Defaults->JUnit->Working directory
set the value$MODULE_DIR$
and Intellij will set the relative path in all junits just like Maven.
从如上方案中可以看出,【3】是最通用的方案。
相关推荐
maven管理项目+springmvc+spring+hibernate+freemarker 集成框架。其中注解格式清晰,hibernate进行了代码封装,对开发效率有了提高,对异常进行了封装。freemarker也有优化,参考common包下。对日期工具类有各种...
基础框架,可满足开发需要
最简单的Maven+SpringMVC+Freemarker入门Demo
本人自己 搭建的 一个maven + springmvc + freemarker 框架,希望对你们 有所帮助。
基于maven+springmvc+redis+mybatis整合案例框架,主要实现redis的读取案例
maven多模块+springMVC+mybatis+freemarker+mysql搭建的一套企业级架构,整体项目已跑通
基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis...
本人 自己搭的一个 maven+spring+springMvc+mybaits+freemarker 框架,希望对你有用,还有sql脚本。
完整的maven+springmvc+mybatis+freemarker+mysql框架整合
Maven+Spring+SpringMVC+MyBatis+FreeMarker模版下载为Word的Demo
Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA
maven+springMVC+mybatis+velocity+mysql+junit项目框架搭建
应用实例,可运行maven+springmvc+mybaits+freemarker+velocity。
springmvc+mybatis+maven+freemarker+urlwrite+decorators
是不是好东西你们去鉴定,SpringMVC + Spring + ibatis 可以配置多数据源,这个Demo 扩展性极强,就看你们自己如何发挥。
IDEA下使用maven构建web项目(SpringMVC+Mybatis整合).非常详细,欢迎使用。
该项目主要技术架构包括:spring+springMVC+Mybatis+MYSQL+Redis+Maven,适合初学者,简单易懂。
maven构建Spring4+SpringMVC+Mybatis3
一个Maven项目小示例
eclipse+maven+springmvc+spring+mybatis案例附带mysql数据库