spring mvc入門,springmvc
1.1. spring mvc簡介1.1.1. MVC:Model-View-Control
架構性質的C層要完成的主要工作:封裝web請求為一個資料對象、調用商務邏輯層來處理資料對象、返回處理資料結果及相應的視圖給使用者。
1.1.2. 簡要概述spring mvc
Spring C 層架構的核心是 DispatcherServlet,它的作用是將請求分發給不同的後端處理器,也即 使用了一種被稱為FrontController 的模式(後面對此模式有簡要說明)。 Spring 的C 層架構使用了後端控制器來、映射處理器和視圖解析器來共同完成C 層架構的主要工作。並且spring 的C 層架構還真正地把業務層處理的資料結果和相應的視圖拼成一個對象,即我們後面會經常用到的ModelAndView 對象。
1.2. Spring MVC入門案例1.2.1. 建立web工程1.2.2. 引入spring jar包和log4j包
檔案目錄結構
1.2.3. 配置web.xml
|
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"version="3.0">
<display-name>HelloSpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置springmvc 的核心servlet -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 指定springmvc 設定檔的路徑 預設為 /WEB-INF/<[servlet-name]-servlet.xml> 例如此處為/WEB-INF/spring-servlet.xml
下面配置的也就是預設值 -->
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<!-- 配置隨伺服器啟動自動初始化 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<!-- 過濾所有尾碼名為.do的請求 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app> |
1.2.4. 建立一個實現了Controller介面的控制器
|
package com.morris.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloController implements Controller {
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
ModelAndView mav = new ModelAndView("hello.jsp");
mav.addObject("message", "Hello World");
return mav;
}
} |
1.2.5. 配置spring-servlet.xml檔案
|
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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">
<beanid="simpleUrlHandlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<propertyname="mappings">
<props>
<propkey="/hello.do">helloControl</prop>
<!-- 說明:hello.do的請求將給名為helloControl的bean進行處理。 -->
</props>
</property>
</bean>
<beanid="helloControl"class="com.morris.controller.HelloController"></bean>
</beans> |
1.2.6. 測試
啟動tomcat,在瀏覽器輸入:http://localhost:8080/HelloSpringMVC/hello.do
出現如下的介面