最佳答案
引言
Spring MVC 是一個風行的 Java Web 開辟框架,它遵守 MVC(Model-View-Controller)計劃形式,可能幫助開辟者疾速構建高品質的 Web 利用順序。本文將經由過程一系列實戰案例,帶你從零開端,輕鬆控制 Spring MVC。
一、Spring MVC 簡介
Spring MVC 是 Spring 框架的一部分,它供給了豐富的功能跟易於利用的介面,使得 Web 開辟變得愈加簡單。Spring MVC 的核心組件包含:
- DispatcherServlet:前端把持器,擔任接收懇求並挪用響應的處理器。
- HandlerMapping:將懇求映射四處理器上。
- HandlerAdapter:適配器,將懇求委託給處理器。
- Controller:處理器,處理懇求並生成呼應。
- ViewResolver:視圖剖析器,將模型數據轉達給視圖。
二、實戰案例:創建一個簡單的 Spring MVC 利用
2.1 創建項目
- 利用 Maven 創建一個 Web 項目。
- 在
pom.xml
文件中增加 Spring MVC 依附。
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>
2.2 設置 DispatcherServlet
- 在
web.xml
文件中設置 DispatcherServlet。
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
2.3 創建把持器
- 創建一個把持器類,比方
HelloController
。
@Controller
public class HelloController {
@RequestMapping("/hello")
public String sayHello() {
return "hello";
}
}
2.4 創建視圖
- 在
src/main/webapp/WEB-INF/views
目錄下創建一個名為hello.jsp
的 JSP 頁面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello, World!</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
2.5 運轉項目
- 啟動 Tomcat 伺服器。
- 在瀏覽器中拜訪
http://localhost:8080/hello
,應當會看到 「Hello, World!」 的信息。
三、實戰案例:利用註解停止懇求映射
3.1 創建把持器
- 創建一個把持器類,比方
AnnotationController
。
@Controller
public class AnnotationController {
@RequestMapping("/annotation")
public String sayAnnotation() {
return "annotation";
}
}
3.2 創建視圖
- 在
src/main/webapp/WEB-INF/views
目錄下創建一個名為annotation.jsp
的 JSP 頁面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Annotation, World!</title>
</head>
<body>
<h1>Annotation, World!</h1>
</body>
</html>
3.3 運轉項目
- 在瀏覽器中拜訪
http://localhost:8080/annotation
,應當會看到 「Annotation, World!」 的信息。
四、實戰案例:利用 Spring MVC 處理表單提交
4.1 創建把持器
- 創建一個把持器類,比方
FormController
。
@Controller
public class FormController {
@RequestMapping("/form")
public String showForm() {
return "form";
}
@PostMapping("/submit")
public String submitForm(@ModelAttribute("user") User user) {
// 處理表單提交邏輯
return "success";
}
}
4.2 創建視圖
- 在
src/main/webapp/WEB-INF/views
目錄下創建一個名為form.jsp
的 JSP 頁面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Form Example</title>
</head>
<body>
<form:form action="submit" modelAttribute="user">
<div>
<label for="name">Name:</label>
<form:input path="name" id="name"/>
</div>
<div>
<label for="email">Email:</label>
<form:input path="email" id="email"/>
</div>
<div>
<input type="submit" value="Submit"/>
</div>
</form:form>
</body>
</html>
- 創建一個名為
success.jsp
的 JSP 頁面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Form Submission Success</title>
</head>
<body>
<h1>Form submitted successfully!</h1>
</body>
</html>
4.3 運轉項目
- 在瀏覽器中拜訪
http://localhost:8080/form
,填寫表單並提交,應當會看到 「Form submitted successfully!」 的信息。
五、總結
經由過程以上實戰案例,信賴你曾經對 Spring MVC 有了一定的懂得。在現實開辟中,Spring MVC 可能幫助你更疾速、更高效地構建 Web 利用順序。祝你進修高興!