Spring MVC 的環境搭建和入門小程式

來源:互聯網
上載者:User

標籤:圖解   line   -o   ...   cal   color   環境   version   參數   

1.1.下載spring架構套件。1.1.1百度搜尋Spring Framework。

進入spring官網,在網頁右邊選擇想要下載的版本。

1.1.2進入頁面按Ctrl+F搜尋Distribution Zip Files

 

 

找到頁面,點擊地址。來到下載面

連結地址為:http://repo.spring.io/release/org/springframework/spring/

Spring源碼:https://github.com/spring-projects/spring-framework

如:

 

然後選擇想要下載的版本就可以了。

1.2.添加jar包到工程項目中

解壓下載的zip包,得到如下包結構

 

將libs目錄下的jar檔案複製一份到工程目錄,WEB-INF/lib目錄下。最重要的別忘了把commons-logging-1.2.jar的包倒進去,這個包需要另外下載,沒有這個包的話,項目部署到Tomcat中會起不起來的,

在我雲端硬碟裡面有,提供給大家:連結:http://pan.baidu.com/s/1eSMdt2U 密碼:vsfc

如:

 

1.3.配置web.xml檔案

Web.xml配置內容如下:

 

 1 <!-- 配置DispatcherServlet 攔截所有的使用者請求 --> 2 <servlet> 3     <servlet-name>springDispatcherServlet</servlet-name> 4     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <!-- 配置DispatcherServlet 的初始化參數:配置Spring MVC設定檔的位置和名稱 --> 6     <init-param> 7         <param-name>contextConfigLocation</param-name> 8         <!--這是設定檔的路徑,是放在src檔案下的--> 9         <param-value>classpath:springmvc.xml</param-value>10     </init-param>11     <load-on-startup>1</load-on-startup>12 </servlet>13 <servlet-mapping>14         <servlet-name>springDispatcherServlet</servlet-name>15         <url-pattern>/</url-pattern>16 </servlet-mapping>

 

在eclipse中配置了SpringIDE外掛程式。按Alt+/可以自動添加dispatcher的相關配置。以上就是自動產生的。我們只需要修改url和設定檔為準和名稱即可。

在<init-param.../>標籤對中,定義了Spring mvc設定檔的位置和名稱。在不聲明此標籤對的情況下,spring的預設設定檔路徑是/WEB-INF/<servlet-name>-servlet.xml。此處的代碼中的<servlet-name>名為springDispatcherServlet,所有設定檔的名稱就應該為springDispatcherServlet-servlet.xml。並且應該放在WEB-INF目錄下。

1.4.配置Spring MVC設定檔

Spring的設定檔名稱和存放路徑應該和web.xml設定檔中聲明的一致。未聲明檔案目錄的,一般放於src目錄下。

Spring設定檔如下:

 

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4     xmlns:context="http://www.springframework.org/schema/context" 5     xmlns:mvc="http://www.springframework.org/schema/mvc" 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> 9     <!-- 配置掃描的自訂的包 -->10     <context:component-scan base-package="org.cjh.Demo"></context:component-scan>11     <!-- 配置視圖解析器:如何把handler方法傳回值解析為實際的物理視圖 -->12     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">13         <property name="prefix" value="/WEB-INF/content/"></property>14         <property name="suffix" value=".jsp"></property>15     </bean>16 </beans>

 

此處設定檔中,用到了<context:.../>標籤對,我們在建xml檔案的時候需要把beans檔案路徑添加進去。

下面的property參數,分別聲明的了物理頁面的首碼(檔案路徑)和尾碼(尾碼名.jsp)

 

1.使用@RequestMapping()註解映射請求的URL

2.傳回值會通過視圖解析器為實際的物理視圖,對於 InternalResourceViewResolver內部資源檢視解析器,會做如下解析:

3.在設定檔中:prefix的值+RequestMapping()的傳入值+suffix 就等於物理頁面的路徑。

解析出來後的路徑就是/WEB-INF/content/hello.jsp

 

 

 

 

1.5.編寫處理請求的處理器

處理器需要在開頭類的開頭聲明@Controller來告訴設定檔,我是一個處理器。然後在接受請求的方法前面聲明@RequestMapping("/action")做請求映射,當請求接受到了action請求就會執行該方法。

控制器代碼如下代碼如下:

 

 1 @Controller 2 public class hello { 3     /** 4      * 1.使用@RequestMapping()註解映射請求的URL 5      * 2.傳回值會通過視圖解析器為實際的物理視圖,對於InternalResourceViewResolver內部資源檢視解析器,會做如下解析: 6      * 在設定檔中:prefix的值+RequestMapping()的傳入值+suffix 就等於物理頁面的路徑。 7      * /WEB-INF/content/hello.jsp 8      * @return 9      */10     @RequestMapping("/action")11     public String helloworld(){12         System.out.println("Hello World");13         return "hello";14     }15 }

 

這個類聲明了controller控制器,並且為方法聲明了requestionmapping來處理請求資訊。這個資訊返回的“hello”字串代表的是物理視圖名稱。詳見代碼中的注釋。

 

1.6.編寫視圖頁面

建立一個jsp頁面然後在<body>...</body>標籤對內填寫如下連結。

1 <a href="action">驗證登入</a>

在index.jsp頁面中聲明的頁面請求參數為action,對應了處理器處理的請求參數,所有這個頁面會交個相應處理器處理。並且跳轉相應頁面。

工程目錄如:

訪問路徑為:http://localhost:8080/Spring_test_01/action

訪問路徑格式為:伺服器名稱/工程路徑名/請求參數

 

Spring MVC 的環境搭建和入門小程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.