<p>        今天使用Spring  Tool Suite 搞了一個 SpringMVC超級入門的程序,沒想到碰到了一些跟SpringMVC無關的問題,在此記錄一下,給後來者和本身一個借鑑。 <br />Spring MVC Hello World <br /><font color="#ff0000"><strong>1、準備開發環境和運行環境:</strong></font> <br />☆開發工具:Spring  Tool  Suite <br />☆運行環境:tomcat6.0.20 <br />☆工程:動態web工程(spring-3.x-mvc) <br />☆spring框架下載: <br />spring-framework-3.1.1.RELEASE-with-docs.zip <br />☆依賴jar包: <br />一、  Spring框架jar包: <br />爲了簡單,將spring-framework-3.1.1.RELEASE-with-docs.zip/<font color="#0000ff">dist/下的全部jar</font>包拷貝到項目的WEB-INF/lib目錄下; <br />二、  Spring框架依賴的jar包(能夠根據關鍵字查找一下快速定位): <br />須要添加Apache commons logging日誌,此處使用的是<font color="#0000ff">commons.logging-1.1.1.jar</font>; <br />           位於 \projects\ivy-cache\repository\org.apache.commons\com.springsource.org.apache.commons.logging\1.1.1\目錄下 <br />須要添加jstl標籤庫支持,此處使用的是<font color="#0000ff">jstl-1.1.2.jar和standard-1.1.2.jar</font>; <br />分別位於\projects\ivy-cache\repository\javax.servlet\com.springsource.javax.servlet.jsp.jstl\1.1.2和 <br />            \projects\ivy-cache\repository\org.apache.taglibs\com.springsource.org.apache.taglibs.standard\1.1.2,目錄下</p> <p><font color="#ff0000"><strong>2、  前端控制器的配置dispatcherServlet</strong></font></p> <p>在咱們的<font color="#0000ff">web.xml</font>中添加以下配置:</p> <p><font face="Arial"><?xml version="1.0" encoding="UTF-8"?> <br /><web-app xmlns:xsi="</font><a href="http://www.w3.org/2001/XMLSchema-instance""><font face="Arial">http://www.w3.org/2001/XMLSchema-instance"</font></a> <br /><font face="Arial">    xmlns="</font><a href="http://java.sun.com/xml/ns/javaee""><font face="Arial">http://java.sun.com/xml/ns/javaee"</font></a> <br /><font face="Arial">    xsi:schemaLocation="</font><a href="http://java.sun.com/xml/ns/javaee"><font face="Arial">http://java.sun.com/xml/ns/javaee</font></a><font face="Arial"> </font><a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd""><font face="Arial">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</font></a> <br /><font face="Arial">    id="WebApp_ID" version="2.5"></font></p> <p><font face="Arial">    <!-- Spring mvc的主servlet --> <br />    <servlet> <br />        <servlet-name><font color="#9e7c7c">helloController</font></servlet-name> <br />        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <br />        <load-on-startup>1</load-on-startup> <br />    </servlet> <br />    <servlet-mapping> <br />        <servlet-name>helloController</servlet-name> <br />        <url-pattern>/</url-pattern> <br />    </servlet-mapping></font></p> <p><font face="Arial"></web-app></font></p> <p><strong>load-on-startup</strong><strong>:</strong>表示啓動容器時初始化該Servlet;</p> <p><strong>url-pattern</strong><strong>:</strong>表示哪些請求交給Spring Web MVC處理, 「/」 是用來定義默認servlet映射的。也能夠如「*.html」表示攔截全部以html爲擴展名的請求。</p> <p>自此請求已交給Spring Web MVC框架處理,所以咱們須要配置Spring的配置文件,默認DispatcherServlet會加載WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件。本示例爲WEB-INF/ helloController-servlet.xml。</p> <p>而後<font color="#0000ff">建立<servlet-name>-servlet.xml文件</font>,這裏對應的就是helloController-servlet.xml,放在web.xml的同級目錄</p> <p><font face="Arial"><?xml version="1.0" encoding="UTF-8"?> <br /><beans xmlns="</font><a href="http://www.springframework.org/schema/beans""><font face="Arial">http://www.springframework.org/schema/beans"</font></a> <br /><font face="Arial">    xmlns:xsi="</font><a href="http://www.w3.org/2001/XMLSchema-instance""><font face="Arial">http://www.w3.org/2001/XMLSchema-instance"</font></a><font face="Arial"> xmlns:mvc="</font><a href="http://www.springframework.org/schema/mvc""><font face="Arial">http://www.springframework.org/schema/mvc"</font></a> <br /><font face="Arial">    xmlns:p="</font><a href="http://www.springframework.org/schema/p""><font face="Arial">http://www.springframework.org/schema/p"</font></a><font face="Arial"> xmlns:context="</font><a href="http://www.springframework.org/schema/context""><font face="Arial">http://www.springframework.org/schema/context"</font></a> <br /><font face="Arial">    xsi:schemaLocation="</font><a href="http://www.springframework.org/schema/mvc"><font face="Arial">http://www.springframework.org/schema/mvc</font></a><font face="Arial"> </font><a href="http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><font face="Arial">http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd</font></a> <br /><font face="Arial">        </font><a href="http://www.springframework.org/schema/beans"><font face="Arial">http://www.springframework.org/schema/beans</font></a><font face="Arial"> </font><a href="http://www.springframework.org/schema/beans/spring-beans.xsd"><font face="Arial">http://www.springframework.org/schema/beans/spring-beans.xsd</font></a> <br /><font face="Arial">        </font><a href="http://www.springframework.org/schema/context"><font face="Arial">http://www.springframework.org/schema/context</font></a><font face="Arial"> </font><a href="http://www.springframework.org/schema/context/spring-context-3.0.xsd""><font face="Arial">http://www.springframework.org/schema/context/spring-context-3.0.xsd"</font></a><font face="Arial">></font></p> <p><font face="Arial">    <font color="#d16349"><context:component-scan base-package="com.hunter.spring3.introduce" /> <br />    <mvc:annotation-driven /></font></font> <br /><font face="Arial">    <!-- ViewResolver --> <br />  <font color="#d16349">  <bean <br />        class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <br />        <property name="viewClass" <br />            value="org.springframework.web.servlet.view.JstlView" /> <br />        <property name="prefix" value="/WEB-INF/view/" /> <br />        <property name="suffix" value=".jsp" /></font> <br />    </bean> <br />    </beans></font></p> <p>說明:</p> <p>1)全部請求都要由DispatcherServlet來處理,所以映射到"/"上面(包括靜態頁面),</p> <p><load-on-startup>不加經測試也未見出錯,並且若是要修改spring-servlet.xml的配置位置或名字,</p> <p>能夠加</p> <p><init-param></p> <p>       <param-name>contextConfigLocation</param-name> <br />       <param-value>/WEB-INF/spring-servlet.xml</param-value> <br /></init-param></p> <p>但必定要放到<load-on-startup>前面,不然xml校驗出錯(經測試)。</p> <p>2)在spring-servlet.xml中,引入了mvc和context兩個命名空間,其中<mvc:annotation-driven/>表示這裏使用註解進行開發,<context:component-scan>指明註解所在的包名,InternalResourceViewResolver這個類的配置,說明邏輯視圖轉換成物理視圖的前綴和後綴,其viewClass的屬性若是是jsp的話經測試,不設置也可。</p> <p>3)InternalResourceViewResolver:用於支持Servlet、JSP視圖解析; <br />iewClass:JstlView表示JSP模板頁面須要使用JSTL標籤庫,classpath中必須包含jstl的相關jar包; <br />prefix和suffix:查找視圖頁面的前綴和後綴(前綴[邏輯視圖名]後綴), <br />好比傳進來的邏輯視圖名爲hello,則該該jsp視圖頁面應該存放在「WEB-INF/view/hello.jsp」;</p> <p> </p> <p><font color="#ff0000"><strong>3、編寫Controller層</strong></font></p> <p><font face="Arial">package com.hunter.spring3.introduce;</font></p> <p><font face="Arial">import org.springframework.stereotype.Controller; <br />import org.springframework.web.bind.annotation.RequestMapping;</font></p> <p><font face="Arial"><font color="#0000ff">@Controller <br /></font>public class HelloWorldController {</font></p> <p><font face="Arial">   <font color="#0000ff">@RequestMapping(value={"/hello","/"}) <br /></font>    public String handler() { <br />        return "hello"; <br />    } <br />}</font></p> <p><font face="Arial"></font> <br /><font color="#ff0000"><strong>4、編寫jsp</strong></font></p> <p>在<em>/WEB-INF/建立view目錄,在view下</em>建立hello.jsp便可用以下地址訪問:</p> <p><font color="#ff0000"><strong>5、啓動web容器,此處爲tomcat ,也可換成jetty等等</strong></font></p> <blockquote> <p>(觀察RequestMapping配置的路徑)</p> </blockquote> <blockquote> <p><a href="http://localhost:8080/<context-path>/hello">/hello">http://localhost:8080/<context-path>/hello</a></p> </blockquote> <blockquote> <p>或者</p> <p><a href="http://localhost:8080/<context-path>/">/">http://localhost:8080/<context-path>/</a></p> </blockquote> <p> </p> <p><font color="#ff0000"><strong>6、問題記錄</strong></font></p> <p><strong><font color="#ff0000">1.spring tool suite 啓動tomcat後 404錯誤</font></strong></p> <blockquote> <p>首先刪除當前spring tool suite 下創建的全部Server,而且將服務器與應用的關聯也要一併完全刪除。而後從新添加一個外部服務器。先不要將應用與該Server綁定,雙擊該服務器或者右鍵open,進入屬性編輯器中,這時會發現「Server Locations」中選項都是可選的了,能夠選擇一個你但願發佈到的路徑。如我但願將項目發佈到我本地自行安裝的tomcat下。<strong><font color="#ff0000"><a href="http://static.oschina.net/uploads/img/201411/12165008_t5om.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201411/12165008_MgQ6.png" width="244" height="129" /></a></font></strong></p> </blockquote> <p><strong><font color="#ff0000">2. jar包<a href="http://blog.csdn.net/kelingbest/article/details/7600976">will not be exported or published. Runtime ClassNotFoundExceptions may result</a>。致使啓動時 Class not  found :org.springframework.web.servlet.DispatcherServlet</font></strong></p> <blockquote> <p><font color="#000000">起初創建web工程後,引入jar包是經過右鍵項目 ADD External Jars的方式引入的,致使 WEB-INF/lib目錄下無實際引用的jar。</font></p> <p><font color="#000000">直接複製所須要的jar,手工粘貼到lib下,這樣sts會自動的加載到路徑下。</font></p> <p><font color="#000000">ClassNotFound 問題獲得解決,</font></p> </blockquote> <p><strong><font color="#ff0000"></font></strong></p>html