使用Maven添加依賴的jar包
?
?第一個還沒用上
剛開始沒加spring-context,@Controller沒法用
?
web.xml配置
1.? ? ???配置DispatcherServlet?
????????<description>Spring?MVC?Servlet</description>
????????<servlet-name>springMVC</servlet-name>
????????<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
????????<init-param>
????????????<description>Spring?MVC?配置文件</description>
????????????<param-name>contextConfigLocation</param-name>
????????????<param-value>classpath:springMVC.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>
代碼如上,就不多解釋了
還是說一點吧
注意:<load-on-startup>1</load-on-startup>是啟動順序,讓這個Servlet隨Servletp容器一起啟動~
<url-pattern>/</url-pattern>,請求映射配置為“/”,框架能夠捕獲所有URL請求,同時又將靜態資源的請求轉交給web容器處理(之后將進一步說明)
?
2.????? 過濾器
??1?<!--?過濾器?-->
?3?????????<description>
?4?????????</description>
?5?????????<display-name>CharacterEncodingFilter</display-name>
?6?????????<filter-name>CharacterEncodingFilter</filter-name>
?7?????????<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
?8?????????<init-param>
?9?????????????<param-name>encoding</param-name>
10?????????????<param-value>utf-8</param-value>
11?????????</init-param>
12?????</filter>
13?????<filter-mapping>
14?????????<filter-name>CharacterEncodingFilter</filter-name>
15?????????<url-pattern>/*</url-pattern>
16?????</filter-mapping>
解決Post提交中文亂碼問題,也不必自己寫過濾器啦
?
?
springMVC配置
1.xml schema配置
剛開始忽好忽壞的,忘記報啥錯了……查了查是這個的問題,缺一不可呀
?2?<beans?xmlns="http://www.springframework.org/schema/beans"
?3?????xmlns:context="http://www.springframework.org/schema/context"
?4?????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns:mvc="http://www.springframework.org/schema/mvc"
?5?????xsi:schemaLocation="
?6??http://www.springframework.org/schema/beans
?7??http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
?8??http://www.springframework.org/schema/context
?9??http://www.springframework.org/schema/context/spring-context-4.0.xsd
10??http://www.springframework.org/schema/mvc??
11??http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
2.自動掃描包名
?
讓其中的注解生效
?1?<!--?自動掃描的包名?-->
?
3.視圖解析器
JSP模板頁面用到了JSTL標簽庫
2?????<bean
3?????????class="org.springframework.web.servlet.view.InternalResourceViewResolver">
4?????????<property?name="viewClass"
5?????????????value="org.springframework.web.servlet.view.JstlView"?/>
6?????????<property?name="prefix"?value="/WEB-INF/views/"?/>
7?????????<property?name="suffix"?value=".jsp"?/>
8?????</bean>
?4.自動注冊
這個好用啦,會自動注冊DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter 兩個bean
一開始沒加也能用呢Q^Q,時好時壞報錯?No mapping found for HTTP request with URI [xxx]?in DispatcherServlet with name 'springMVC'?就查到缺這個了。為什么呢?
?
(寫到這里我已經心累了)
?
?
5.靜態資源映射
?
本來可以介紹一番,但是截圖上傳圖片太麻煩了(可能會上傳PPT,可以參考
2?????<mvc:resources?location="/resources/"?mapping="/resources/**"?/>
6.靜態資源處理?
也可以把靜態資源轉交給web容器處理。
淚奔,添加他是因為忽然又報錯,No mapping found for HTTP request with URI……
然而剛剛試著刪掉還是能用呢,不能理解?
?1?<mvc:default-servlet-handler/>
7.攔截器
??1?<!--?攔截器?-->
?3?????????<mvc:interceptor>
?4?????????????<mvc:mapping?path="/user/**"?/>
?5?????????????<bean?class="interceptor.UserLoginInterceptor"?/>
?6?????????</mvc:interceptor>
?7?????????<mvc:interceptor>
?8?????????????<mvc:mapping?path="/admin/**"?/>
?9?????????????<bean?class="interceptor.AdminLoginInterceptor"?/>
10?????????</mvc:interceptor>
11?????</mvc:interceptors>
以上代碼用來對未登錄用戶做權限管理,沒啥好說的,由于只用到了預處理方法,也可以用過濾器來實現。但是攔截器和過濾器還是不一樣的,感覺它的postHandle方法是過濾器做不到的。(沒有用過,瞎猜的。
?
忘記了來源的圖片?
?