This article shares Spring MVC framework configuration method for your reference. The specific content is as follows
1. Overview
Spring MVC function: used to realize the interaction between the front-end browser and the subsequent programs
Spring MVC is an MVC framework based on Spring. The so-called MVC (model, controller, view) . The function of the entire Spring MVC is to interact with model (data) between controller (backend program) and view (front-end browser) based on Spring.
As for the advantages and disadvantages of Spring MVC, if you don’t understand it deeply, you won’t comment on it.
2. The referenced jar package
Since it is based on Spring, the core jar package (beans, context, core, expression, commons-logging) of Spring MVC is necessary; there is a (web, webmvc) related Jar package, and the special package (aop) package is not necessary, but if it is based on annotations, it is necessary when scanning the package.
3. Configuration file
A configuration file is an initialization file that explicitly configures the program execution. The configuration file is as follows:
springmvc-config.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-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!--<context:component-scan base-package="com.itheima.controller"/> --> <!-- Configure the processor Handle, map the "/firstController" request --> <bean name="/firstController" /> <!--<mvc:annotation-driven />--!> <!-- Processor mapper, search the name of the processor Handle as a url --> <bean /> <!-- Processor adapter, configure the call to the handleRequest() method in the processor --> <bean /> <!-- View parser--> <bean> </bean></beans>
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_3_1.xsd" version="3.1"> <servlet> <!-- Configure front-end filter--> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <!-- Load configuration file during initialization --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <!-- Indicates that the container loads the Servlet immediately upon startup --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>
Image source (Detailed explanation of the front-end controller architecture of spring mvc DispatcherServlet)
Steps: 1. The client initiates access and is intercepted by the front-end controller of Spring MVC (DispatcherServlet)
2. The interceptor will find the handlerMapping, let the mapper find the specific bean according to the URL. For example, if the URL "/firstController" above, it will find the corresponding bean and feedback it to the DispatcherServlet
3. DispatcherServlet hand over the found bean to the adapter (handlerAdapter), and the adapter calls the corresponding handler (executes the method in the bean)
4. After the execution is completed, return the result to the DispatcherServlet, and then hand it over to the view resolver (ViewReslover)
5. After the view parsing is completed, hand it over to the DispatcherServlet, and then hand it over to the view for rendering (such as JSP). Finally, the rendered result is fed back to the client
4. Controller class
package com.itheima.controller;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Controller class*/public class FirstController implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) { // Create ModelAndView object ModelAndView mav = new ModelAndView(); // Add data to the model object mav.addObject("msg", "This is my first Spring MVC program"); // Set the logical view name mav.setViewName("/WEB-INF/jsp/first.jsp"); // Return ModelAndView object return mav; }}The adapter used in the example (SimpleControllerHandlerAdapter) requires the handler to implement the Controller interface.
5. JSP
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Beginner</title></head><body> ${msg}</body></html>Test passes
PS: The above code is the code for the dark horse video tutorial, and I typed it manually.
Statement: This article is a beginner Spring MVC for taking notes. It is completely novice and has a simple understanding. If any major player is willing to give advice, I am very grateful.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.