SpringMVC environment setup-start with simplicity
What is SpringMVC? Many people are the first to want to know what he can do! How to use it? In fact, my own understanding of Spring MVC is an extension of the Spring framework. We usually develop websites without other frameworks. java Web comes with servlets that we are familiar with, right. Spring MVC is split into Model, View, and Controller. Spring helps us with these three things. Is the familiar MVC pattern!
1. Help us manage models (such as set and get management of User classes ).
2. How to transfer data to the View layer. (JSP, etc)
3. The Controller control layer is used to process page requests. (In fact, it is equivalent to our servlet ).
Creating a project... skipped --- go to Baidu or Google
To develop applications using the framework, maven is generally used to manage and customize the download of jar packages. If you have never heard of it, you can go to Baidu to get maven-related materials.
The simplest springmvc requires only one configuration.
Configuration of pom. xml
4.0.0
psspring
psspring
war
0.0.1-SNAPSHOT
psspring Maven Webapp
http://maven.apache.org
org.springframework
spring-webmvc
3.0.5.RELEASE
psspring
This section is used to import the jar package required by spring mvc.
org.springframework
spring-webmvc
3.0.5.RELEASE
Of course, the spring configuration file is required for spring web projects. This file is as follows:
Because we should try our best to understand what we need. This thing can both manage beans and make other configurations. In short, spring configuration files are all configured in this type of files. Of course, many times, we will configure different files to better differentiate them.
If you only have one file. Our text must be named the xx-servlet.xml springMVC and will go to the end file-servlet. xml during initialization. (This is his rule)
You can name this document a-servlet.xml or b-servlet.xml or anything else. This file must be placed in the WEB-INF directory. Why? You can study it...
Here is the meaning of automatic scanning and controller Management.
Base-package = "here is the location of your controller class" --- equivalent to our servlet class. (Personal understanding)
Next, configure the web. xml file.
The spring project must load the spring configuration file. As mentioned above, you only need to load the xml file of the nodes that contain beans.
ContextConfigLocation
/WEB-INF/x-servlet.xml
Org. springframework. web. context. ContextLoaderListener
X
Org. springframework. web. servlet. DispatcherServlet
X
/
Index. jsp
In this way, the springmvc environment has been set up.
Next we will write a Controller class for testing.
Package cn. ps. spring. controller; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. servlet. modelAndView; @ Controllerpublic class MainController {@ RequestMapping ("/abc") public ModelAndView test () {System. out. println ("Hello, spring MVC doesn't need to be so complicated! "); Return new ModelAndView ("/login. jsp ");}}
If @ Controller is added to any class, it can be used as the Controller class of spring to receive and process parameters for data transmission.
Http: /localhost: 8888/psspring/abc
In this way, you can access
Yes. This is the simplest Spring MVC environment. Spring is a very large framework. Anything you want to add can be customized. Slowly explore, you will like him.