玩轉Spring MVC(三)----spring基本設定檔

來源:互聯網
上載者:User

這篇文章總結一下spring mvc的基本配置,首先貼一張我的項目的目錄截圖,有一些多餘的檔案,大家不必在意:


用到的一些jar包在這:<a>http://download.csdn.net/detail/u012116457/8426199</a>


接下來是web.xml的配置:

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>      <!--1.配置spring分發器(是總的控制中心  被攔截的url會匯聚到該servlet) --><servlet>    <servlet-name>springmvc</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <!--配置spring上下文  (從該目錄下載入spring mvc的設定檔) -->    <init-param>       <param-name>contextConfigLocation</param-name>       <param-value>/WEB-INF/classes/spring/spring*.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>  <!-- tomcat啟動後立即載入 --></servlet><!--2. 配置spring攔截的url模板  以.do結尾的url--><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><!--3. 註冊設定檔讀取器,監聽spring設定檔的變化 詳見 注3--><listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>         <!--4.  設定設定檔列表 設定全域參數 為3的參數配置資訊--><context-param><param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/classes/spring/*Config.xml</param-value></context-param> <!--5. 編碼字元集統一為UTF-8(過濾器) --><filter><display-name>encodingFilter</display-name><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter></web-app>

接下來是springMVC-servlet.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"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                 http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd                     http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-3.0.xsd">     <!-- @Controller註解的使用前提配置  詳細解釋見(注.txt 1. )-->   <mvc:annotation-driven />   <!--  <context:annotation-config/> -->   <!-- 對module包中的所有類進行掃描,以完成Bean建立和自動依賴注入的功能-->   <context:component-scan base-package="module">   </context:component-scan>      <!-- 啟動Spring MVC的註解功能,完成請求和註解POJO的映射 --><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />    <!-- 定義視圖解析器,在視圖模型前後添加首碼尾碼 暫時只支援jsp尾碼-->   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/" /><!-- 路徑首碼 --><property name="suffix" value=".jsp" /><!-- 尾碼 --></bean></beans>
這些就是spring mvc最基礎的配置,下面是一個控制類的執行個體:

package module.controller;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/start")   public class StartController {private static String START="module/jsp/start"; @RequestMapping("/index.do")   public String start(HttpServletRequest request){return START;}}
這樣在瀏覽器中輸入:http://localhost:8080/springMVC/start/index.do即可訪問到start.jsp頁面


聯繫我們

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