Springmvc簡單使用流程圖(使用的大概印象)

來源:互聯網
上載者:User

標籤:前端   資料   artifact   name   標籤   servlet   --   整數   instance   

一.Springmvc簡單使用流程圖

1.1  Maven項目配置依賴(Spring全家桶+Tomcat運行外掛程式)

<dependencies>

     <!-- springmvc依賴包 -->

     <dependency>

         <groupId>org.springframework</groupId>

         <artifactId>spring-core</artifactId>

         <version>${spring.version}</version>

     </dependency>

     <dependency>

         <groupId>org.springframework</groupId>

         <artifactId>spring-web</artifactId>

         <version>${spring.version}</version>

     </dependency>

     <dependency>

         <groupId>org.springframework</groupId>

         <artifactId>spring-tx</artifactId>

         <version>${spring.version}</version>

     </dependency>

     <dependency>

         <groupId>org.springframework</groupId>

         <artifactId>spring-jdbc</artifactId>

         <version>${spring.version}</version>

     </dependency>

     <dependency>

         <groupId>org.springframework</groupId>

         <artifactId>spring-webmvc</artifactId>

         <version>${spring.version}</version>

     </dependency>

     <dependency>

         <groupId>org.springframework</groupId>

         <artifactId>spring-aop</artifactId>

         <version>${spring.version}</version>

     </dependency>

     <dependency>

         <groupId>org.springframework</groupId>

         <artifactId>spring-context-support</artifactId>

         <version>${spring.version}</version>

     </dependency>

     <!-- JSTL標籤類 -->

     <dependency>

         <groupId>jstl</groupId>

         <artifactId>jstl</artifactId>

         <version>${jstl.version}</version>

     </dependency>

     <dependency>

        <groupId>junit</groupId>

         <artifactId>junit</artifactId>

         <version>4.12</version>

         <scope>test</scope>

     </dependency>

</dependencies>

1.2 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: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-4.0.xsd

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

  <context:component-scan base-package="com.code.controller"/> <!--這裡需要配置controller的全包掃描-->

</beans>

1.3 web.xml的配置

 <servlet>

     <servlet-name>springmvc-first</servlet-name>

     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

     <!-- 載入springmvc主設定檔 -->

     <init-param>

         <param-name>contextConfigLocation</param-name>

         <param-value>classpath:springmvc.xml</param-value>

     </init-param>    

     <!-- 配置什麼時候載入前端控制器,說明:

         1.配置大於等於0的整數,表示web容器啟動的時候載入

         2.配置小於0的整數,表示在第一次請求到達的時候載入  -->

     <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

     <servlet-name>springmvc-first</servlet-name>

     <!-- 配置攔截的請求url規則,說明:

         1.*.do,表示以.do結尾的請求進入前端控制器

         2./,表示所有請求都進入前端控制器 -->

     <url-pattern>*.do</url-pattern>

  </servlet-mapping>

1.4  編寫頁面及控制器

幾個重要的點:

1.4.1   Controller 註解要加,說明是控制器。

1.4.2   ModelAndView:模型和視圖。用於設定響應的視圖;用於設定響應的模型資料

1.4.3   @RequestMapping:配置請求的url(頁面的請求)

1.4.4   mav.setViewName("/WEB-INF/jsp/hello.jsp");返回視圖的名稱

@Controller

public class HelloController {

/**

 * ModelAndView:模型和視圖。用於設定響應的視圖;用於設定響應的模型資料

 * @RequestMapping:配置請求的url

 */

@RequestMapping("/hello.do")

public ModelAndView hello(){

     // 1.建立ModelAndView對象

     ModelAndView mav = new ModelAndView();

     // 2.設定響應模型資料

     /**

      * addObject:設定模型資料

      * 參數:

      *      attributeName:模型名稱

      *      attributeValue:模型資料

      */

     mav.addObject("hello", "大家");

     // 3.設定響應的頁面

     /**

      * setViewName:設定響應的視圖

      * 參數:

      *      viewName:視圖名稱(是視圖的實體路徑) */

     mav.setViewName("/WEB-INF/jsp/hello.jsp");

     return mav;

}

建立hello.jsp頁面,直接通過.do 訪問就可以了。

Springmvc簡單使用流程圖(使用的大概印象)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.