日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

(二)spring MVC配置

發布時間:2023/12/10 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (二)spring MVC配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用Maven添加依賴的jar包

?

?第一個還沒用上

剛開始沒加spring-context,@Controller沒法用

?

web.xml配置

1.? ? ???配置DispatcherServlet?

<servlet>
????????<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?<!--?過濾器?-->

?2?<filter>
?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配置

剛開始忽好忽壞的,忘記報啥錯了……查了查是這個的問題,缺一不可呀

?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?<!--?自動掃描的包名?-->

2?<context:component-scan?base-package="controller"?/>

?

3.視圖解析器

JSP模板頁面用到了JSTL標簽庫

1?<!--?視圖解析器?-->
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'?就查到缺這個了。為什么呢?

1?<mvc:annotation-driven?/>


?

(寫到這里我已經心累了)

?

?

5.靜態資源映射

?

本來可以介紹一番,但是截圖上傳圖片太麻煩了(可能會上傳PPT,可以參考

1?<!--?對靜態資源文件的訪問?-->
2?????<mvc:resources?location="/resources/"?mapping="/resources/**"?/>



6.靜態資源處理?

也可以把靜態資源轉交給web容器處理。

淚奔,添加他是因為忽然又報錯,No mapping found for HTTP request with URI……

然而剛剛試著刪掉還是能用呢,不能理解?

?1?<mvc:default-servlet-handler/>


7.攔截器

??1?<!--?攔截器?-->

?2?????<mvc:interceptors>
?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方法是過濾器做不到的。(沒有用過,瞎猜的。

?

忘記了來源的圖片?

?

轉載于:https://www.cnblogs.com/imirror707/p/springMVC2.html

總結

以上是生活随笔為你收集整理的(二)spring MVC配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。