SpringMVC的使用(一)

簡介

  1. springMVC是spring組件的一部分,springMVC基於spring

  2. MVC
    model 數據模型 (service entity dao)
    view 視圖 表現層(html,jsp,freemarker,xml,excel)
    controller 控制器 (servlet,springMVC,struts 1,2)

  3. 主要解決了控制層問題

基本構造

  1. 前端控制器 DispatcherServlet (中央控制器)

  2. 處理器 handler 處理用戶的業務

  3. 處理的結果返回前端控制器(DispatcherServlet根據處理的結果判斷,如果有頁面請求,返回視圖)

搭建

  1. 搭建spring spring要和web集成

  2. 配置前端控制器 DispatcherServlet
        a.配置url地址需要使用後綴名配置 *.action
        b.restful 分格配置 /
    springMVC會默認加載 WEB-INF/(servlet的名字)-servlet.xml 這個配置文件
        a.在servletContext中指定 mvc的配置文件
        b.在servletConfig中指定 mvc的配置文件

  3. 建立控制層 將控制層放到spring容器中 (掃包,打標記)

  4. 配置2個核心組件 處理器映射器,處理器適配器
    a.配置HandlerMapping 處理器映射器 (地址映射)
       在類上@RequestMapping 指定類的位置
       在方法上加 @RequestMapping指定方法的位置
    b.配置HandlerAdapter處理器適配器 處理業務
    c.同時配置2大組件可以使用註解驅動來配置 <mvc:annotation-driven></mvc:annotation-driven>

  5. 配置視圖解析器 org.springframework.web.servlet.view.InternalResourceViewResolver
    a.前綴和後綴 ,指定視圖的位置
    b.配置視圖的種類 默認是JSP視圖

DispatcherServlet

  1. 配置核心組件需要指定springMVC的配置文件

  2. 配置地址映射 後綴名*.action | / (restful風格)
    / 配置之後會出現 靜態資源文件不能被訪問的問題 (圖片,視頻,音頻,js文件,css文件)
        a.在springMVC的配置文件中加上 <mvc:default-servlet-handler />會對靜態資源攔截轉由默認Servlet進行處理
        b.在springMVC的配置文件中加上<mvc:resources location="/WEB-INF/public/" mapping="/resources/**"/>將location的目錄地址映射成mapping的地址進行訪問

處理器映射與適配

處理器映射器

  1. requestMapping 可以只指定方法 類可以省略(不建議)
    路徑不能重複 類的路徑不重複 同一個類中 方法的路徑不能重複

  2. 路徑可以帶/ 也可以帶.action 還可以多級目錄 也可以什麼都不帶

  3. requestMapping value可以指定數組 多路徑指向一個方法

  4. method 參數指定請求的類型 RequestMethod.GET POST …

處理器適配器 ==>調用handler

  1. 默認返回ModelAndView 如果只是爲了跳轉頁面 可以直接返回String

  2. 如果跳轉頁面
        轉發 默認使用視圖解析器前綴+後綴拼接地址 forward: 地址要寫全路徑 (視圖解析器前綴和後綴失效)(不推薦)
        重定向 redirect: 地址要寫全路徑 (視圖解析器前綴和後綴失效)

  3. 返回數據
        ModelAndView
        a.設置viewName 轉發的頁面
        b.設置ModelMap addAllObject() 發送到前臺的數據或者直接設置鍵值對 addObject()
        (原理 request.setAttribute())

  4. 支持原生態的servlet
        springMVC所有的方法自帶 HttpServletRequest request, HttpServletResponse response

亂碼問題

  1. 配置spring字符過濾器 org.springframework.web.filter.CharacterEncodingFilter

  2. 指定編碼爲UTF-8

參數接收

  1. 單個普通類型 支持request獲取
    支持直接指定方法參數

  2. 多個普通類型 支持封裝成一個參數Bean
    在使用springMVC參數映射的時候基本類型請全部使用包裝類

  3. 數組類型
    在後臺使用數組接收 單個參數或者封裝成對象裏面設置 都可以

  4. 集合類型
    a.在後臺要用集合接收
    b.接收的時候前臺的name屬性要指定下標
    c.指定下標後再通過 xx[flag].屬性 的方式設置內部的屬性

配置示例

web.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">


    <!--spring字符過濾器-->
    <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- spring的監聽器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--加載spring配置文件的地址-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- 前端控制器-->
    <servlet>
        <servlet-name>MVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mvc-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>MVC</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
</web-app>

mvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.igeek.action"></context:component-scan>

    <!--處理器映射器-->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />-->
    <!--處理器適配器-->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />-->
    <!--處理器驅動-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--默認是jsp的視圖-->
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <!-- 配置邏輯視圖的前綴 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 配置邏輯視圖的後綴 -->
        <property name="suffix" value=".jsp" />
        <!--前綴+視圖字符串+後綴-->
    </bean>


    <!-- 對靜態資源文件的訪問 方案一 (二選一) -->
    <mvc:default-servlet-handler />
    <!--<mvc:resources location="/WEB-INF/publicResources/" mapping="/resources/**"/>-->

</beans>
@Controller
@RequestMapping("/login")
public class LoginAction {

    @RequestMapping(value = {"xx","login"},method = RequestMethod.GET)
    public ModelAndView gotoLogin(HttpServletRequest request){
        ModelAndView view = new ModelAndView();
        view.setViewName("login");
        view.addObject("username","xiaowang");
        //業務代碼
        return view;
    }

    @RequestMapping("serv")
    public void testLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("username","小王");
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
    }

    @RequestMapping("param")
    public void getPara(UserBean user){
        System.out.println(user);
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章