SpringMvc基礎入門使用

引語:SpringMvc作爲一個前端框架來說最終要的功能莫過於:從頁面接收數據——–從後端返回數據;
一,導包(SpringMvc獨立運行的包有15個)
這裏寫圖片描述
二,配置SpringMvc的前端控制器到web.xml中

 <!-- 前端控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置springMvc需要加載的核心配置文件 ,如果沒有的話就回去/WEB-INF/springmvc-servlet.xml -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!-- 設置tomcat在一啓動的時候就加載這個servlet -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

三,增加SpringMvc框架的核心配置文件springmvc.xml到項目中。

  1. 如果你在配置前端控制器的時候沒有指定SpringMvc的核心配置文件的位置以及名稱,SpirngMvc框架會自動在/WEB-INF/目錄下尋找springmvc-servlet.xml文件。
  2. 自己在web.xml配置前端控制器的時候配置標籤,使SpringMvc框架尋找到核心配置文件。
<!--簡單SpringMvc的核心配置文件模板-->
<?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:dubbo="http://code.alibabatech.com/schema/dubbo" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://code.alibabatech.com/schema/dubbo 
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">



     <!-- 配置@controller的註解掃描的路徑 --> 
     <!--如果你註解開發必須要配置此項,base-package參數是你的controlelr包地址, 多個包中間使用半角逗號分隔--> 
    <context:component-scan base-package="cn.redrat.controller"></context:component-scan>      
</beans>

四,編寫controller包下的java類(提供模板如下)


//註解:標識這是一個controller類 SpringMvc會在包掃描的時候將該類添加到內存當中
@Controller
public class ItemList {

    //指定url到請求方法的映射
    //url中輸入一個地址,找到這個方法.例如:localhost:8081/springmvc0523/list.action
    @RequestMapping("/list")
    public ModelAndView  itemsList() throws Exception{
        List<Items> itemList = new ArrayList<>();

        //商品列表
        Items items_1 = new Items();
        items_1.setName("聯想筆記本_3");
        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蘋果手機!");

        itemList.add(items_1);
        itemList.add(items_2);

        //模型和視圖
        //model模型: 模型對象中存放了返回給頁面的數據
        //view視圖: 視圖對象中指定了返回的頁面的位置
        ModelAndView modelAndView = new ModelAndView();

        //將返回給頁面的數據放入模型和視圖對象中
        modelAndView.addObject("itemList", itemList);

        //指定返回的頁面位置
        modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");

        return modelAndView;

    }



}

五,測試訪問路徑(本文給出的路徑爲http://lcoalhost:8080/helloSpringMvc/list.action

  1. 訪問結尾是.action
  2. 因爲只有*.action的SpringMvc框架纔會攔截,這取決於你在web.xml中配置SpringMvc前端控制器的url-pattern攔截的地址。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章