使用Maven创建Spring MVC的Hello项目
2017-09-04 本文已影响145人
每天学点编程
使用Maven创建Spring MVC项目
mvn archetype:generate -DgroupId=com.maijunjin.springmvc.test -DartifactId=springmvc-test -DarchetypeArtifactId=maven-archetype-webapp
把项目导入eclipse中。
创建两个缺失的目录:

在pom.xml中记得把Packaging改为jar:

配置Spring MVC的依赖

由于spring框架可能会被多个项目引用,是的,很多项目都使用了spring框架,所以:

就是统一spring框架的版本。
配置web.xml

为了完整演示spring mvc的DispatcherServlet
,我们配置了contextConfigLocation
。
配置Spring mvc的配置文件

请注意跟一般的spring项目的区别。
<mvc:annotation-driven/>
是开启Spring mvc相关的注解配置。
编写Spring MVC的控制器

log4j的配置
log4j.properties

运行项目
切换到pom.xml文件所在的目录下:
mvn jetty:run
运行结果如下:

访问http://localhost:8080/hello/mvc

建议配置日志——log4j.properties

解决遇到的bug——Error scanning entry from java file://
详细的错误信息如下:

解决方法:
删除以下目录C:\software\maven\repository\log4j:

然后重新运行maven命令。
解决遇到的bug——Java compiler level does not match the version of the installed Java project facet.

解决办法:
