創建一個springMVC項目總結


創建一個springMVC項目總結

1.如何搭建起一個Spring mvc的環境並運行程序。

  參考:http://www.cnblogs.com/bigdataZJ/p/springmvc1.html

  1.新建項目:File-New-Other,選擇Dynamic web project

  

  2.項目建好之后,目錄結構如下:

  

  3.導入jar包。我們基於Spring mvc框架進行開發,需要依賴一下的spring jar包:

  

  4.配置web.xml文件

  

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     id="WebApp_ID" version="2.5">
 6     
 7     <!-- 配置DispatchcerServlet -->
 8     <servlet>
 9         <servlet-name>springDispatcherServlet</servlet-name>
10         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
11         <!-- 配置Spring mvc下的配置文件的位置和名稱 -->
12         <init-param>
13             <param-name>contextConfigLocation</param-name>
14             <param-value>classpath:springmvc.xml</param-value>
15         </init-param>
16         <load-on-startup>1</load-on-startup>
17     </servlet>
18     
19     <servlet-mapping>
20         <servlet-name>springDispatcherServlet</servlet-name>
21         <url-pattern>/</url-pattern>
22     </servlet-mapping>
23     
24 </web-app>

 

   

注意:1. line12-15用於配置spring mvc的配置文件的位置和名稱,這里說明會新建一個springmvc.xml的配置文件

   2. 我們也可以不新建springmvc.xml,而是用默認的,默認的配置文件格式為/WEB-INF/[servlet-name]-servlet.xml,對應這里的就是springDispatcherServlet-servlet.xml

   3. 這里的servlet-mapping表示攔截的模式,這里是“/”,表示對於所有的請求的攔截,包括靜態資源如html, js, jpg等。這時候對於靜態資源的訪問就會報404的錯誤。關於如何解決后面會介紹

  

  5.Springmvc.xml(scr下)在src目錄下新建springmvc.xml

  

 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.0.xsd
 8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 9         
10         
11         <!-- 配置自動掃描的包 -->
12         <context:component-scan base-package="com.jackie.springmvc"></context:component-scan>
13         
14         <!-- 配置視圖解析器 如何把handler 方法返回值解析為實際的物理視圖 -->
15         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
16             <property name = "prefix" value="/WEB-INF/views/"></property>
17             <property name = "suffix" value = ".jsp"></property>
18         </bean>
19 </beans>

  

注意:1. line12表示spring監聽的范圍,這里是在com.jackie.springmvc下

   2. line15-18,是添加了一個視圖解析器,用於把在控制器中handler的結構解析為實際的物理視圖,這個要配合controller類來解析,詳見下面。

  6.HelloWorld.java(com.jackie.springmvc.handlers下)

 1 package com.jackie.springmvc.handlers;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 @Controller
 7 public class HelloWorld {
 8 
 9     /**
10      * 1. 使用RequestMapping注解來映射請求的URL
11      * 2. 返回值會通過視圖解析器解析為實際的物理視圖, 對於InternalResourceViewResolver視圖解析器,會做如下解析
12      * 通過prefix+returnVal+suffix 這樣的方式得到實際的物理視圖,然后會轉發操作
13      * "/WEB-INF/views/success.jsp"
14      * @return
15      */
16     @RequestMapping("/helloworld")
17     public String hello(){
18         System.out.println("hello world");
19         return "success";
20     }
21 }

 

 

注意:1. 首先要在類的前面添加“Controller”注解,表示是spring的控制器,這里會寫一個方法hello()

   2. hello方法上方有一個@RequestMapping, 是用於匹配請求的路徑,比如這里匹配的請求路徑就是“http://localhost:8080/springTest/springmvc/helloworld”,即當tomcat服務啟動后,在瀏覽器輸入這個url時,如果在這個方法打斷點了,就會跳入該方法。

   3. 這個return的結果不是亂寫的,這個返回的字符串就是與上面springmvc.xml中line15-18進行配合的,springmvc.xml中聲明了prefix和suffix,而夾在這兩者之間的就是這里返回的字符串,所以執行完這個方法后,我們可以得到這樣的請求資源路徑“/WEB-INF/views/success.jsp”,這個success.jsp是需要我們新建的

 

  7.index.jsp(WebContent下)

  在新建success.jsp之前,我們需要有一個入口,也就是這里的index.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 
11 <a href="helloworld">hello world</a>
12 
13 </body>
14 </html>

當訪問index.jsp時,頁面上會展示一個超鏈接,點擊超鏈后,url中的地址就會發生跳轉,由“http://localhost:8080/springTest/index.jsp”跳轉到“http://localhost:8080/springTest/helloworld”,而這個url請求就會進入HelloWorld中的hello方法,因為其與該方法上的“/helloworld”匹配。

  8.success.jsp(WEB-INF/views下)  該頁面是作為請求成功后的相應頁面

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM