1胸私、寫在前面
在項目真實開發(fā)過程中厌处,框架使用太多,已經(jīng)不知道Spring的最簡形式和原理了岁疼,在此記錄學(xué)習(xí)過程
2阔涉、核心操作
- @Controller接口暴露
- 請求網(wǎng)址測試
- 靜態(tài)頁面下載
- 添加登錄攔截器
3、詳細(xì)操作
3.1捷绒、@Controller接口暴露
@Controller在springboot中直接標(biāo)注瑰排,然后就可以進(jìn)項目的接口訪問,但是在SpringMVC的架構(gòu)中不是這樣
DispatchcerServlet是Spring實現(xiàn)的核心組件暖侨,其原理決定了為什么Spring不能直接是要科技館Controller標(biāo)注
1)添加POM依賴
2)web.xml添加依賴(DispatchcerServlet)監(jiān)聽器
<!-- 配置DispatchcerServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring mvc下的配置文件的位置和名稱 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置過濾器-->
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3)添加spring-mvc.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: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">
<description>Spring MVC Configuration</description>
<!-- 配置自動掃描的包, 使用 Annotation 自動注冊 Bean,只掃描 @Controller -->
<context:component-scan base-package="com.enzoism.spring" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 默認(rèn)的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 定義視圖文件解析, 配置視圖解析器 如何把handler 方法返回值解析為實際的物理視圖 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 靜態(tài)資源映射 -->
<mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
</beans>
3.2椭住、請求網(wǎng)址測試
- localhost:8082/test/index
- localhost:8082/test/main
- localhost:8082/test/json
- localhost:8082/test/hello
- localhost:8082/main/index
- http://localhost:8082/static/index.html
說明:沒有使用Controller以前,html靜態(tài)是可以進(jìn)行訪問的字逗,添加dispatchcerServlet后京郑,所有的請求都被攔截,靜態(tài)頁面只能在指定的目錄下才能夠被訪問葫掉,其余所有JSP頁面跳轉(zhuǎn)都是controller請求
3.3傻挂、靜態(tài)頁面下載
- 靜態(tài)登錄模板下載
- 靜態(tài)登錄百度云
- 當(dāng)前代碼下載(master分支):https://gitee.com/enzoism/java-simple-springmvc
3.4、添加登錄攔截器
- 當(dāng)前代碼下載(master-01-interceptor分支):https://gitee.com/enzoism/java-simple-springmvc