1.創建web 項目,導入SpringMVC 相關支持jar 包html
commons-logging-1.2.jar下載地址:https://commons.apache.org/proper/commons-logging/download_logging.cgijava
2.配置web.xml(重點)web
關鍵的步驟:配置核心控制器:Servletspring
1 <!-- 配置SpringMVC核心控制器 --> 2 <servlet> 3 <servlet-name>DispatcherServlet</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 </servlet> 6 7 <servlet-mapping> 8 <servlet-name>DispatcherServlet</servlet-name> 9 <url-pattern>*.action</url-pattern> 10 </servlet-mapping>
3.編寫spring-mvc.xmlapache
建議放在classpath瀏覽器
下面有一個mvc 的名稱空間:spring-mvc
SpringMVC的完整配置:mvc
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-4.3.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> 12 13 <!-- 掃描Spring註解 --> 14 <context:component-scan base-package="cn.sm1234.controller"></context:component-scan> 15 16 </beans>
修改web.xmlapp
1 <!-- 配置SpringMVC核心控制器 --> 2 <servlet> 3 <servlet-name>DispatcherServlet</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <!-- 參數,用於加載spring-mvc.xml --> 6 <init-param> 7 <param-name>contextConfigLocation</param-name> 8 <param-value>classpath:spring-mvc.xml</param-value> 9 </init-param> 10 </servlet> 11 12 <servlet-mapping> 13 <servlet-name>DispatcherServlet</servlet-name> 14 <url-pattern>*.action</url-pattern> 15 </servlet-mapping>
4.編寫Controller 類測試jsp
1 package cn.sm1234.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.servlet.ModelAndView; 6 7 @Controller 8 public class HelloController { 9 10 @RequestMapping("/hello") 11 public ModelAndView hello(){ 12 System.out.println("執行 HelloController的 hello方法"); 13 //把數據保存到 ModelAndView(至關於保存 request域對象) 14 ModelAndView mv = new ModelAndView(); 15 mv.addObject("name", "Spring MVC"); 16 //返回物理路徑 17 mv.setViewName("/WEB-INF/jsp/success.jsp"); 18 return mv; 19 } 20 }
5.編寫頁面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <% 4 String path = request.getContextPath() + "/"; 5 String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path; 6 %> 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <base href="<%=basePath%>" /> 11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 12 <title>第一個SpringMVC程序</title> 13 </head> 14 <body> 15 第一個SpringMVC程序 16 <hr> 17 ${name } 18 </body> 19 </html>
6.訪問測試
訪問路徑:http://localhost:8080/ch01.spring-mvc/hello.action
結果:
瀏覽器:
控制檯: