引用自: 的博客
关于SpringMVC的介绍我就不多说了,网上一搜一大堆,好多大鸟的博客都有详细的描述,之前看的跟开涛学SpringMVC,写的非常好,SpringMVC运行的流程和原理讲的非常的细致在此我引用一下开涛前辈的图片和文字,大家要是想看原文就点击上面的链接。
SpringMVC处理请求的流程图
大家一定要仔细的看,最好是拿张纸,画一画,可比你光看有效果,大家可以与纯MVC模式对比一下,这样理解起来就不是那么的难了。
对上面的图在此细化
核心开发步骤:
DispatcherServlet在web.xml中的部署描述,从而拦截请求到Spring Web MVC
HandlerMapping的配置,从而将请求映射到处理器
HandlerAdapter的配置,从而支持多种类型的处理器
ViewResolver的配置,从而将逻辑视图名解析为具体视图技术
处理器(页面控制器)的配置,从而进行功能处理
把上面的五个步骤和图一一对应起来,之后的开发就是按照这五个步骤进行的!
后来我想实现一个简单的用户登录验证的例子来验证一下,毕竟实践是验证理论的唯一途径也是学习的最好方式!
没有用到数据库,没有必要,我这里只是学习SpringMVC的运行机制
首先建立一个Web项目取名为SpringLoginDemo
在导入所需要的包,如图:(下面有下载)
接下来做什么呢?看上面的那五个步骤,
1、部署web.xml
第一步部署web.xml,从而拦截请求到spring Web MVC
1. <?xml version="1.0" encoding="UTF-8"?>
2. <web-app version="2.5"
3. xmlns="http://java.sun.com/xml/ns/javaee"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
7. <display-name></display-name>
8. <welcome-file-list>
9. <welcome-file>index.jsp</welcome-file>
10. </welcome-file-list>
11. <servlet>
12. <servlet-name>Dispatcher</servlet-name>
13. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
14. <!-- Spring配置文件 -->
15. <init-param>
16. <param-name>contextConfigLocation</param-name>
17. <param-value>classpath:applicationContext.xml</param-value>
18. </init-param>
19. </servlet>
20. <servlet-mapping>
21. <servlet-name>Dispatcher</servlet-name>
22. <url-pattern>*.do</url-pattern>
23. </servlet-mapping>
24. </web-app>
2、编写eneity类,User.java
1. package com.example.entity;
2.
3. public class User {
4. private String username;
5. private String password;
6.
7. public User(String username, String password) {
8. super();
9. this.username = username;
10. this.password = password;
11. }
12. public String getUsername() {
13. return username;
14. }
15. public void setUsername(String username) {
16. this.username = username;
17. }
18. public String getPassword() {
19. return password;
20. }
21. public void setPassword(String password) {
22. this.password = password;
23. }
24. }
3、LoginController.java类
1. package com.example.controller;
2.
3. import java.util.HashMap;
4. import java.util.Map;
5.
6. import javax.servlet.http.HttpServletRequest;
7. import javax.servlet.http.HttpServletResponse;
8.
9. import org.springframework.web.servlet.ModelAndView;
10. import org.springframework.web.servlet.mvc.AbstractController;
11.
12. import com.example.entity.User;
13.
14. public class LoginController extends AbstractController {
15. //成功与失败字段
16. private String successView;
17. private String failView;
18.
19. public String getSuccessView() {
20. return successView;
21. }
22.
23. public void setSuccessView(String successView) {
24. this.successView = successView;
25. }
26.
27. public String getFailView() {
28. return failView;
29. }
30.
31. public void setFailView(String failView) {
32. this.failView = failView;
33. }
34.
35. @Override
36. protected ModelAndView handleRequestInternal(HttpServletRequest request,
37. HttpServletResponse response) throws Exception {
38. //不应该是这样写,但是这样看起来比较容易理解
39. String username = request.getParameter("username");
40. String password = request.getParameter("password");
41. User user = getUser(username, password);
42. //保存相应的参数,通过ModelAndView返回
43. Map<String ,Object> model=new HashMap<String,Object>();
44. if(user !=null){
45. model.put("user", user);
46. return new ModelAndView(getSuccessView(),model);
47. }else{
48. model.put("error", "用户名或密码输入错误!!!");
49. return new ModelAndView(getFailView(),model);
50. }
51. }
52. //为了方便直接写的验证方法
53. public User getUser(String username,String password){
54. if(username.equals("test") && password.equals("test")){
55. return new User(username,password);
56. }else{
57. return null;
58. }
59. }
60. }
4、核心配置applicationContext.xml
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:aop="http://www.springframework.org/schema/aop"
5. xmlns:tx="http://www.springframework.org/schema/tx"
6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
9. <bean id="loginController" class="com.example.controller.LoginController">
10. <!-- 注意这里的两个属性,对应的是两个需要跳转的页面,一个是显示用户,一个是登录失败还是登录界面 -->
11. <property name="successView" value="showUser"></property>
12. <property name="failView" value="login"></property>
13. </bean>
14. <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
15. <property name="mappings">
16. <props>
17. <prop key="/login.do">loginController</prop>
18. </props>
19. </property>
20. </bean>
21. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
22. <property name="prefix" value="/"></property>
23. <property name="suffix" value=".jsp"></property>
24. </bean>
25. </beans>
来看一下图片
5、三个jsp页面
index.jsp
1. <%@ page language="java" contentType="text/html; charset=GB18030"
2. pageEncoding="GB18030"%>
3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4. <html>
5. <head>
6. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
7. <title>Insert title here</title>
8. </head>
9. <body>
10. <a href="login.jsp">进入</a>
11. </body>
12. </html>
login.jsp
1. <%@ page language="java" contentType="text/html; charset=GB18030"
2. pageEncoding="GB18030"%>
3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4. <html>
5. <head>
6. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
7. <title>Insert title here</title>
8. </head>
9. <body>
10. ${error }
11. <form action="login.do" method="post">
12. 用户登陆<br>
13. <hr>
14. 用户名:<input type="text" name="username"><br>
15. 密码:<input type="text" name="password"><br>
16. <input type="submit" value="登陆">
17. </form>
18. </body>
19. </html>
showUser.jsp
1. <%@ page language="java" contentType="text/html; charset=GB18030"
2. pageEncoding="GB18030"%>
3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4. <html>
5. <head>
6. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
7. <title>Insert title here</title>
8. </head>
9. <body>
10. 用户信息<br>
11. 用户名:${user.username }<br>
12. 密码:${user.password }<br>
13. <font color="red">欢迎登陆!!!</font><br>
14. </body>
15. </html>
6、启动Tomcat服务器运行
附件源码
下载地址 : https://gitee.com/KingXin666/SpringMVCDemo