高並發秒殺系統--SpringMVC整合


[SpringMVC運行流程]

 

 

[Handler注解映射技巧]

 

[請求方法的細節處理]

1.如何處理請求參數和方法參數的綁定?

2.如何限制方法接收的請求方式?

3.如何進行請求轉發和重定向?

4.如何給數據模型賦值?

5.如何返回JSON數據?

6.如何獲取cookie數據?

 

[SpringMVC的整合配置]

1.在web.xml中配置入口DispatcherServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0"
         metadata-complete="true">
    <!--用maven創建的web-app需要修改servlet的版本為3.1-->
    <!-- SpringMVC配置 -->
    <!-- 1:配置DispatcherServlet -->
    <servlet>
        <servlet-name>seckill-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置SpringMVC的Spring配置文件
            Mybatis -> Spring -> SpringMVC
         -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-*.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>seckill-dispatcher</servlet-name>
        <!-- 默認匹配所有的請求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

2.spring-web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">
    <!-- SpringMVC配置 -->
    <!-- 1:開啟SpringMVC注解模式 -->
    <!-- 簡化配置:
        (1)自動注冊DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
        (2)提供一些列功能:數據綁定,數字和日期的format @NumberFormat,@DateTimeFormat,
                xml,jsonm默認讀寫支持
     -->
    <mvc:annotation-driven/>

    <!-- 2:靜態資源默認servlet配置
        (1)加入對靜態資源的處理:js,gif,png
        (2)允許使用"/"做整體映射
     -->
    <mvc:default-servlet-handler/>

    <!-- 3:配置jsp 顯示ViewResovler -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 4:掃描web相關的bean -->
    <context:component-scan base-package="org.azcode.web"/>

</beans>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM