雖然作java好幾年了,可是用spring的時間並不長,如今準備基於Spring開發一個產品,在開發產品的同時,作一系列的實例,把Spring的使用過程記錄下來。html
涉及到的技術會包括Spring和Spring MVC等,以實踐爲主,適合新手一塊兒學習,高手請飄過。java
今天開始第一篇:Spring MVC Hello Worldgit
一、建立Web Projectgithub
我使用的IDE是MyEclipse 2014,其餘版本的操做相似。web
Target runtime選擇none,一路默認或者直接點擊Finishspring
二、建立完成後,會報HttpServlet找不到的錯誤api
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
引入servlet-api.jar便可,若是安裝了tomcat能夠從tomcat裏面拷貝這個jar包,若是在剛纔第一步選擇了Target runtime會自動添加這些jar包,可是還會添加些別的。tomcat
三、引入spring的jar包,這裏用的版本是3.0.5,jar包的種類參考了《Spring 3.x企業應用開發實戰》,後續的工程也會繼續參考這本書。mvc
spring的jar包和源碼下載地址在官網上不是很是好找,記錄下,方便使用app
spring jar包下載地址:
http://repo.spring.io/release/org/springframework/spring/
spring源碼下載地址:
https://github.com/spring-projects/spring-framework/tags
引入的方式:將jar報拷貝到WEB-INF/lib下便可
引入的jar包能夠參看我在碼雲上分享的代碼,地址:
http://git.oschina.net/smilease/spring-example
hello world 項目自己不須要引入這麼多jar包,之後的功能會用到,先引入了。
四、修改web.xml,若是沒有就新建一個,web.xml,是整個項目的基礎配置,其餘配置文件須要經過它來配置。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>spring example</display-name> <servlet> <servlet-name>tsingyu</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>tsingyu</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
這裏配置了spring要攔截的後綴名.html,配置了歡迎頁的路徑,這個歡迎頁也是要被sping攔截的。
五、添加tsingyu-servlet.xml,tsingyu是web.xml裏面servlet-name的值,spring默認加載 *-servlet.xml,默認路徑是在web.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" 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"> <!-- 掃描web包,應用Spring的註解 --> <context:component-scan base-package="cn.tsingyu.spring.example.controller"/> <!-- 配置視圖解析器,將ModelAndView及字符串解析爲具體的頁面 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> </beans>
這裏配置了controller類的路徑和視圖文件的路徑及後綴。
六、新建controller文件,路徑與上面的配置保持一致
package cn.tsingyu.spring.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class IndexController { @RequestMapping(value = "/index.html") public String loginPage(){ return "hello"; } }
經過@Controller註解來聲明這是一個controller,經過@RequestMapping來配置要匹配的請求路徑,這裏配置的是歡迎頁的路徑,這樣訪問項目跟地址的時候就會進入到這個方法處理。
七、新建視圖文件,路徑及後綴與tsingyu-servlet.xml的配置一致,這裏只作一個簡單的hello world的jsp頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello World</title> </head> <body> Hello World </body> </html>
八、發佈到tomcat上,啓動tomcat,訪問http://localhost:8080/spring1/,看到hello world,成功。
以上就是spirng mvc的hello world,後續會繼續擴展。
完整代碼見: