最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC實現(xiàn)Controller的三種方式總結(jié)

 更新時間:2022年02月22日 09:51:20   作者:ych717  
這篇文章主要介紹了SpringMVC實現(xiàn)Controller的三種方式總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

實現(xiàn)Controller的三種方式

1.實現(xiàn)Controller接口

實現(xiàn)Controller接口,重寫handleRequest方法,ModelAndView對象是一個模型視圖對象,既可以添加數(shù)據(jù),又可以保存頁面信息,并且處理請求的方式是轉(zhuǎn)發(fā)。這個對象要拆成兩部分來看

model和view。轉(zhuǎn)發(fā)到下一個頁面之后,會把 model中的數(shù)據(jù)渲染到view中展示。在頁面可以使用el表達式獲取。Model中數(shù)據(jù)的范圍是 request。

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
?* @className: PACKAGE_NAME.MyController1
?* @description: TODO
?* @author: ych
?* @create: 2021-06-02 20:25
?*/
public class MyController1 implements Controller {
? ? @Override
? ? public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
? ? ? ? //實例化ModelAndView對象
? ? ? ? ModelAndView modelAndView = new ModelAndView();
? ? ? ? //放數(shù)據(jù),相當(dāng)于request.setAttribute("msg", "我是數(shù)據(jù)");
? ? ? ? modelAndView.addObject("msg", "我是數(shù)據(jù)");
? ? ? ? //放視圖
? ? ? ? modelAndView.setViewName("forward:/WEB-INF/jsp/show.jsp");
? ? ? ? return modelAndView;
? ? }
}

Spring-mvc.xml配置文件中添加,如下信息;

<bean class="MyController1" id="controller1"/>

2.實現(xiàn)HttpRequestHandler接口

實現(xiàn)HttpRequestHandler接口,重寫 handleRequest方法。這個實現(xiàn)方式與servlet 基本一致。

import org.springframework.web.HttpRequestHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
?* @className: PACKAGE_NAME.MyController2
?* @description: TODO
?* @author: ych
?* @create: 2021-06-02 20:28
?*/
public class MyController2 implements HttpRequestHandler {
? ? @Override
? ? public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
? ? ? ? //共享數(shù)據(jù)到request
? ? ? ? request.setAttribute("msg", "我是數(shù)據(jù)");
? ? ? ? //請求轉(zhuǎn)發(fā)
? ? ? ? request.getRequestDispatcher("/WEB-INF/jsp/show.jsp").forward(request, response);
? ? }
}

Spring-mvc.xml配置文件中添加,如下信息;

<bean class="MyController2" id="controller2"/>

3.全注解

全注解,開發(fā)中寫的@Controller注解必須配合掃描才能變成控制器。掃描組件會把頭部帶有注解的類管理起來。

@RequestMapping是提供請求訪問路徑的注解,比如UserController上添加的@RequestMapping(“/”),這是相對路徑,相對于整個程序來說的,所以可以在項目下直接訪問到這個控制器類。

方法 test的頭部添加@RequestMapping(“/test.do”),表示訪問到這個控制器類之后,在訪問路徑上再添加上“/test.do”才能訪問到這個方法。

一個注解控制器類中可以定義很多的方法,只需要在方法頭部添加不同的@RequestMapping 的值就可以吧這些方法作為不同的控制器使用,所以注解模式在開發(fā)中最常用。

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
?* @className: PACKAGE_NAME.MyController3
?* @description: TODO
?* @author: ych
?* @create: 2021-06-02 20:32
?*/
@Controller
@RequestMapping("/")
public class MyController3 {
? ? @RequestMapping("/test1.do")
? ? public ModelAndView test(){
? ? ? ? //創(chuàng)建模型視圖對象
? ? ? ? ModelAndView modelAndView = new ModelAndView();
? ? ? ? //添加模型數(shù)據(jù),相當(dāng)于request.setAttribute("msg", "helloworld");
? ? ? ? modelAndView.addObject("msg", "helloworld");
? ? ? ? //放視圖
? ? ? ? modelAndView.setViewName("forward:/WEB-INF/jsp/show.jsp");
? ? ? ? return modelAndView;
? ? }
}

在spring-mvc.xml文件中必須增加上掃描組件:

<context:component-scan base-package="需要掃描的包路徑"/>

關(guān)于SpringMVC的控制器(Controller)

控制器Controller

控制器負責(zé)解析用戶的請求并將其轉(zhuǎn)換為一個模型

在SpringMVC中一個控制器可以包含多個方法

SpringMVC中對于Controller的配置方式

  • 實現(xiàn)Controller接口
  • 注解實現(xiàn)Controller

1、實現(xiàn)Controller接口

1、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
? ? ? ? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ? ?xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
? ? ? ? ?version="4.0">
? ? <servlet>
? ? ? ? <servlet-name>springmvc</servlet-name>
? ? ? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
? ? ? ? <init-param>
? ? ? ? ? ? <param-name>contextConfigLocation</param-name>
? ? ? ? ? ? <param-value>classpath:springmvc-servlet.xml</param-value>
? ? ? ? </init-param>
? ? ? ? <load-on-startup>1</load-on-startup>
? ? </servlet>
? ? <servlet-mapping>
? ? ? ? <servlet-name>springmvc</servlet-name>
? ? ? ? <url-pattern>/</url-pattern>
? ? </servlet-mapping>
</web-app>

2、springmvc-servlet.xml

? ? <context:component-scan base-package="com.springmvc.controller"/>
? ? <mvc:default-servlet-handler/>
? ? <mvc:annotation-driven/>
? ? <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
? ? ? ? <property name="prefix" value="/WEB-INF/jsp/"/>
? ? ? ? <property name="suffix" value=".jsp"/>
? ? </bean>

3、創(chuàng)建Controller類

package com.springmvc.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test1Controller implements Controller {
? ? public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
? ? ? ? ModelAndView modelAndView = new ModelAndView();
? ? ? ? modelAndView.addObject("msg","Controller");
? ? ? ? modelAndView.setViewName("test");
? ? ? ? return modelAndView;
? ? }
}

在springmvc-servlet.xml中為Test1Controller注冊bean

<bean id="/test1" class="com.springmvc.controller.Test1Controller"/>

2、注解實現(xiàn)Controller

1、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
? ? ? ? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ? ?xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
? ? ? ? ?version="4.0">
? ? <servlet>
? ? ? ? <servlet-name>springmvc</servlet-name>
? ? ? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
? ? ? ? <init-param>
? ? ? ? ? ? <param-name>contextConfigLocation</param-name>
? ? ? ? ? ? <param-value>classpath:springmvc-servlet.xml</param-value>
? ? ? ? </init-param>
? ? ? ? <load-on-startup>1</load-on-startup>
? ? </servlet>
? ? <servlet-mapping>
? ? ? ? <servlet-name>springmvc</servlet-name>
? ? ? ? <url-pattern>/</url-pattern>
? ? </servlet-mapping>
</web-app>

2、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: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
? ? ? ? https://www.springframework.org/schema/context/spring-context.xsd
? ? ? ? http://www.springframework.org/schema/mvc
? ? ? ? https://www.springframework.org/schema/mvc/spring-mvc.xsd">
? ? <!-- 自動掃描指定的包,下面所有注解類交給IOC容器管理 -->
? ? <context:component-scan base-package="com.springmvc.controller"/>
? ? <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
? ? ? ? <property name="prefix" value="/WEB-INF/jsp/"/>
? ? ? ? <property name="suffix" value=".jsp"/>
? ? </bean>
<!-- ? ?<bean id="/test1" class="com.springmvc.controller.Test1Controller"/>-->
</beans>

3、Test2Controller

? ? ? //被這個注解的類中的所有方法,如果返回值是String,并且有具體頁面可以跳轉(zhuǎn),那么就會被視圖解析器解析;
? ? ? @Controller
? ? ? public class Test2Controller {
? ? ??
? ? ? ? ? @RequestMapping("/t2")
? ? ? ? ? public String test2(Model model){
? ? ? ? ? ? ? model.addAttribute("msg","注解實現(xiàn)Controller");
? ? ? ? ? ? ? return "test";
? ? ? ? ? }
? ? ? }

4、test.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
? ? <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java 編程之IO流資料詳細整理

    Java 編程之IO流資料詳細整理

    這篇文章主要介紹了Java 編程之IO流資料詳細整理的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • SpringBoot密碼加密的實現(xiàn)示例

    SpringBoot密碼加密的實現(xiàn)示例

    本文主要介紹了SpringBoot密碼加密的實現(xiàn)示例,包括引入依賴、配置加密工具、生成加密密鑰、加密密碼、配置解密,具有一定的參考價值,感興趣的可以了解一下
    2024-08-08
  • Spring如何消除代碼中的if-else/switch-case

    Spring如何消除代碼中的if-else/switch-case

    這篇文章主要給大家介紹了關(guān)于Spring如何消除代碼中if-else/switch-case的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • java遍歷途中修改數(shù)據(jù)及刪除數(shù)據(jù)的方法總結(jié)

    java遍歷途中修改數(shù)據(jù)及刪除數(shù)據(jù)的方法總結(jié)

    在使用java的集合類遍歷數(shù)據(jù)的時候,在某些情況下可能需要對某些數(shù)據(jù)進行刪除,下面這篇文章主要給大家介紹了關(guān)于java遍歷途中修改數(shù)據(jù)及刪除數(shù)據(jù)的方法總結(jié),需要的朋友可以參考下
    2023-10-10
  • java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實例

    java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實例

    這篇文章主要介紹了java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • jackson在springboot中的使用方式-自定義參數(shù)轉(zhuǎn)換器

    jackson在springboot中的使用方式-自定義參數(shù)轉(zhuǎn)換器

    這篇文章主要介紹了jackson在springboot中的使用方式-自定義參數(shù)轉(zhuǎn)換器,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java 泛型總結(jié)(一):基本用法與類型擦除

    Java 泛型總結(jié)(一):基本用法與類型擦除

    本文主要介紹了Java泛型的使用以及類型擦除相關(guān)的問題。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • Spring中@RefreshScope注解的處理方法詳解

    Spring中@RefreshScope注解的處理方法詳解

    這篇文章主要介紹了Spring中@RefreshScope注解的處理方法詳解,spring啟動時會調(diào)用ClassPathBeanDefinitionScanner.java類中的doScan()對包路徑下的所有class進行掃描,獲取bean的定義,同時對bean的@RefreshScope(@Scope的父類)進行處理,需要的朋友可以參考下
    2023-10-10
  • javaweb 實現(xiàn)文件下載的方法及實例代碼

    javaweb 實現(xiàn)文件下載的方法及實例代碼

    這篇文章主要介紹了javaweb 實現(xiàn)文件下載的方法的相關(guān)資料,這里提供了實現(xiàn)代碼,需要的朋友可以參考下
    2016-11-11
  • Java使用注解實現(xiàn)防止重復(fù)提交實例

    Java使用注解實現(xiàn)防止重復(fù)提交實例

    這篇文章主要介紹了Java使用注解實現(xiàn)防止重復(fù)提交實例,在一些項目中由于用戶誤操作,多次點擊表單提交按鈕,會產(chǎn)生很多次的數(shù)據(jù)交互,為了解決這一問題,本文使用注解來實現(xiàn)防止重復(fù)提交,需要的朋友可以參考下
    2023-07-07

最新評論

军事| 康保县| 周至县| 霍林郭勒市| 巴中市| 塘沽区| 吴忠市| 临邑县| 崇明县| 喀喇沁旗| 白河县| 桂平市| 城固县| 陆川县| 海晏县| 阿克苏市| 麟游县| 青浦区| 车致| 怀远县| 阿荣旗| 迁安市| 清徐县| 会同县| 祁阳县| 重庆市| 凤凰县| 依兰县| 广灵县| 正蓝旗| 海宁市| 南通市| 萨嘎县| 瑞金市| 天峨县| 黄龙县| 吉林省| 建瓯市| 松潘县| 溆浦县| 长宁县|