Summary of Common Interview Questions of SpringMVC

1. What is Spring MVC? Briefly introduce your understanding of spring MVC?

Spring MVC is a lightweight web framework based on Java that implements the request driven type of MVC design pattern. By separating Model, View and Controller, it decouples the responsibilities of the web layer, divides complex web applications into logical parts, simplifies development, reduces errors, and facilitates cooperation between developers in the group.

2. Spring MVC process?

(1) The user sends a request to the front-end controller DispatcherServlet;
(2) After the DispatcherServlet receives the request, it calls the HandlerMapping processor mapper to request to obtain a Handle;
(3) The processor mapper finds the specific processor according to the request url, generates the processor object and the processor interceptor (if any), and returns them to the DispatcherServlet;
(4) DispatcherServlet calls the HandlerAdapter processor adapter;
(5) The HandlerAdapter calls the specific processor (Handler, also called the back-end controller) through adaptation;
(6) The handler returns to ModelAndView after execution;
(7) The HandlerAdapter returns the Handler execution result ModelAndView to the DispatcherServlet;
(8) DispatcherServlet transmits ModelAndView to ViewResolver view parser for parsing;
(9) The ViewResolver returns the specific View after resolution;
(10) DispatcherServlet renders the view (that is, fills the model data into the view)
(11) DispatcherServlet responds to the user.

3. Advantages of Springmvc:

(1) It can support various view technologies, not limited to JSP;
(2) Integration with Spring framework (such as IoC container, AOP, etc.);
(3) Clear role assignment: front-end controller (dispatcherServlet), request to processor mapping (handlerMapping), processor adapter (HandlerAdapter), view resolver (ViewResolver).
(4) Support mapping strategies for various request resources.

4. What are the main components of Spring MVC?

(1) Front end controller DispatcherServlet (no programmer development required)
Function: Receiving request and response results is equivalent to a repeater. The DispatcherServlet reduces the coupling between other components.
(2) Processor mapper HandlerMapping (no programmer development required)
Function: Find the handler according to the requested URL
(3) Processor Adapter HandlerAdapter
Note: The handler should be written according to the rules required by the HandlerAdapter, so that the adapter HandlerAdapter can correctly execute the handler.
(4) Processor Handler (need programmer to develop)
(5) ViewResolver (not developed by programmers)
Function: analyze the view, and convert it into a real view according to the logical name of the view
(6) View (programmer needs to develop jsp)
View is an interface whose implementation class supports different view types (jsp, freemarker, pdf, etc.)

6. How does Spring MVC set redirection and forwarding?

(1) Forward: add “forward:” before the return value, such as “forward: user. do? Name=method4”
(2) Redirect: add “redirect:” before the return value, such as "redirect: http://www.baidu.com “

7. How does SpringMvc and AJAX call each other?

Through the Jackson framework, you can directly convert objects in Java into Json objects that Js can recognize. The specific steps are as follows:
(1) Join Jackson.jar
(2) Configure the mapping of json in the configuration file
(3) In accepting Ajax methods, you can directly return Object, List, etc., but the @ ResponseBody annotation should be added in front of the method.

8. How to solve the problem of Chinese garbled code in POST requests, and how to deal with GET?

(1) To solve the problem of garbled post requests:
Configure a CharacterEncodingFilter in web.xml and set it to utf-8;
(2) There are two ways to solve the problem of garbled Chinese parameters in the get request:
① Modify the tomcat configuration file to add a code consistent with the project code, as follows:
② Another method is to re code parameters:
String userName=new String (request. getParamter (“userName”). getBytes (“ISO8859-1”), “utf-8”)
ISO8859-1 is the default encoding of tomcat. The content encoded by tomcat needs to be encoded as utf-8.

9. Spring MVC exception handling?

Answer: You can throw exceptions to the Spring framework for processing; We just need to configure a simple exception handler and add a view page to the exception handler.

10. Is SpringMvc’s controller a singleton mode? If so, what are the problems and how to solve them?

A: It is a singleton mode, so there is a thread safety problem during multithreaded access. Do not use synchronization, which will affect performance. The solution is that you cannot write fields in the controller.

11. What are the common annotations of Spring MVC?

@RequestMapping: annotation for processing request url mapping, which can be used on classes or methods. If it is used on a class, it means that all methods in the class that respond to requests take this address as the parent path.
@RequestBody: annotation implements receiving the json data of http requests and converting json into java objects.
@ResponseBody: the annotation implementation converts the returned object of the coneoller method into a json object to respond to the customer.

12. The controller annotation in SpringMvc usually uses that annotation. Is there any other annotation that can replace it?

Answer: The @ Controller annotation is generally used, and the @ RestController annotation can also be used. The @ RestController annotation is equivalent to @ ResponseBody + @ Controller, representing the presentation layer. In addition, no other annotation is generally used instead.

13. If I want to intercept the get submission method in intercepting requests, how do I configure it?

A: You can add method=RequestMethod GET.

14. How to get the Request or Session in the method?

Answer: Declare the request directly in the formal parameter of the method, and SpringMvc will automatically pass in the request object.

15. If you want to get the parameters passed in from the foreground in the intercepting method, how can you get them?

Answer: It is OK to declare this parameter directly in the formal parameter, but the name must be the same as the passed parameter.

16. If there are many parameters passed in from the foreground, and these parameters are all the same object, how to get the object quickly?

Answer: Declare the object directly in the method, and SpringMvc will automatically assign the attribute to the object.

17. What is the return value of the function in SpringMvc?

Answer: The return value can have many types, including String and ModelAndView. The ModelAndView class combines views and data together, but it is generally better to use String.

18. What objects does SpringMvc use to transfer data from the background to the foreground?

Answer: Through the ModelMap object, you can call the put method in this object, add the object to it, and the foreground can get it through the el expression.

19. How to put the data in ModelMap into Session?

A: You can add @ SessionAttributes annotation to the class. The string contained in the annotation is the key to be put into the session.

20. How does the interceptor write in SpringMvc:

There are two writing methods, one is to implement the HandlerInterceptor interface, the other is to inherit the adapter class, and then implement the processing logic in the interface method; Then configure the interceptor in the configuration file of SpringMvc:

21. Annotation principle:

Annotation is a special interface that inherits Annotation in essence, and its specific implementation class is a dynamic proxy class generated by Java runtime. When we obtain annotations through reflection, we return the dynamic proxy object generated by the Java runtime. Calling a custom annotation method through a proxy object will eventually call the invoke method of AnnotationInvocationHandler. This method will retrieve the corresponding value from the memberValues map. The source of memberValues is the Java constant pool.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/6580.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Leetcode每日一题】 综合练习 - 全排列 II(难度⭐⭐)(71)

1. 题目解析 题目链接:47. 全排列 II 这个问题的理解其实相当简单,只需看一下示例,基本就能明白其含义了。 2.算法原理 算法思路梳理 为了生成给定数组nums的全排列,同时避免由于重复元素导致的重复排列,我们可以遵…

(三)科研技能-非英语母语者英文论文写作注意事项

目录 前言 一、英文写作重要性及文章目的 二、错误及改正方法 1.“a”、“an”、“the” 2.语句过长 3. 先说明目的、地点或原因,为句子的主旨作铺垫 4. 将表示时间的短语放在句首的倾向 5.将最重要的主语放在句首,以示强调 6.“Which”、“Tha…

Web的介绍

什么是web web:全球广域网 ,也成为万维网,是通过浏览器访问的网站 web访问的流程 浏览器先对前端服务器(前端程序)发送请求 然后前端服务器对浏览器进行响应 浏览器对后端服务器(Java程序)发…

关于YOLO8学习(五)安卓部署ncnn模型--视频检测

前文 关于YOLO8学习(一)环境搭建,官方检测模型部署到手机 关于YOLO8学习(二)数据集收集,处理 关于YOLO8学习(三)训练自定义的数据集 关于YOLO8学习(四)模型转换为ncnn 简介 本文将会讲解: (1)使用前文生成的ncnn模型,部署到安卓端,并且实现视频中,人脸的检测…

02_Java综述

目录 面向对象编程两种范式抽象OOP 三原则封装继承多态多态、封装与继承协同工作 面向对象编程 面向对象编程(Object-Oriented Programming,OOP)在Java中核心地位。几乎所有的Java程序至少在某种程度上都是面向对象的。OOP与java是密不可分的。下面说一下OOP的理论…

【Java探索之旅】内部类 静态、实例、局部、匿名内部类全面解析

文章目录 📑前言一、内部类1.1 概念1.2 静态内部类1.3 实例内部类1.4 局部内部类1.5 匿名内部类 🌤️全篇总结 📑前言 在Java编程中,内部类是一种强大的特性,允许在一个类的内部定义另一个类,从而实现更好的…

SFOS1:开发环境搭建

一、简介 最近在学习sailfish os的应用开发,主要内容是QmlPython。所以,在开发之前需要对开发环境(virtualBox官方SDKcmake编译器python)进行搭建。值得注意的是,我的开发环境是ubuntu22.04。如果是windows可能大同小异…

Docker-Compose编排LNMP并部署WordPress

前言 随着云计算和容器化技术的快速发展,使用 Docker Compose 编排 LNMP 环境已经成为快速部署 Web 应用程序的一种流行方式。LNMP 环境由 Linux、Nginx、MySQL 和 PHP 组成,为运行 Web 应用提供了稳定的基础。本文将介绍如何通过 Docker Compose 编排 …

Pytorch: nn.Embedding

文章目录 1. 本质2. 用Embedding产生一个10 x 5 的随机词典3. 用这个词典编码两个简单单词4. Embedding的词典是可以学习的5. 例子完整代码 1. 本质 P y t o r c h \mathrm{Pytorch} Pytorch 的 E m b e d d i n g \mathrm{Embedding} Embedding 模块是一个简单的查找表&#…

1.Spring Security介绍

spring security 功能 1.认证authentication,系统认为用户是否登录。 2.授权authorization,判断用户是否有权限去做某些事情。 spring security的缺点是重量级的。 还有另外一个安全认证权限框架是Apache下轻量级权限控制框架Shrio框架。 shiro的缺…

Stable Diffusion Windows部署教程

文章目录 1、准备2、Anaconda安装3、git安装4、开始部署 1、准备 官网需要翻墙软件最少6G内存,显卡在2060以上 2、Anaconda安装 这是一个运行python的环境管理工具进入官网点击download下载打开文件一路到 Advanced Options,勾选第一项注册到环境变量…

C++设计模式-结构型设计模式

写少量的代码来应对未来需求的变化。 单例模式 定义 保证一个类仅有一个实例,并提供一个该实例的全局访问点。——《设计模式》GoF 解决问题 稳定点: 类只有一个实例,提供全局的访问点(抽象) 变化点&#xff1a…

仿知乎网站问答源码,开源版

仿知乎网站问答源码,开源版 需要一定动手能力 发文章,发视频,发想法,提问回答,注册登录 开发环境 使用技术:springbootthymeleafRedis; 开发环境:tomcat8.0,jdk8.0, ID…

怎么用CAPL与Python交互

怎么用CAPL与其他应用程序交互 怎么用CAPL与Python交互 怎么用CAPL与Python交互 怎么用CAPL与其他应用程序交互前言1、CAPL怎么调Python?1.1CAPL调Python的命令1.2CAPL调用Python实例 2、怎么把python运行的结果返回给CAPL2.1通过环境变量 3、CAPL调Python的输入参…

使用 Docker-Compose 部署 ZooKeeper + Kafka + Kafka-UI

使用 Docker-Compose 部署 Kafka + ZooKeeper 1. 无密码验证部署1.1 启动 ZooKeeper1.2 查看 zookeeper 状态1.3 启动 Kafka1.4 Kafka 配置文件1.4 使用命令操作 Kafak 生产、消费1.4.1 创建topic1.4.2 查看某个 topic1.4.3 获取所有 topic1.4.4 删除 topic1.4.4 发送消息1.4.5…

我们的手机是如何连接上网的?骨干网又是什么?

什么是骨干网(Backbone Network) 几台计算机连接起来,互相可以看到其他人的文件,这叫局域网。整个城市的计算机都连接起来,就是城域网。把城市之间连接起来的网就叫骨干网。 这些骨干网是国家批准的可以直接和国外连…

环形列表 题目分析

一、题目链接:. - 力扣(LeetCode) 二、思路 1.使用快慢指针,快指针一次走两步,慢指针一次走一步,二者相对速度为1 2.当慢指针刚好进入环时,设二者的相对距离为N 3.如果链表中存在环&#xff…

Colab - Introduction to Object Detection using TensorFlow Hub

Colab - Introduction to Object Detection using TensorFlow Hub 1. 源由2. TensorFlow Hub3. 目标检测3.1 举例 - EfficientDet/D4 COCO 20173.2 下载示例图像3.2.1 显示部分样本3.2.2 定义一个将类别ID映射到类别名称和颜色的字典 3.3 加载模型3.4 单张照片执行推理3.4.1 推…

Python基础详解一

一&#xff0c;print打印 print("hello word") print(hello word) 双引号和单引号都可以 二&#xff0c;数据类型 Python中常用的有6种值的类型 输出类型信息 print(type(11)) print(type("22")) print(type(22.2)) <class int> <class str&…

C#图像:2.图像处理

类库项目ImageProcessingLibrary代码 里面是几种图像处理函数&#xff0c;可以生成DLL文件被其他&#xff08;下面&#xff09;项目添加引用 using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.…