欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

model和view。轉(zhuǎn)發(fā)到下一個(gè)頁(yè)面之后,會(huì)把 model中的數(shù)據(jù)渲染到view中展示。在頁(yè)面可以使用el表達(dá)式獲取。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 {
? ? ? ? //實(shí)例化ModelAndView對(duì)象
? ? ? ? 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.實(shí)現(xiàn)HttpRequestHandler接口

實(shí)現(xiàn)HttpRequestHandler接口,重寫(xiě) handleRequest方法。這個(gè)實(shí)現(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ù)");
? ? ? ? //請(qǐng)求轉(zhuǎn)發(fā)
? ? ? ? request.getRequestDispatcher("/WEB-INF/jsp/show.jsp").forward(request, response);
? ? }
}

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

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

3.全注解

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

@RequestMapping是提供請(qǐng)求訪問(wèn)路徑的注解,比如UserController上添加的@RequestMapping(“/”),這是相對(duì)路徑,相對(duì)于整個(gè)程序來(lái)說(shuō)的,所以可以在項(xiàng)目下直接訪問(wèn)到這個(gè)控制器類(lèi)。

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

一個(gè)注解控制器類(lèi)中可以定義很多的方法,只需要在方法頭部添加不同的@RequestMapping 的值就可以吧這些方法作為不同的控制器使用,所以注解模式在開(kāi)發(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)建模型視圖對(duì)象
? ? ? ? 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

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

在SpringMVC中一個(gè)控制器可以包含多個(gè)方法

SpringMVC中對(duì)于Controller的配置方式

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

1、實(shí)現(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類(lèi)

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中為T(mén)est1Controller注冊(cè)bean

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

2、注解實(shí)現(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">
? ? <!-- 自動(dòng)掃描指定的包,下面所有注解類(lèi)交給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

? ? ? //被這個(gè)注解的類(lèi)中的所有方法,如果返回值是String,并且有具體頁(yè)面可以跳轉(zhuǎn),那么就會(huì)被視圖解析器解析;
? ? ? @Controller
? ? ? public class Test2Controller {
? ? ??
? ? ? ? ? @RequestMapping("/t2")
? ? ? ? ? public String test2(Model model){
? ? ? ? ? ? ? model.addAttribute("msg","注解實(shí)現(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>

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

相關(guān)文章

  • spring的13個(gè)經(jīng)典面試題

    spring的13個(gè)經(jīng)典面試題

    Spring框架是一個(gè)開(kāi)放源代碼的J2EE應(yīng)用程序框架,是針對(duì)bean的生命周期進(jìn)行管理的輕量級(jí)容Spring解決了開(kāi)發(fā)者在J2EE開(kāi)發(fā)中遇到的許多常見(jiàn)的問(wèn)題,我們這篇文章就來(lái)了解一下spring的面試題
    2021-06-06
  • SpringBoot ApplicationListener事件監(jiān)聽(tīng)接口使用問(wèn)題探究

    SpringBoot ApplicationListener事件監(jiān)聽(tīng)接口使用問(wèn)題探究

    這篇文章主要介紹了SpringBoot ApplicationListener事件監(jiān)聽(tīng)接口使用問(wèn)題,自定義監(jiān)聽(tīng)器需要實(shí)現(xiàn)ApplicationListener接口,實(shí)現(xiàn)對(duì)應(yīng)的方法來(lái)完成自己的業(yè)務(wù)邏輯。SpringBoot Application共支持6種事件監(jiān)聽(tīng)
    2023-04-04
  • java文件上傳下載功能實(shí)現(xiàn)代碼

    java文件上傳下載功能實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了java文件上傳下載功能實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的朋友可以參考一下
    2016-06-06
  • spring源碼閱讀--@Transactional實(shí)現(xiàn)原理講解

    spring源碼閱讀--@Transactional實(shí)現(xiàn)原理講解

    這篇文章主要介紹了spring源碼閱讀--@Transactional實(shí)現(xiàn)原理講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java遞歸算法簡(jiǎn)單示例兩則

    Java遞歸算法簡(jiǎn)單示例兩則

    這篇文章主要介紹了Java遞歸算法,通過(guò)兩則示例分析了Java遞歸算法實(shí)現(xiàn)階乘與求和的具體操作技巧,需要的朋友可以參考下
    2017-09-09
  • spring boot org.junit.jupiter.api不存在的解決

    spring boot org.junit.jupiter.api不存在的解決

    這篇文章主要介紹了spring boot org.junit.jupiter.api不存在的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 淺談java如何實(shí)現(xiàn)Redis的LRU緩存機(jī)制

    淺談java如何實(shí)現(xiàn)Redis的LRU緩存機(jī)制

    今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著java如何實(shí)現(xiàn)Redis的LRU緩存機(jī)制展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • SpringSecurity中的EnableWebSecurity注解啟用Web安全詳解

    SpringSecurity中的EnableWebSecurity注解啟用Web安全詳解

    這篇文章主要介紹了SpringSecurity中的EnableWebSecurity注解啟用Web安全詳解,@EnableWebSecurity是Spring?Security用于啟用Web安全的注解,典型的用法是該注解用在某個(gè)Web安全配置類(lèi)上,實(shí)現(xiàn)了接口,需要的朋友可以參考下
    2023-12-12
  • Spring依賴注入多種類(lèi)型數(shù)據(jù)的示例代碼

    Spring依賴注入多種類(lèi)型數(shù)據(jù)的示例代碼

    這篇文章主要介紹了Spring依賴注入多種類(lèi)型數(shù)據(jù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • 使用Feign遠(yuǎn)程調(diào)用時(shí),序列化對(duì)象失敗的解決

    使用Feign遠(yuǎn)程調(diào)用時(shí),序列化對(duì)象失敗的解決

    這篇文章主要介紹了使用Feign遠(yuǎn)程調(diào)用時(shí),序列化對(duì)象失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評(píng)論