1.SpringMVC的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"> <!--1.注冊dispatcherservlet 實際上就是一個servlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--SpringMVC配置--> <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> </web-app>
2.SpringMVC配置文件的編寫:
<?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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 原理級別的編寫流程: 處理器映射器 處理器適配器 視圖解析器 Controller bean --> <!--注解級別的編寫--> <!--1.自動掃描包,讓該包下的注解生效 由spring進行 bean 管理--> <context:component-scan base-package="com.xbf.controller"/> <!--2.讓SpringMVC不處理靜態資源--> <mvc:default-servlet-handler/> <!--3. 支持注解驅動 在spring中使用@RequestMapping來完成映射關系, 要想使@RequestMapping注解生效 必須向上下文中注冊DefaultAnnotationHandlerMapping 和一個AnnotationMethodHandlerAdapter實例 這兩個實例分別在類級別和方法級別處理。 而annotation-driven配置幫助我們自動完成上述兩個實例的注入。 --> <mvc:annotation-driven/> <!--4.視圖解析器--> <!--我們將視圖放在 web-inf目錄下,這個目錄客戶不能進行直接訪問,比較安全--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
3.controller層的編寫:
package com.xbf.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/controller") public class HelloController { @RequestMapping("/hello") public String hello(Model model){ model.addAttribute("msg","hellospring"); return "hello"; } }
Restful風格:(Controller層的編寫)
@Controller //自動掃描包的注解 @RequestMapping("/restful") //注解驅動 public class RestfulController { @RequestMapping("/test1/{p1}/{p2}") //restful風格的前端傳入參數的形式 //接受前端參數的注解: @PathVariable public String restful(@PathVariable int p1, @PathVariable int p2, Model model){ int sum=p1+p2; //獲取前端傳地進來的參數 model.addAttribute("msg",sum); //將使用參數的計算結果放入目標數 //並返回給前端頁面 return "hello"; }
4,前端頁面的編寫:
注解為@Controller是為了讓Spring IOC容器初始化時自動掃描到;
@RequestMapping是為了映射請求路徑,這里因為類與方法上都有映射所以訪問時應該是/HelloController/hello;
方法中聲明Model類型的參數是為了把Action中的數據帶到視圖中;
<%-- Created by IntelliJ IDEA. User: xbf Date: 2019/8/8 Time: 1:30 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} </body> </html>
總結:
流程:
1.web.xml文件的編寫:
dispatcherservlet的注冊--》SpringMVC配置文件,起動級別,過濾請求的范圍
2.springmvc-servlet.xml文件的編寫:
自動掃描包
注解驅動
靜態資源過濾
視圖解析
3.controller層的編寫
@Controller
@RequestMappping("hello")
4.前端頁面的編寫;
總之總的思想就是:用戶前端傳遞來的請求---》DispatcherServlet-----》具體的Controller進行處理並返回!