SpringMVC的基礎知識整理(2)快速入門程序

3、springmvc入門程序

    早期springmvc用非直接配置映射器、適配器、處理器啥的,十分的不方便;後來都使用註解來快速開發,簡單方便,簡化開發。
添加依賴的jar包,springmvc版本:spring3.2,需要spring3.2所有jar(一定包括spring-webmvc-3.2.0.RELEASE.jar)

    3.1、前端控制器還是要在web.xml中配置:

<!-- springmvc前端控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- contextConfigLocation配置springmvc加載的配置文件(配置處理器映射器、適配器等等) 
                如果不配置contextConfigLocation,默認加載的是/WEB-INF/servlet名稱-serlvet.xml(springmvc-servlet.xml) -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

       <!-- 第一種:*.action,訪問以.action結尾 由DispatcherServlet進行解析 
            第二種:/,表示所有訪問的地址都由DispatcherServlet進行解析,對於靜態文件的解析需要配置不讓DispatcherServlet進行解析,使用此種方式可以實現 RESTful風格的url 
            第三種:/*,這樣配置不對,使用這種配置,最終要轉發到一個jsp頁面時,仍然會由DispatcherServlet解析jsp地址,不能根據jsp頁面找到handler,會報錯。 -->

3.2、在spring容器中配置註解映射器適配器
    在spring3.1之前使用
        org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping 註解映射器。
        org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter 註解適配器。
    在spring3.1之後使用
        org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping 註解映射器。
        org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter 註解適配器。

    但是,上面的配置依舊還是麻煩,然後就產生了使用標籤來配置
<!--使用 mvc:annotation-driven 代替上邊註解映射器和註解適配器配置,mvc:annotation-driven 默認加載很多的參數綁定方法,比如json轉換解析器就默認加載了,如果使用mvc:annotation-driven,則不用配置上邊的RequestMappingHandlerMapping和RequestMappingHandlerAdapter,實際開發時使用mvc:annotation-driven -->
    
<mvc:annotation-driven></mvc:annotation-driven>

3.3、開發Handler

//使用Controller標識 它是一個控制器
    @Controller
    @RequestMapping("/itemsController3")
    public class ItemsController3 {
        //商品查詢列表
        //@RequestMapping實現 對queryItems方法和url進行映射,一個方法對應一個url

        @RequestMapping("/queryItems")
        public ModelAndView queryItems()throws Exception{
            //調用service查找 數據庫,查詢商品列表,這裏使用靜態數據模擬
            List<Items> itemsList = new ArrayList<Items>();
            //向list中填充靜態數據
            Items items_1 = new Items();
            items_1.setName("聯想筆記本");
            items_1.setPrice(6000f);
            items_1.setDetail("ThinkPad T430 聯想筆記本電腦!");
            Items items_2 = new Items();
            items_2.setName("蘋果手機");
            items_2.setPrice(5000f);
            items_2.setDetail("iphone6蘋果手機!");
            itemsList.add(items_1);
            itemsList.add(items_2);
            
            //返回ModelAndView
            ModelAndView modelAndView =  new ModelAndView();
            //相當 於request的setAttribut,在jsp頁面中通過itemsList取數據
            modelAndView.addObject("itemsList", itemsList);
            //指定視圖
            modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
            
            return modelAndView;
        }
    }

3.4、在spring容器中加載Handler

<!-- 對於註解的Handler可以單個配置bean -->
    <bean class="cn.itcast.ssm.controller.ItemsController3" />
    <!-- 實際開發中建議使用組件掃描,可以掃描controller、service 等,這裏讓掃描c指定controller的包-->
    <context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>

3.5、在spring容器中配置視圖解析器,並編寫視圖 items/itemsList.jsp

<!-- 視圖解析器,解析jsp解析,默認使用jstl標籤,classpath下的得有jstl的包  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

3.6、部署調試
    瀏覽器訪問:http://localhost:8080/springmvcfirst1208/itemsController3/queryItems.action

入門程序小結

通過入門程序理解springmvc前端控制器、映射器、適配器、Handler處理器、視圖解析器用法。
前端控制器配置:
    第一種:*.action,訪問以.action結尾 由DispatcherServlet進行解析
    第二種:/,所以訪問的地址都由DispatcherServlet進行解析,對於靜態文件的解析需要配置不讓DispatcherServlet進行解析,使用此種方式可以實現 RESTful風格的url

在spring容器springmvc.xml中配置映射器、適配器、Handler處理器、視圖解析器用法:

<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"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- springmvc映射器和適配器 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <!-- 掃描handler包,即controller -->
    <context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>

    <!-- 視圖解析器
    解析jsp解析,默認使用jstl標籤,classpath下的得有jstl的包  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

 

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