【初】使用maven在Eclipse中搭建SpringMVC

(1)eclipse中配置maven:
(2)命令行创建spring mvc目录结构:
1、在需要创建的目录下运行:
mvn archetype:generate -DgroupId=SpringStudy -DartifactId=helloSpringMvc -DarchetypeArtifactId=maven-archetype-webapp
2、将项目导入eclipse:
Import——Maven ——Existing Maven Project
3、java文件夹缺失手动添加:

1.png

(3)编写配置文件:
1、pom.xml文件:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
SpringStudy
helloSpringMvc
war
1.0-SNAPSHOT
helloSpringMvc Maven Webapp
http://maven.apache.org


2.6
1.7.6
4.1.3.RELEASE




org.springframework
spring-framework-bom
${spring.version}
pom
import





org.springframework
spring-webmvc


commons-lang
commons-lang
${commons-lang.version}


org.slf4j
slf4j-log4j12
${slf4j.version}


slf4j-api
org.slf4j



javax.servlet.jsp
jsp-api
2.1
provided


javax.servlet
jstl
1.2


junit
junit
3.8.1
test


javax.servlet
servlet-api
3.0-alpha-1



helloSpringMvc


org.eclipse.jetty
jetty-maven-plugin
9.2.2.v20140723


org.apache.maven.plugins
maven-compiler-plugin

1.7
1.7
UTF-8

${project.basedir}/src/main/webapp/WEB-INF/lib






2、web.xml文件:
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >




mvc-dispatcher
org.springframework.web.servlet.DispatcherServlet


contextConfigLocation
/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml

1


mvc-dispatcher

/


3、mvc-dispatcher-servlet.xml文件:创建路径如下:

3.png


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
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">











4、HelloMVCController.java文件:
路径如下:

4.png

package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/hello")
public class HelloMVCController {

@RequestMapping("/mvc")
//host:8080/hello/mvc
public String helloMvc() {
return "home";
}
}
(5)在tomcat中运行:

【初】使用maven在Eclipse中搭建SpringMVC_第1张图片
44png.png

(5)使用jetty运行:
因为在pom.xml文件中加入了jetty插件,可以帮助程序运行,在项目根目录下,输入命令:
mvn jetty:run

【初】使用maven在Eclipse中搭建SpringMVC_第2张图片
44png.png

【参】
搭建IntelliJ IDEA+maven+jetty+SpringMVC 开发环境
搭建IntelliJ IDEA+maven+jetty+SpringMVC 开发环境(二)

你可能感兴趣的:(【初】使用maven在Eclipse中搭建SpringMVC)