准备:
1)jdk1.7
2)myeclipse12
3)maven3.3.9
4)tomcat7.0(最好是解压缩版)
步骤:
1)打开myeclipse.新建maven project.
Next->Next->Finish
得到目录如下。
2)此时jsp显示错误。因为pom.xml没有导入相关都jar包。故先对pom.xml进行修改。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.winne.test.maven.springmvc</groupId> <artifactId>mavenspringmvctest</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>mavenspringmvctest Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <springframework.version>4.0.6.RELEASE</springframework.version> </properties> <dependencies> <!-- 导入java ee jar 包 --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- spring mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework.version}</version> </dependency> <!-- JSP/Servlet/Jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <finalName>mavenspringmvctest</finalName> </build> </project>
3)等待一会会儿时间就显示没错了。因为导入了javaee-api.
在WEB-INF建立下列项。
忽略applicationContext.xml。去掉好了。
web.xml如下
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Archetype Created Web Application</display-name> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 配置springmvc 入口 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring-web.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 首页 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>在web.xml中配置了spring.xml的自定义路径
classpath:config/spring-web.xml位置如下图。
4)编写java
package com.winne.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/") public class HelloWorldController { @RequestMapping(method=RequestMethod.GET) public String sayHello(ModelMap model){ model.addAttribute("greeting", "Hello World from Spring 4 MVC"); return "welcome"; } @RequestMapping(value = "/helloagain", method = RequestMethod.GET) public String sayHelloAgain(ModelMap model) { model.addAttribute("greeting", "Hello World Again, from Spring 4 MVC"); return "welcome"; } }5)右键点击project,选run,点击maven clean,maven install,Myeclipse Server Application
6)最终的目录如下。
另外几点说明:
1)