創建一個空白maven項目
maven引入spring-webmvc依賴
<dependency>
<groupId>org.springframework</groupId>
<!--SpringMVC 最核心依賴-->
<artifactId>spring-webmvc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
配置web.xml
此文件剛創建的項目沒有,自己創建即可,main目錄下創建webapp/WEB-INF目錄,WEB-INF下創建web.xml,web.xml裏面的配置如下
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--SpringMVC最核心的servlet:DispatcherServlet-->
<servlet>
<servlet-name>ssm</servlet-name>
<!--攔截請求,創建對應的Controller進行處理-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--指明DispatcherServlet初始化時加載的配置文件-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<!--啓動程序時對Servlet進行加載 0:優先級最高-->
<load-on-startup>0</load-on-startup>
</servlet>
<!--spring mvc 影射 ,啓用上面的Servlet需要作以下配置-->
<servlet-mapping>
<servlet-name>ssm</servlet-name>
<!-- / :攔截所有請求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--此項配置爲POST提交參數亂碼處理配置,使用TOMCAT時必須配置,使用jetty時無須配置,jetty自帶了亂碼配置-->
<filter>
<filter-name>characterFilter</filter-name>
<!--CharacterEncodeingFilter 將Post請求中的參數字符集設置爲UTF-8-->
<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>characterFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
上面代碼裏面有一行指明瞭DispatcherServlet初始化時加載的配置文件-applicationContext.xml,此文件新創建的項目也是沒有的,需要自己創建,resources目錄下創建applicationContext.xml
applicationContext.xml配置如下
<?xml version="1.0" encoding="UTF-8" ?>
<!--
//mvc命名空間,用於對mvc進行控制和配置
xmlns:mvc="http://www.springframework.org/schema/mvc"
//引入context命名空間,用於啓用註解
xmlns:context="http://www.springframework.org/schema/context"
-->
<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/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">
<!--啓用Spring註解形式掃描對象-->
<context:component-scan base-package="com.zhangyx"></context:component-scan>
<!--啓用SpringMVC的註解模式-->
<mvc:annotation-driven>
<mvc:message-converters>
<!--StirngHttpMessageConverter 用於設置文本類型http響應的設置-->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<!--配置支持的媒體類型(MIME)-->
<property name="supportedMediaTypes">
<list>
<!--響應輸出的文本被瀏覽器作爲html進行解釋,使用字符集爲UTF-8-->
<value>text/html;charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--將靜態資源排隊在外,用於提高執行效率【img,js,css等】-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>
測試
SpringMVC基本框架完成,很簡單吧,簡單測試一下
java目錄下創建controller類,com.zhangyx.mvc.TestController,[包.類]
@Controller
public class TestController {
@GetMapping("/test")
@ResponseBody
public String test(){
return "success";
}
}
直接訪問localhost:80/test,輸出success
我的端口是80,你的是什麼寫什麼