spring mvc 是什麼
Spring Web MVC是一種基于Java的實作了Web MVC設計模式的請求驅動類型的輕量級Web架構,即使用了MVC架構模式的思想,将web層進行職責解耦,基于請求驅動指的就是使用請求-響應模型,架構的目的就是幫助我們簡化開發,Spring Web MVC也是要簡化我們日常Web開發的
spring mvc 流程圖

個人了解的流程(如有不對請指正)
首先在web.xml中配置DispatcherServlet,攔截用戶端的http請求,在根據自己的spring mvc配置檔案 找到相應的controller,對請求進行相應的處理後,傳回ModelAndView類對象,DispatcherServlet生成相應的model模型,把model傳入相應的view視圖,實作顯示。
核心類與接口
DispatcherServlet – 前置控制器
HandlerMapping接口 – 處理請求的映射
HandlerMapping接口的實作類
SimpleUrlHandlerMapping 通過配置檔案,把一個URL映射到Controller
DefaultAnnotationHandlerMapping 通過注解,把一個URL映射到Controller類上
HandlerAdapter接口 – 處理請求的映射
AnnotationMethodHandlerAdapter類,通過注解,把一個URL映射到Controller類的方法上
Controller接口 – 控制器
HandlerInterceptor 接口–攔截器
ViewResolver接口的實作類
UrlBasedViewResolver類 通過配置檔案,把一個視圖名交給到一個View來處理
InternalResourceViewResolver類,比上面的類,加入了JSTL的支援
View接口
JstlView類
LocalResolver接口
HandlerExceptionResolver接口 –異常處理
SimpleMappingExceptionResolver實作類
ModelAndView類
常見的注解
@Controller 負責注冊一個bean 到spring 上下文中
@RequestMapping 控制器指定可以處理哪些 URL 請求
@RequestBody 用于讀取Request請求的body部分資料,使用系統預設配置的HttpMessageConverter進行解析,然後把相應的資料綁定到要傳回的對象上 ,再把HttpMessageConverter傳回的對象資料綁定到 controller中方法的參數上
@ResponseBody 用于将Controller的方法傳回的對象,通過适當的HttpMessageConverter轉換為指定格式後,寫入到Response對象的body資料區
@ModelAttribute
在方法定義上使用 @ModelAttribute 注解:Spring MVC 在調用目标處理方法前,會先逐個調用在方法級上标注了@ModelAttribute 的方法
在方法的入參前使用 @ModelAttribute 注解:可以從隐含對象中擷取隐含的模型資料中擷取對象,再将請求參數 –綁定到對象中,再傳入入參将方法入參對象添加到模型中
@RequestParam 在處理方法入參處使用 @RequestParam 可以把請求參 數傳遞給請求方法
@ExceptionHandler 注解到方法上,出現異常時會執行該方法
@ControllerAdvice 使一個Contoller成為全局的異常處理類,類中用@ExceptionHandler方法注解的方法可以處理所有Controller發生的異常
spring mvc使用者登入入門案例
業務邏輯:等index.jsp中填寫使用者資訊,然送出到背景的/hello控制器中進行校驗,檢驗通過後跳轉到successs.jsp,檢驗失敗跳轉到error.jsp中
第一步導入要使用的jar包
第二步在WEB-INF下建立檔案夾jsp,再jsp檔案夾中建立successs.jsp和error.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>
<p>hello spring mvc</p>
${map.name}
</body>
</html>
<%@ 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>
${message1 }
</body>
</html>
第三步配置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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>springMvc</display-name>
<!-- 配置springmvc的檔案 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加載src目錄下的springmvc的配置檔案 srpingmvc-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.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.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>
</web-app>
第四步 配置src目錄下的springmvc-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: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 http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- scan the package and the sub package -->
<context:component-scan base-package="com.springMVC.test"/>
<!-- don't handle the static resource -->
<mvc:default-servlet-handler />
<!-- if you use annotation you must configure following setting -->
<mvc:annotation-driven />
<!-- configure the InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<!-- 字首 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 字尾 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
第五步建立控制器hellController.java
在src目錄下的com.springMVC.test包下建立類hellController.java
package com.springMVC.test;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/*★★★★★★這裡導包的時候一定要對 ,,不然使用的ModelAndView對象是不正确的*/
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/c")
public class helloController {
@RequestMapping("/hello")
public String sayHello(@RequestParam("username")String userName,@RequestParam("password")String password){
if(userName.equals("alleged")&&password.equals("root")){
System.out.println(userName+password);
return "successs";
}else{
System.out.println(userName+password);
return "error";
}
}
//帶資料傳回
}
最後一步建立首頁index.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>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<form method="post" action="/springMvc/c/hello">
<input type="text" required="required" placeholder="使用者名" name="username"></input>
<input type="password" required="required" placeholder="密碼" name="password"></input>
<button class="but" type="submit">登入</button>
</form>
<a href="contacts.html">contacts</a>
</body>
</html>