Eclipse+maven 构建第一个简单的springmvc项目

时间:2022-06-17 06:57:43

先给出项目的目录:

Eclipse+maven 构建第一个简单的springmvc项目

在eclipse下使用maven构建第一个springmvc项目步骤如下:

1.创建maven project(此处默认你已了解maven),此处需要注意以下两点

Eclipse+maven 构建第一个简单的springmvc项目

Eclipse+maven 构建第一个简单的springmvc项目

2.创建完毕后会看到一个 pom.xml的配置文件,此时需要引入spring web mvc的相关maven依赖,具体版本请看:MVNRepository ,一般,在这里,你可以搜索相关的maven依赖,copy到pom.xml文件即可

(copy保存后就会下载相关的包了)

我的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.example</groupId>
<artifactId>springmvc-maven</artifactId>
<packaging>war</packaging> <!-- 使用的是war包 -->
<version>0.0.1-SNAPSHOT</version>
<name>springmvc-maven Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<!-- 导入springmvc的相关依赖,RELEASE是稳定版 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency> </dependencies> <!-- 配置maven插件 -->
<build>
<!-- java编译插件 -->
<!-- eclipse默认使用的jdk是1.5的 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build> <!-- <build>
<finalName>springmvc-maven</finalName>
</build> -->
</project>

3.配置web.xml文件,此时的web.xml在WEB-INF 目录下,在本例子中web.xml主要是servlet的基本配置。如下:

 <!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:example-servlet.xml</param-value>
</context-param>
<!-- Could not open ServletContext resource [/WEB-INF/example-servlet.xml] -->
<!-- 不过这个貌似不是必须的?只要下面那个就可以?虽然会waring? -->
<!-- servlet的配置 -->
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 在web.xml里配置需要加载的spring配置文件。 如果要装入多个配置文件,在<param-value>标记中用逗号作分隔符即可。 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:example-servlet.xml</param-value>
</init-param> <load-on-startup>1</load-on-startup> <!-- 配置servlet的启动时刻 -->
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/</url-pattern> <!-- 系统中的请求经过的 -->
</servlet-mapping> </web-app>

解析一下上面代码:

第16行,建立一个名为example的servlet,根据官方文档,你要建立一个与之对应的example-servlet.xml(记住这点)

第24行,好像是决定servlet的启动时刻,是随着服务器启动还是等请求到来才启动,上面的设置是随着服务器启动而启动(这点不是很确定)

第8-11行和19-22行结合使用,因为此时使用的是maven来管理项目,它有个专门存放资源文件的目录 src/main/resources,上面数说的建立的example-servlet.xml不是很往常一样,直接放在WEB-INF目录下,而是放在src/main/resources下,这两部分是为了避免出现<!--  Could not open ServletContext resource [/WEB-INF/example-servlet.xml] --> 这个错误(建议读一下官方文档

Eclipse+maven 构建第一个简单的springmvc项目

Eclipse+maven 构建第一个简单的springmvc项目

4.接着就是在src/main/resources目录下建立example-servlet.xml配置文件,并进行以下的配置

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置controller层路径扫描与视图解析器 -->
<!--扫描controller所在的包 -->
<context:component-scan
base-package="com.example.springweb.mvc" /> <!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" /> <!--前缀-->
<property name="suffix" value=".jsp" /> <!-- 后缀 -->
</bean> <!-- ... -->
</beans>

上面的xml中,12行-22行是新增的配置,剩余部分都是基本的,其实这部分你可以在官网找到的

Eclipse+maven 构建第一个简单的springmvc项目

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven/> </beans>

在example-servlet.xml中有这样一句配置

<context:component-scan
base-package="com.example.springweb.mvc" />

因此,要在src/main/java下新建一个 com.example.springweb.mvc 包,并在其下建立controller类

5.建立controller类

在src/main/java下新建一个名为 com.example.springweb.mvc 的包,并新建一个名为IndexController的java类。

IndexController.java

package com.example.springweb.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class IndexController { @RequestMapping("/home") //这里路由映射为/home,所以http://localhost:8080/springmvc-maven/不能访问到
public String home() { //这 里方法是Sring类型,因此要在WEB-INF创建一个home.jsp的页面
return "home";
} }

此时在IndexController.java定义了一个String类型返回值的方法,对应的要在 在WEB-INF创建一个名为home.jsp的页面(原理我也不懂)

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>你好</h1>
</body>
</html>

最后,右键项目Run on Server 即可。