JavaMVC框架之SpringMVC

歡迎查看Java開發之上帝之眼系列教程,若是您正在爲Java後端龐大的體系所困擾,若是您正在爲各類繁出不窮的技術和各類框架所迷茫,那麼本系列文章將帶您窺探Java龐大的體系。本系列教程但願您能站在上帝的角度去觀察(瞭解)Java體系。使Java的各類後端技術在你心中模塊化;讓你在工做中能將Java各個技術瞭然於心;可以即插即用。本章咱們來一塊兒瞭解Java的MVC框架之SpringMVC。html

 

Java語言如今應用比較多的MVC框架有SpringMVC,Struts2兩種。本章咱們就來說解SpringMVC,對於本篇SpringMVC本篇不會介紹其詳細知識點及具體應用,Spring系列詳細使用和高級應用相關知識都在第四章:Spring專欄進行講解,本篇將會介紹SpringMVC的基礎相關概念,這些基礎概念是很重要的,它就如同蓋房子的基石。本章示例源碼下載前端

 

什麼是MVC?java

不可免俗地我仍是想在本章開始與你們一塊兒回顧一下什麼是MVC?MVC其實就是一種軟件的設計模式。在開發中並無強制咱們必須去遵循這種設計模式,可是遵循MVC模式會使咱們系統層次更清晰;職責更明確;擴展性更強;耦合度下降。
什麼是SpringMVC?
SpringMVC就是一個嚴格遵循MVC設計模式的框架。說其是一個框架,那麼SpringMVC框架中就應該有與Model,View,Controller相對應的組件;分別是Model模型對象,視圖解析器,Controller控制器。
Spring MVC屬於SpringFrameWork的後續產品,SpringMVC對於構建WEB項目而言是可選的,若是你使用Spring框架,你能夠選擇使用SpringMVC或者集成其餘MVC框架。同時Spring MVC分離了控制器、模型對象、過濾器以及處理程序對象的角色,這種分離讓它們更容易進行定製。
爲何使用SpringMVC?
  1. 學習門檻低,容易上手
  2. SpringMVC繼承了Spring框架的靈活性,容易擴展
  3. Spring將控制器,模型對象,過濾器等分離,使組件之間鬆耦合
  4. 支持多種視圖
  5. 輕鬆使用Spring生態下的其餘組件
SpringMVC請求流程
  1. 用戶發起請求到DispatchServlet
  2. Handler Mapping匹配請求信息的Handler(匹配條件:請求路徑,方法,header信息)
  3. HandlerMapping向DispatchServlet返回handler,返回過程執行攔截器鏈(一系列攔截器Interceptor)
  4. 請求HandlerAdapter執行Handler
  5. HandlerAdapter根據Handler類型執行不一樣的Handler(處理器)
  6. Handler執行完畢返回給HandlerAdapter(處理器適配器)ModelAndView對象
  7. HandlerAdapter將ModelAndView返回給DispatchServlet
  8. DispatchServlet請求ViewResolver解析ModelAndView
  9. ViewResolver向DispatchServlet返回View
  10. DispatchServlet將View和模型數據進行視圖渲染
  11. DispatchServlet向用戶響應結果
SpringMVC經常使用組件
前端控制器DispatcherServlet
接受請求,響應結果,至關於轉發器,中央處理器,減小了與其餘組件之間的耦合度
處理器映射器HandlerMapping
根據規則查找須要執行的Handler
處理器適配器HandlerAdapter
根據Handler類型,尋找相應處理器執行Handler
處理器 Handler
能夠實際處理請求的方法,如被@RequestMapping標註的方法
視圖解析器 ViewResolver
進行視圖解析,根據邏輯視圖名解析成真正的視圖(view)
視圖View
View是一個接口,實現類支持不一樣的view類型(jsp,framemark,pdf…)
文件上傳解析器MultipartResolver
 用於處理上傳請求

配置SpringMVC

典型的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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


    <!--加載數據字典-->
    <context:property-placeholder location="classpath:resource.properties"/>

    <!--配置默認的Servlet做爲靜態資源的Handler-->
    <mvc:default-servlet-handler/>

    <!-- 註解驅動:做用:替咱們自動配置最新版的註解的處理器映射器和處理器適配器-->
    <mvc:annotation-driven/>

    <!-- 配置@Controller註解掃描 -->
    <context:component-scan base-package="com.jimisun.controller"></context:component-scan>

    <!--配置InternalResourceViewResolver視圖解析器解析返回數據-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="cache" value="false"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
        <property name="contentType" value="text/html;charset=UTF-8"/>
    </bean>

    <!--配置自定義exceptionResolver異常解析器處理異常-->
    <bean id="exceptionResolver" class="com.jimisun.exception.MyExceptionResolver"/>

    <!--配置文件上傳-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="104857600"/>
        <property name="maxInMemorySize" value="4096"/>
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>

    <!--配置攔截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <!--攔截路徑規則-->
            <mvc:mapping path="/user/**"/>
            <!--排除路徑-->
            <mvc:exclude-mapping path="/user/login.html"></mvc:exclude-mapping>
            <mvc:exclude-mapping path="/user/register.html"></mvc:exclude-mapping>
            <bean id="viewLoginInterceptor" class="com.jimisun.interceptor.ViewLoginInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

</beans>
從Servlet3.0開始,能夠徹底脫離XML對SpringWeb項目進行配置
/**
 * @Author:jimisun
 * @Description:
 * @Date:Created in 19:42 2018-09-28
 * @Modified By:
 */
public class MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {


    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

SpringMVC單元測試 單元測試示例源碼下載 

/**
 * @Author:jimisun
 * @Description:
 * @Date:Created in 07:58 2018-09-25
 * @Modified By:
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:SpringMvc.xml",
})
public class TestControllerTest {

    @Autowired
    private TestService testService;

    @Test
    public void sayhello() {
        String test = testService.sayHelloService("test");
        System.out.println(test);
    }
}

驗證Web請求參數

對於Controller接收到的參數,幾乎在全部狀況下咱們都須要驗證,SpringMVC開發中經常使用的驗證方法有兩種,Spring框架定義Validator校驗JSP-303 Bean Validation校驗。git

Spring Validator參數校驗 Spring Validator參數示例源碼下載
    @RequestMapping("sayhello")
    public void sayhello(@Validated User user, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
        } else {
            String s = testService.sayHelloService(user.getUsername());
            System.out.println(s);
        }

JSP-303 Bean Validation校驗 Bean Validation校驗源碼下載github

Spring在3.1的時候增長了對JSP-303 Bean Validation規範的支持,不只能夠對SpringMVC進行校驗,還能夠對Hibernate的對象存儲進行校驗,是一個通用的校驗框架,在開發中咱們都是用JSP-303 Bean Validation進行校驗,也是我的推薦使用的。
hibernate-validator依賴
<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.4.Final</version>
</dependency>

相關配置web

<mvc:annotation-driven validator="validator"/>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
        <property name="validationMessageSource" ref="messageSource"/>
    </bean>
    <!-- 校驗錯誤信息配置文件 -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <!-- 資源文件名 -->
        <property name="basename" value="classpath:validationMessages"/>
        <!-- 對資源文件內容緩存時間,單位秒 -->
        <property name="fileEncodings" value="GBK"/>
        <property name="defaultEncoding" value="GBK"/>
        <property name="cacheSeconds" value="120"/>
    </bean>
在接收參數時使用 @Valid進行校驗
    @RequestMapping("sayhello")
    public void sayhello(@Valid User user, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
        } else {
            String s = testService.sayHelloService(user.getUsername());
            System.out.println(s);
        }
    }
使用下面註解對參數進行約束
Bean Validation 中內置的 constraint
註解
做用
@Valid
被註釋的元素是一個對象,須要檢查此對象的全部字段值
@Null
被註釋的元素必須爲 null
@NotNull
被註釋的元素必須不爲 null
@AssertTrue
被註釋的元素必須爲 true
@AssertFalse
被註釋的元素必須爲 false
@Min(value)
被註釋的元素必須是一個數字,其值必須大於等於指定的最小值
@Max(value)
被註釋的元素必須是一個數字,其值必須小於等於指定的最大值
@DecimalMin(value)
被註釋的元素必須是一個數字,其值必須大於等於指定的最小值
@DecimalMax(value)
被註釋的元素必須是一個數字,其值必須小於等於指定的最大值
@Size(max, min)
被註釋的元素的大小必須在指定的範圍內
@Digits (integer, fraction)
被註釋的元素必須是一個數字,其值必須在可接受的範圍內
@Past
被註釋的元素必須是一個過去的日期
@Future
被註釋的元素必須是一個未來的日期
@Pattern(value)
被註釋的元素必須符合指定的正則表達式
Hibernate Validator 附加的 constraint
註解
做用
@Email
被註釋的元素必須是電子郵箱地址
@Length(min=, max=)
被註釋的字符串的大小必須在指定的範圍內
@NotEmpty
被註釋的字符串的必須非空
@Range(min=, max=)
被註釋的元素必須在合適的範圍內
@NotBlank
被註釋的字符串的必須非空
@URL(protocol=,
host=,    port=, 
regexp=, flags=)
被註釋的字符串必須是一個有效的url
@CreditCardNumber
被註釋的字符串必須經過Luhn校驗算法,
銀行卡,信用卡等號碼通常都用Luhn
計算合法性

Java開發之上帝之眼系列教程其餘文章

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

勘誤&感謝

  本系列文章資料來源不少出自於互聯網和在下自己的看法,受限於我的技術能力水平和其餘相關知識的限制,相關看法錯誤或者資料引用錯誤請各位幫助留言校訂!引用資料多來自於互聯網,在下在引用前會遵循各位前輩或者博主的引用說明表示感謝,但互聯網資料可能是轉發再轉發或存在遺漏請原做者內信聯繫指正。

相關文章
相關標籤/搜索