Eclipse创建SpringMVC项目

1. 创建项目

1.1 创建动态页面

在Eclipse中创建Dynamic Web Project

微信图片_20240529235826.png

微信图片_20240529235934.png

针对上图,如果在创建项目时忘记勾选了,项目没有web.xml怎么办呢? 不要慌张!
解决方法:
右键你的项目–>Java EE Tools–>Generate Deployment Descriptor Stub–>单击
刷新你的项目,web.xml就生成啦!

1.2 导入相关的jar包

在WEB-INF下的lib中导入Spring相关的jar包
将 spring-framework 中 libs 目录下的 JAR 类库复制到 Web 工程 WEB-INF/lib 目录下。复制时只需要复制XXX.RELEASE.jar 的文件。
由于 SpringMVC 依赖于 Apache Commons Logging 组件,所以还需要复制 commons-logging-1.2.jar 文件。

2. 项目配置

2.1 web.xml

将WebContent/WEB-INF/web.xml文件中的内容替换成以下(2.3写法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!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>

<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

将WebContent/WEB-INF/web.xml文件中的内容替换成以下(2.2写法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>HelloWorld</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<display-name>SpringMVCTest</display-name>

<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

*2.2 SpringMVC-servlet

在WEB-INF路径下建立一个SpringMVC-servlet文件(和2.3选择一个做法就行)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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:context="http://www.springframework.org/schema/context"
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-4.3.xsd">

<!-- 扫描找到控制层 -->
<context:component-scan base-package="controller"></context:component-scan>

<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

2.3 spring-mvc.xml

在src根目录下创建一个config文件夹存放spring-mvc.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<context:component-scan base-package="controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

3. 项目编写

3.1 控制层(controller)

类路径:src/controller,注意在2.2,2.3中扫描类的位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

@RequestMapping(value = "hello") // 表示名为hello的请求响应
public String hello() {
System.out.println("test success!");
return "success";
}

}

3.2视图层(jsp)

创建路径:WebContent/index.jsp作为首页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%--
Created by IntelliJ IDEA.
User: kd_13
Date: 2024/5/25
Time: 16:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index</title>
</head>
<body>
<h1>This is index.jsp</h1>
<a href="hello">Click me!!</a>
</body>
</html>

路径:webapp/WEB-INF/pages/success.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<h1>康弟弟的Eclipse SpringMVC项目</h1>
</body>
</html>

4. 项目截图

4.1 完整路径截图

4.2 页面截图

image-20241123231709650

5. 项目总结

1.一定一定不能缺少commons-logging这个包,否则TomCat会无法运行。

2.src作为根目录,其中文件的引用将src作为com根目录就行。