MVC: Model-View-Controller(模型-視圖-控制器) 模式。這種模式用於應用程序的分層開發。css
model(模型):用於存儲數據及用戶請求的業務邏輯;html
view(視圖):向控制器提交數據;顯示模型中的數據;前端
Controller(控制器):根據視圖層的請求判斷交給哪一個模型處理、處理後的結果交給哪一個視圖顯示。java
(圖片來源:菜鳥教程)web
先新建項目,用maven導入依賴。而後在web.xml文件中進行配置,部署DispatcherServlet(前端控制器)spring
<!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 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Archetype Created Web Application</display-name> <!-- 前端控制器 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 加載 springmvc 配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.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> <!-- 解決中文亂碼問題 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
因爲須要加載springmvc.xml文件,所以在 resources 文件夾中新建該文件,並進行配置express
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 開啓註解掃描,掃描 Controller --> <context:component-scan base-package="com.max.ssm.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 配置視圖解析器對象 --> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前綴 --> <property name="prefix" value="/WEB-INF/pages/"/>
<!-- 後綴 --> <property name="suffix" value=".jsp"/> </bean> <!-- 過濾靜態文件 <mvc:resources mapping="/css/" location="/css/**"/> <mvc:resources mapping="/image/" location="/image/**"/> <mvc:resources mapping="/js/" location="/js/**"/> --> <!-- 開啓SpringMVC註解的支持 --> <mvc:annotation-driven /> </beans>
配置文件都寫好後,因爲視圖解析器對象中設置了先後綴,所以在 WEB-INF 目錄下建立 pages文件夾,而後寫 index.jsp 文件spring-mvc
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <a href="account/findAll">Click to other page</a> </body> </html>
超連接的路徑有了,須要建立 Controller 文件mvc
package com.max.ssm.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; // @xxx 是Spring的註解,其中 RequestMapping 是映射器,與超連接路徑一致 @Controller @RequestMapping("/account") public class AccountController { @RequestMapping("/findAll") public String findAll(){ System.out.println("Find all the accounts"); return "list"; } }
點擊鏈接返回一個字符串「list」,須要一個新頁面 list.jsp,文件名與返回值一致app
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> What' s up? </body> </html>
這只是一個筆記而已,寫得不詳細,只是但願偶爾翻一翻把流程過一下看成複習。