【java框架】SpringMVC(2)--SpringMVC實現Controller方式

来源:https://www.cnblogs.com/yif0118/archive/2020/03/14/12494239.html
-Advertisement-
Play Games

1. SpringMVC的Controller實現方式 SpringMVC實現Controller的方式主要有控制器實現方式與全註解實現方式,其中全註解實現方式是當前項目中比較常用的一種方式。 1.1.控制器實現方式 1.1.1. 實現Controller介面 創建一個類實現Controller介面 ...


1.   SpringMVC的Controller實現方式

SpringMVC實現Controller的方式主要有控制器實現方式全註解實現方式,其中全註解實現方式是當前項目中比較常用的一種方式。

1.1.控制器實現方式

1.1.1.     實現Controller介面

創建一個類實現Controller介面:

/**
 * 實現Controller方式一:
 *      實現一個Controller介面,實現handleRequest方法
 *      並且在Springmvc的配置文件中配置這個bean,指定Demo1Controller的訪問路徑
*/
public class Demo1Controller implements Controller {
  @Override
  public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {
    System.out.println("進入demo1Controller視圖Model實現方式...");
        //創建ModelAndView對象
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("/WEB-INF/views/demo1Controller.jsp");
    return modelAndView;
  }
}

配置applicationContext-mvc.xml文件,配置bean交給Spring來管理:

<?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: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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--開啟靜態資源的訪問-->
    <mvc:default-servlet-handler />
    <!--SpringMVC的配置文件:把控制器類交給Spring來管理-->
    <!--name:訪問的映射路徑-->
    <!--Controller實現方式一配置-->
    <bean name="/demo1Controller" class="cn.yif.controllerImpl01.Demo1Controller"></bean>
</beans>

1.1.2.     實現HttpRequestHandler介面

創建一個類來實現HttpRequestHandler介面,實現handleRequest方法:

/**
 * 實現Controller方式二:
 *      實現一個HttpRequestHandler介面,實現handleRequest方法
 *      並且在Springmvc的配置文件中配置這個bean,指定Demo2Controller的訪問路徑
 */
public class Demo2Controller implements HttpRequestHandler {
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("進入demo2Controller視圖Model實現方式...");
        //獲取參數
        //request.getParameter("name");
        //轉發
        request.getRequestDispatcher("/WEB-INF/views/demo2Controller.jsp").forward(request, response);
    }
}

配置applicationContext-mvc.xml文件,配置bean交給Spring來管理:

<?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: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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--開啟靜態資源的訪問-->
    <mvc:default-servlet-handler />
    <!--SpringMVC的配置文件:把控制器類交給Spring來管理-->
    <!--name:訪問的映射路徑-->
    <!--Controller實現方式二配置-->
    <bean name="/demo2Controller" class="cn.yif.controllerImpl02.Demo2Controller"></bean>
</beans>

1.1.3.     普通類(POJO)註解實現

創建一個類,在類中可以提供多個method方法,使用@RequestMapping映射類路徑+方法路徑,,這樣一個類中就可以配置多個訪問路徑:

/**
 * 實現Controller方式三:
 *      普通類(POJO)和註解@RequestMapping
 *      配置訪問路徑:類路徑+方法路徑
 *      這樣一個類中可以配置多個方法、多個方法url映射
 *      同樣需要在applicationContext-mvc.xml中配置bean
 *      而且需要開啟SpringMVC的註解支持:識別並掃描類上面的註解@RequestMapping
 */
@RequestMapping("/demo3Controller")
public class Demo3Controller {
    @RequestMapping("/add")
    public ModelAndView add(){
        System.out.println("進入demo3Controller視圖Model的add方法...");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/WEB-INF/views/demo3Controller_add.jsp");
        return modelAndView;
    }

    @RequestMapping("/del")
    public ModelAndView del(){
        System.out.println("進入demo3Controller視圖Model的del方法...");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/WEB-INF/views/demo3Controller_del.jsp");
        return modelAndView;
    }
}

配置applicationContext-mvc.xml文件,配置bean交給Spring來管理,無需配置name訪問路徑,註意:必須配置開啟SpringMVC註解配置的掃描:

<?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: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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--開啟靜態資源的訪問-->
    <mvc:default-servlet-handler />
    <!--開啟SpringMVC對註解的支持-->
    <mvc:annotation-driven />
    <!--SpringMVC的配置文件:把控制器類交給Spring來管理-->
    <!--Controller實現方式三配置:在Controller類中配置了url,這裡無需配置-->
    <bean class="cn.yif.controllerImpl03.Demo3Controller"></bean>
</beans>

1.2.全註解實現方式

全註解實現方式較控制器實現方式簡單,而且只需要我們普通的一個Controller類上面添加@Controller與@RequestMapping註解即可,是一種項目中常見的註解配置方式。

主要的步驟如下:

①    創建一個普通的類,在類上面配置@Controller、@RequestMapping註解;

/**
  * 實現Springmvc全註解方式:
  *      寫一個普通的Controller類
*      無需在applicationContext-mvc.xml中配置bean,只需要使用@Controller告訴Spring這是一個bean
*      通過@RequestMapping在類與方法上映射請求路徑,可以映射多個方法的請求路徑
*/
@Controller
@RequestMapping("/annoController")
public class AnnotationController {
    @RequestMapping("/add")
    public ModelAndView add(){
        System.out.println("進入AnnotationController中的add方法...");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/WEB-INF/views/annoController_add.jsp");
        return modelAndView;
    }

    @RequestMapping("/del")
    public ModelAndView del(){
        System.out.println("進入AnnotationController中的add方法...");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/WEB-INF/views/annoController_del.jsp");
        return modelAndView;
    }
}

②    在applicationContext-mvc.xml中配置—開啟對SpringMVC註解的支持、配置包的掃描(具體到哪個路徑下去掃描@Controller)、相容Spring3.2版本的配置,具體配置如下:

applicationContext-mvc.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
    <!--開啟靜態資源的訪問-->
    <mvc:default-servlet-handler />
    <!--開啟SpringMVC對註解的支持-->
    <mvc:annotation-driven />
    <!-- 進行包的掃描,去看類上面是否有相應的標簽配置:包含@Component、@Controller、@Service、@Repository -->
    <context:component-scan base-package="cn.yif.controllerallannoImpl" />
    <!-- 這個不是必須的(spring3.2版本前使用) 配上後相容性好 -->
    <context:annotation-config />
</beans>

③   註意事項:還需要加上spring-aop.jar包,否則在處理註解映射時無法找到對應切麵的映射會拋出aop異常:

Caused by: java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource

解決方案,導入spring-aop-4.1.2.RELEASE.jar包即可。

對應方法訪問頁面/annoController/add與annoController/del:

 

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 背景 項目交叉編譯為可執行文件之後,在其他目錄執行文件時提示找不到配置文件 解決方案 直接採用以下代碼獲取到實際執行文件的路徑,然後拼接配置文件即可 代碼分析 os.Args是用來獲取命令行執行參數分片的,當使用 時 分片0會是一串複雜的路徑,原因是直接run go文件時會將文件轉移到臨時路徑下,然 ...
  • Dubbo admin管理控制台目前還沒有正式發佈,但是源碼已托管在github上,我們可以自行下載使用; 目前的管理控制台已經發佈0.1版本,結構上採取了前後端分離的方式,前端使用Vue和Vuetify分別作為Javascript框架和UI框架,後端採用Spring Boot框架。既可以按照標準的 ...
  • 1、Spring 1.x時代 在Spring 1.x時代,都是通過XML文件配置Bean。隨著項目的不斷擴大,需要將Bean的定義配置分放到不同的XML配置文件中。開發的時候需要頻繁的在java類和XML配置文件中切換。 2、Spring 2.x時代 隨著 JDK 1.5帶來的註解支持,Spring ...
  • 資源限制 時間限制:1.0s 記憶體限制:256.0MB 問題描述 利用字母可以組成一些美麗的圖形,下麵給出了一個例子: ABCDEFG BABCDEF CBABCDE DCBABCD EDCBABC 這是一個5行7列的圖形,請找出這個圖形的規律,並輸出一個n行m列的圖形。 輸入格式 輸入一行,包含兩 ...
  • [TOC] pandas對象有一個常用數學,統計學方法的集合。大部分屬於歸納或彙總統計。這些方法從DataFrame的行或列中抽取一個Series或一系列的值。 pandas的描述性統計的方法和NumPy的方法相比,內建了處理缺失值的功能,很好地針對於每一個我們需要處理的數據。 一:一些基本方法 1 ...
  • 作者:Moon Light Dream 出處:https://www.cnblogs.com/Moon Light Dream/ 轉載:歡迎轉載,但未經作者同意,必須保留此段聲明;必須在文章中給出原文連接;否則必究法律責任 什麼是go cache KV存儲引擎有很多,常用的如redis,rocksd ...
  • 關於這篇博文 關於面向對象的話題,是一個十分重要的話題,由於博主是做java出身的,在java 中,由於java的嚴謹性,面向對象可以搞死你(/很難很難),在python中,所有的 一切都TM怎麼簡單,所以,這放出了詳細的python面向對象階段的學習筆記,供你 去翻閱,實例代碼也在裡面。 項目地址 ...
  • @Param()註解 引用類型不需要加 如果只有一個基本類型的,可以忽略 在 sql 中引用的就是在註解中設定的欄位名 高級結果映射 多對一: 一對多 動態 sql if choose (when, otherwise) trim (where, set) foreach 實體類 if 如果上面的 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...