Spring MVC series of teaching materials (1)-Tutorial
Must read: Framework-based programs must run successfully. For the version of the JAR package and the correctness of the configuration file, there are strict requirements. Anything wrong will cause the framework program to fail. If you are learning this framework for the first time, be sure to strictly follow the instructions of the tutorial and completely imitate the operation until you successfully see the running effect. After the first success, confidence and ideas will have a good foundation, and then according to their doubts, make the original changes and adjustments to the "successful" code, which can greatly save the time of learning and improve efficiency Do n’t make changes without permission, creating obstacles to your own learning
Step 1: first run, to see the effect, re-learning
Step 2: imitate and troubleshooting
Step 3: Create project springmvc
Step 4: introducing jar package
Step 5: web.xml
Step 6: Create springmvc-servlet.xml
Step 7: Control class IndexController
step 8: prepare index.jsp
step 9: deploy in the tomcat restart testing
step 10: schematic
step 11: Download the complete project
step 12: exercise
Step 2: imitate and troubleshooting
Step 3: Create project springmvc
Step 4: introducing jar package
Step 5: web.xml
Step 6: Create springmvc-servlet.xml
Step 7: Control class IndexController
step 8: prepare index.jsp
step 9: deploy in the tomcat restart testing
step 10: schematic
step 11: Download the complete project
step 12: exercise
Step 1: Run first, see the effect, then learn
Spring MVC needs to do a lot of steps. If any step is missed or wrong, it may fail, which will affect the confidence of learning and mistakenly believe that this tutorial will not work.
So first download the download area (https://how2j.cn/p/5329) can run the project springmvc.rar, unpacked into eclipse, start Tomcat, see if functioning properly. Make sure you can run, make sure the tutorial can run, and then learn the following content.
For the method of importing into Eclipse and running, please refer to: After importing a dynamic Web project into Eclipse and
deploying successfully, test the address and you should see the effect as shown in the figure
Note: This is a dynamic project format does not support an independent way to deploy Tomcat
So first download the download area (https://how2j.cn/p/5329) can run the project springmvc.rar, unpacked into eclipse, start Tomcat, see if functioning properly. Make sure you can run, make sure the tutorial can run, and then learn the following content.
For the method of importing into Eclipse and running, please refer to: After importing a dynamic Web project into Eclipse and
deploying successfully, test the address and you should see the effect as shown in the figure
http:
//127.0.0.1:8080/springmvc/index
|
Note: This is a dynamic project format does not support an independent way to deploy Tomcat
Step 2: imitate and troubleshoot
After ensuring that the runnable project runs correctly, follow the steps of the tutorial strictly and imitate the code again.
The imitation process will inevitably lead to discrepancies in the code, resulting in the inability to obtain the expected operating results. At this moment, by comparing the correct answer (runnable item) and your own code, you can locate the problem.
In this way, learning is effective and troubleshooting is efficient, which can significantly improve the learning speed and cross the threshold of learning.
It is recommended to use diffmerge software for folder comparison. Compare your own project folder with my executable project folder.
This software is very fast hardware, you can know which of the two file folder wrong, and clearly marked
here provides installation and use of green Tutorial: DiffMerge download and tutorial
The imitation process will inevitably lead to discrepancies in the code, resulting in the inability to obtain the expected operating results. At this moment, by comparing the correct answer (runnable item) and your own code, you can locate the problem.
In this way, learning is effective and troubleshooting is efficient, which can significantly improve the learning speed and cross the threshold of learning.
It is recommended to use diffmerge software for folder comparison. Compare your own project folder with my executable project folder.
This software is very fast hardware, you can know which of the two file folder wrong, and clearly marked
here provides installation and use of green Tutorial: DiffMerge download and tutorial
Step 3: Create project springmvc
Create a new project springmvc in eclipse, using dynamic web project. Students who are not familiar with this method, please refer to
the way to develop J2EE applications using Dynamic Web Project
Step 4: import jar package
Download
download area (click to enter)
lib.rar and copy it to unzip e: / project / springmvc / WebContent / WEB-INF / lib directory
Step 5: web.xml
Created under the WEB-INF directory web.xml
configuration entries DispatcherServlet Spring MVC, and all requests are submitted to the Servlet
Note: <the servlet-name> SpringMVC </ the servlet-name>
SpringMVC This name will be used in the next step
configuration entries DispatcherServlet Spring MVC, and all requests are submitted to the Servlet
Note: <the servlet-name> SpringMVC </ the servlet-name>
SpringMVC This name will be used in the next step
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
version
=
"2.4"
xmlns
=
"http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<
servlet
>
<
servlet-name
>springmvc</
servlet-name
>
<
servlet-class
>
org.springframework.web.servlet.DispatcherServlet
</
servlet-class
>
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>springmvc</
servlet-name
>
<
url-pattern
>/</
url-pattern
>
</
servlet-mapping
>
</
web-app
>
|
Step 6: create springmvc-servlet.xml
Create springmvc-servlet.xml
springmvc-servlet.xml in the WEB-INF directory and the previous step
springmvc corresponds
This is Spring MVC profile map
showing access paths / index will give indexController the process id = bean
id = indexController bean configuration of the class: IndexController
springmvc-servlet.xml in the WEB-INF directory and the previous step
<servlet-name>springmvc</servlet-name>
|
springmvc corresponds
This is Spring MVC profile map
showing access paths / index will give indexController the process id = bean
id = indexController bean configuration of the class: IndexController
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<
beans
>
<
bean
id
=
"simpleUrlHandlerMapping"
class
=
"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
>
<
property
name
=
"mappings"
>
<
props
>
<
prop
key
=
"/index"
>indexController</
prop
>
</
props
>
</
property
>
</
bean
>
<
bean
id
=
"indexController"
class
=
"controller.IndexController"
></
bean
>
</
beans
>
|
Step 7: Control class IndexController
The control class IndexController implements the interface Controller and provides the method handleRequest to process the request.
Spring MVC combines the model and the view through the ModelAndView object
Indicates that the view is index.jsp The
model data is message, and the content is "Hello Spring MVC"
Spring MVC combines the model and the view through the ModelAndView object
ModelAndView mav =
new
ModelAndView(
"index.jsp"
);
mav.addObject(
"message"
,
"Hello Spring MVC"
);
|
Indicates that the view is index.jsp The
model data is message, and the content is "Hello Spring MVC"
package
controller;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.springframework.web.servlet.ModelAndView;
import
org.springframework.web.servlet.mvc.Controller;
public
class
IndexController
implements
Controller {
public
ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws
Exception {
ModelAndView mav =
new
ModelAndView(
"index.jsp"
);
mav.addObject(
"message"
,
"Hello Spring MVC"
);
return
mav;
}
}
|
Step 8: prepare index.jsp
Create index.jsp in the WebContent directory
index.jsp is very simple, display the content of the message through the EL expression
index.jsp is very simple, display the content of the message through the EL expression
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<
h1
>${message}</
h1
>
|
Step 9: Deploy in tomcat, restart test
Deploy in Tomcat, restart tomcat, and then visit the address to observe the effect
For the deployment method, please refer to Tomcat-Run On Server via Eclipse
http:
//127.0.0.1:8080/springmvc/index
|
For the deployment method, please refer to Tomcat-Run On Server via Eclipse
步骤 10 : 原理图
1. 用户访问 /index
2. 根据web.xml中的配置 所有的访问都会经过DispatcherServlet
3. 根据 根据配置文件springmvc-servlet.xml ,访问路径/index
会进入IndexController类
4. 在IndexController中指定跳转到页面index.jsp,并传递message数据
5. 在index.jsp中显示message信息
2. 根据web.xml中的配置 所有的访问都会经过DispatcherServlet
3. 根据 根据配置文件springmvc-servlet.xml ,访问路径/index
会进入IndexController类
4. 在IndexController中指定跳转到页面index.jsp,并传递message数据
5. 在index.jsp中显示message信息
步骤 11 : 完整的项目下载
一般说来,根据步骤一步一步的做过来,就可以运行看到结果了。
如果实在看不到结果,多半是因为中间某个步骤不够仔细,大小写错误,多了个下划线等等细微的错误。
如果是这样,还可以在右侧下载完整的项目保证一定可以走通
如果实在看不到结果,多半是因为中间某个步骤不够仔细,大小写错误,多了个下划线等等细微的错误。
如果是这样,还可以在右侧下载完整的项目保证一定可以走通
步骤 12 : 练习
本例实现了访问路径/index,服务端跳转到index.jsp的效果
练习:访问路径/hello 服务端跳转到hello.jsp
练习:访问路径/hello 服务端跳转到hello.jsp
More SpringMVC learning content: https://how2j.cn/k/springmvc/springmvc-springmvc/615.html?p=144217