SpringMVC學習筆記(一、環境搭建)
阿新 • • 發佈:2018-12-17
SpringMVC主要功能
在獲取多個引數、檔案上傳、servlet功能單一方面都有很好的解決辦法
SpringMVC 環境搭建
- 構建一個空的WEB專案(記住勾選xml檔案)
- 匯入所需jar包
- 書寫主配置檔案: 建立resource資料夾 建立springmvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd "> </beans >
在web.xml中新增以下程式碼
<!-- 配置SpringMVC的核心控制器類:DispatcherServlet --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
在springmvc.xml中新增controller層包掃描
<!-- 配置包掃描 -->
<context:component-scan base-package="cn.java.controller"></context:component-scan>
在springmvc.xml中添加註解驅動
<!-- 加入SpringMVC特有的註解驅動 -->
<mvc:annotation-driven></mvc:annotation-driven>