Spring MVC of SSM_ 01_ 01_ SpringMVC - Introduction and execution process of SpringMVC

Posted by fastidious on Sat, 19 Feb 2022 20:17:19 +0100

Catalogue of series articles

1, What is spring MVC?


What are the controllers
servlet, action and controller are all concepts of controller,
But only controller is used in actual development. We call it controller directly

Step 1: import the jar package


Detailed operation
The first step is to create a web project and import the jar package


Then you can create a new current window and import these things,

Just copy it directly
You can find it in the spring package
You can also directly copy the lib in the completed project

Step 2: configure all requests to be managed by spring MVC


This can be copied directly without reciting it
It's OK to understand. There's no need to memorize one by one

<servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>


If you report red, change it
The reason for the error is that lib is not compiled


That's it

Step 3: create dispatcher servlet servlet in the WEB-INF directory xml

Note the dispatcher servlet servlet The name XML is fixed, and the default framework will look for this file

<?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"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
		
</beans>

Copy, put in, as follows

Note that the gray ones above represent those we don't use. Don't worry about them here. They can't be used now. They may be used later

Then the fourth step is to make some configuration in this file

Step 4: configure dispatcherservlet servlet xml

<!-- 1. Configure processor mapping, springmvc Default processor mapper
			 BeanNameUrlHandlerMapping:according to bean of name Attribute url Find handlerController -->
		<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
		<!-- 2. Configuration processor adapts to configurator execution Controller -->
		<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
		<!-- 3.Configuring a controller is relative to configuring an access path -->
		<bean name="/user.do" class="com.gyf.backoffice.web.controller.UserController"/>
		<!-- 4.to configure springmvc view resolver 
			The video path parsed by the view parser is: prefix + suffix -->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/views"/>
			<property name="suffix" value=".jsp"/>
		</bean>

Just write a lot of bean s
Copy the above code, or write it step by step according to the above code
Note that when configuring to step 3, our class name is to configure a controller
We can create a controller first

Step 5: create a Controller

Create a controller under src


After creating the controller, pay attention to the implementation
Attention is to realize!!! Not inheritance


Step 6: create a userlist JSP page


Create a new folder under WEN-INF


It should be noted that an additional inclined rod is added here

After configuration
Then the configuration path in java code is much simpler

This shortens the code of the path
Then if we want to be in userlist JSP page takes this value, how to get it
As before, use the el expression

Step 7: run the project and access the path


Let's see the specific operation
analysis
After all requests come, they will access this class first


Then run the project

The effect comes out when you visit

Find out why


Generally, this error is reported because our class is not integrated
Let's check
Notice, not this

Note that it's not the one above
But look at this


Encounter this kind of problem
First of all, we should have ideas to solve problems
Just two
Or the Tomcat version is incompatible. We can try a lower version, such as 7.9
Or there is something wrong with the configuration - the code problem
No, it's really the problem with this bag
Check one by one
After trying, it has nothing to do with Tomcat
That is to say, there is no wrong configuration after checking
Final solution:
Delete this bag


Run again, I'll go, or I can't
Think about the reason
After a long time
..........................................
Made a low-level mistake
The lib folder should be placed under WEB-INF
fuck

Then regenerate the lib package

Open the file location and copy a copy of lib, named lib copy,
Then delete the current lib and change the previous copy of lib to lib

Note that the class library must be named lib, which is a fixed name in the web project
That's why we've been changing



If you can't, just rebuild the project and import all the jar packages again
So the foundation is really important,
The foundation is not set up well, and the back is very complicated
After the project runs

The data is there

Execution process of spring MVC



This is the source code of controller processing
as follows


Summary:

What does springMvc learn?
In fact, the core of learning is how to receive parameters from the page. Just receive parameters
You only need to know how to receive the data of a page in the controller
That's all the routine behind it