1. 程式人生 > 其它 >SpringMVC(7)-ssm整合實現增刪改查-SpringMVC層

SpringMVC(7)-ssm整合實現增刪改查-SpringMVC層

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編寫的介面實現具體的增刪改查功能