1引言:這裡主要做三件事 1.1resources文件夾下創建spring-mvc.xml並配置:開啟註解驅動(mvc:annotation-driven),靜態資源過濾(mvc:default-servlet-handler),視圖解析器(InternalResourceViewResolver) ...
1引言:這裡主要做三件事
1.1resources文件夾下創建spring-mvc.xml並配置:開啟註解驅動(mvc:annotation-driven),靜態資源過濾(mvc:default-servlet-handler),視圖解析器(InternalResourceViewResolver),掃描web相關的bean,既掃描controller包下的東西(context:component-scan base-package="xxx")
1.2配置resources文件夾下的applicationContext.xml,這裡將spring-dao.xml,spring-service.xml,spring-mvc.xml進行整合
1.3配置web/WEB-INF文件夾下的web.xml文件:servlet(DispatchServlet)和對應的servlet-mapping,亂碼過濾(CharacterEncodingFilter)
2步驟:
2.1在resources文件夾新建spring-mvc.xml問價,代碼如下:其中像註解驅動,靜態資源過濾,視圖解析器這些都是固定的,在視圖解析器中prefix代表路徑的首碼,suffix代表路徑的尾碼,假設controller層返回了一個字元串為“book”(註意:這個controller不是@RestController而是@Controller,如果是@RestController最後返回字元串的話界面僅僅會顯示你返回的字元串,@Controller類型的controller層才會對返回的字元串通過視圖解析器進行路徑拼接)並且使用的是@Controller註解,那麼通過下方代碼中的視圖解析器會將字元串拼接為/WEB-INF/jsp/book.jsp
<?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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--註解驅動--> <mvc:annotation-driven/> <!--靜態資源過濾--> <mvc:default-servlet-handler/> <!--掃描controller包--> <context:component-scan base-package="com.xiaoma1.controller"/> <!--視圖解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
2.2在創建mybatis層的時候我記得在resources文件夾下已經新建過applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring-dao.xml"/> <import resource="spring-service.xml"/> <import resource="spring-mvc.xml"/> </beans>
2.3配置web.xml,這裡要註意,servlet標簽中的init-param標簽裡邊的value一定要是applicationContext.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_4_0.xsd" version="4.0"> <!--配置DispatchServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.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> <!--配置亂碼過濾--> <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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--session過期時間--> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>
至此底層配置結束,下麵就編寫controller和相應的jsp或者html界面就好了,下一節通過controller編寫的介面實現具體的增刪改查功能