- 源码地址: https://github.com/chen-jiacheng/springmvc-quickstart
一、使用 IDEA 创建 Spring MVC 项目
直接创建项目即可
默认项目结构:
springmvc-quickstart
├── pom.xml
└── src├── main│ ├── java│ │ └── com│ │ └── chenjiacheng│ │ └── springmvc│ │ └── Main.java│ └── resources└── test└── java
二、依赖管理
引入servlet-api、jsp、springmvc等
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.chenjiacheng.springmvc</groupId><artifactId>springmvc-quickstart</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- spring framework version --><spring.version>5.3.24</spring.version></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId></dependency><!-- Servlet API --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp.jstl</groupId><artifactId>jstl-api</artifactId><version>1.2</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-framework-bom</artifactId><version>${spring.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><finalName>${project.artifactId}</finalName></build></project>
三、IDEA-WEB项目构建
File => Project Structure
3.1 Model
Deployment Descriptors: ~/IdeaProjects/springmvc-quickstart/src/main/webapp/WEB-INF/web.xml
Web Resource Directories: ~/Jetbrains/IdeaProjects/springmvc-quickstart/src/main/webapp
3.2 Artifacts
项目结构:
springmvc-quickstart
├── pom.xml
├── springmvc-quickstart.iml
└── src├── main│ ├── java│ │ └── com│ │ └── chenjiacheng│ │ └── springmvc│ │ └── Main.java│ ├── resources│ └── webapp│ └── WEB-INF│ └── web.xml└── test└── java
四、代码编写
4.1 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><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:spring-mvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>
4.2 spring-mvc.xml
位置: ~/Jetbrains/IdeaProjects/springmvc-quickstart/src/main/resources/spring-mvc.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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package="com.chenjiacheng.springmvc"/><mvc:default-servlet-handler/><mvc:annotation-driven/><bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/><property name="prefix" value="/views/"/><property name="suffix" value=".jsp"/></bean>
</beans>
4.3 JSP 文件编写
4.3.1 index.jsp
位置: ~/Jetbrains/IdeaProjects/springmvc-quickstart/src/main/webapp/index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Spring MVC</title>
</head>
<body>
<h1>Hello, Spring MVC.</h1>
</body>
</html>
4.3.2 echo.jsp
位置: ~/Jetbrains/IdeaProjects/springmvc-quickstart/src/main/webapp/views/echo.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Echo</title>
</head>
<body>
<h1>EchoServer: ${msg} </h1>
</body>
</html>
4.4 EchoController
位置:~/Jetbrains/IdeaProjects/springmvc-quickstart/src/main/java/com/chenjiacheng/springmvc/controller/EchoController.java
package com.chenjiacheng.springmvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;/*** Created by chenjiacheng on 2024/5/23 00:47** @author chenjiacheng* @since 1.0.0*/
@Controller
public class EchoController {@GetMapping("/echo")public String echo(Model model, @RequestParam("msg")String msg){model.addAttribute("msg",msg);return "echo";}
}
最终项目结构:
springmvc-quickstart
├── pom.xml
├── springmvc-quickstart.iml
└── src├── main│ ├── java│ │ └── com│ │ └── chenjiacheng│ │ └── springmvc│ │ ├── Main.java│ │ └── controller│ │ └── EchoController.java│ ├── resources│ │ └── spring-mvc.xml│ └── webapp│ ├── WEB-INF│ │ └── web.xml│ ├── index.jsp│ └── views│ └── echo.jsp└── test└── java
五、配置Tomcat服务器
5.1 配置Tomcat服务器
5.2 启动&访问
5.2.1 访问地址主页
地址: http://localhost:8080/springmvc_quickstart_war_exploded/
返回: index.jsp
5.2.2 访问 echo.jsp
地址: http://localhost:8080/springmvc_quickstart_war_exploded/echo?msg=hello,springmvc
返回: echo.jsp