三層架構、Mvc配置

三層架構好處將每個部分都獨立開,方便升級擴展

三層架構

   數據層
        1.負責所有對象數據操作的方法
        2.對數據連接處理
        3.對外絕對不暴露任何sql語句
   業務層
        1.事務處理
        2.業務處理
   視圖層
        mvc結構
              M 與業務層接軌處
              C 控制器
              V JSP數據呈現
    MVC:
        模型 M 業務調用,業務層調用
        控制器  C 接受請求找到模型,得到模型結果,跳轉至相應頁面
        視圖 v 呈現數據 

請求過程 1、V>>>C>>>M>>>C>>>V 這個常用 因爲所有都是由控制器來操作 視圖不和邏輯交互

   2、  V>>>C>>>M>>>V

引包:

springmvc
springcontext
javax.servlet
javax.servlet.jsp.jstl
javax.servlet.jsp
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.1.3.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp.jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2</version>
      <scope>provided</scope>
    </dependency>

配置resources
Application.xml(名字自己隨便起)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:annotation-config/> <!--開啓註解模式-->
    <context:component-scan base-package="com.sun"/> <!--管理對象路徑-->
    <mvc:annotation-driven/> <!--開啓MVC註解模式-->
</beans>
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Controller //所明這個類是控制器 要想管理servlet就得將他們集中起來進行統一的管理 
public class Action {
    @RequestMapping("/hello")//和webServlet作用一樣
    public void Method(int password,
                       String uname, HttpServletRequest request,
                       HttpServletResponse response,
                       HttpSession session) {
       //這裏獲得的是 HttpServletRequest請求頭,HttpServletResponse響應頭
        //servlet 有的都可以獲得
        // 表單獲得變得簡單 說明類型 及名稱就可以獲得表單數據
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章