Spring mvc 學習開發筆記

當學習一個mvc框架步驟:
    1,環境搭建 實現 helloworld
    2,如何傳遞參數到Controller
    3,如何從控制器獲取參數
    4,如何完成文件的上傳
    5,如何完成驗證
    6,如何處理異常
    7,深入學習一些原理,和源代碼的學習

1,本筆記學習基於spring的jar包
    spring-framework-3.1.0.RC2-with-docs
2,spring中的幾個概念
    Dispatcher servlet 中央控制器 轉發器
    Handler Mapping 映射處理器
    Controller 控制器
    ViewResolver  視圖轉換器
    View 視圖                   瞭解即可
3,環境搭建
    一,創建 web project
    二,拷貝 上述文件中dist文件下的所有jar包到lib目錄下
    三,拷貝 common-logging 1.1.1 .jar
    四,配置Dispatcher Servlet
         在web.xml中 可以參考 doc下的幫助文檔
            <servlet>
                <servlet-name>hello</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                <load-on-startup>1</load-on-startup>
            </servlet>

            <servlet-mapping>
                <servlet-name>hello</servlet-name>
                <url-pattern>/</url-pattern>
            </servlet-mapping>
            注意在此配置路徑的時候要使用/ 不能寫成/*  /*攔截所有的請求
    五,在WEB-INF下創建spring的配置文件 hello-servlet.xml
        注意配置文件的名字和servlet的名字是由關聯的
        配置文件的內容如下,參考幫助文檔
        <?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:p="http://www.springframework.org/schema/p"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
           
            <!-- 打開spring mvc支持 -->
            <mvc:annotation-driven />
            <!-- 掃描包的路徑 -->
            <context:component-scan base-package="org.springframework.samples.petclinic.web"/>

            <!-- ... -->

        </beans>
    六,創建Controller
        @Controller
        public class HelloContoller{
            @RequestMappging(value={"/","/hello"})
            public String hello () {
                return "hello";
            }
        }
    七,配置ViewResolvers試圖轉換器
            <bean id="jspViewResolver" 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>
            視圖控制器解釋
                return "hello";
                "/WEB-INF/jsp/" + "hello" + ".jsp" = "/WEB-INF/jsp/hello.jsp"
4,訪問路徑配置
    @RequestMapping(value = "/hello")
    @RequestMapping(value = {"/","/hello"})
5,參數的傳遞
    在要訪問的方法中添加參數
    一,@RequestParam("userId") int id
    二,@RequestParam int id
    三,int id
    參數傳遞到頁面
    一,Map<String,Object> map
        map.put("hello","haha");
         ${hello}
    二,Model model 推薦
        model.addAttribute("hello","value");
        //使用object類型作爲key integer
        model.addAttribute(23);
6,怎麼獲取request和response
    直接添加參數
    HttpServletRequest request;
    HttpServletResponse response;
7,文件上傳
    用到時候現查即可
   
   
   
   
   
   
   
           
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章