转自:http://com-xpp.iteye.com/blog/1604183

SpringMVC框架图

 

SpringMVC接口解释

 

DispatcherServlet接口:

Spring提供的前端控制器,所有的请求都有经过它来统一分发。在DispatcherServlet将请求分发给Spring Controller之前,需要借助于Spring提供的HandlerMapping定位到具体的Controller。

HandlerMapping接口:

能够完成客户请求到Controller映射。

Controller接口:

需要为并发用户处理上述请求,因此实现Controller接口时,必须保证线程安全并且可重用。Controller将处理用户请求,这和Struts Action扮演的角色是一致的。一旦Controller处理完用户请求,则返回ModelAndView对象给DispatcherServlet前端控制器,ModelAndView中包含了模型(Model)和视图(View)。从宏观角度考虑,DispatcherServlet是整个Web应用的控制器;从微观考虑,Controller是单个Http请求处理过程中的控制器,而ModelAndView是Http请求过程中返回的模型(Model)和视图(View)。

ViewResolver接口:

Spring提供的视图解析器(ViewResolver)在Web应用中查找View对象,从而将相应结果渲染给客户。

SpringMVC运行原理

 

1.客户端请求提交到DispatcherServlet

2.由DispatcherServlet控制器查询一个或多个HandlerMapping,找到处理请求的Controller

3.DispatcherServlet将请求提交到Controller

4.Controller调用业务逻辑处理后,返回ModelAndView

5.DispatcherServlet查询一个或多个ViewResoler视图解析器,找到ModelAndView指定的视图

6.视图负责将结果显示到客户端

SpringMVC运行实例

Account类:

  1. package com.pb.entity;
  2. public class Account {
  3. private String cardNo;
  4. private String password;
  5. private float balance;
  6. public String getCardNo() {
  7. return cardNo;
  8. }
  9. public void setCardNo(String cardNo) {
  10. this.cardNo = cardNo;
  11. }
  12. public String getPassword() {
  13. return password;
  14. }
  15. public void setPassword(String password) {
  16. this.password = password;
  17. }
  18. public float getBalance() {
  19. return balance;
  20. }
  21. public void setBalance(float balance) {
  22. this.balance = balance;
  23. }
  24. }

LoginController类:

  1. package com.pb.web.controller;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.springframework.web.servlet.ModelAndView;
  7. import org.springframework.web.servlet.mvc.AbstractController;
  8. import com.pb.entity.Account;
  9. public class LoginController extends AbstractController {
  10. private String successView;
  11. private String failView;//这两个参数是返回值传给applicationContext.xml,进行页面分配
  12. public String getSuccessView() {
  13. return successView;
  14. }
  15. public void setSuccessView(String successView) {
  16. this.successView = successView;
  17. }
  18. public String getFailView() {
  19. return failView;
  20. }
  21. public void setFailView(String failView) {
  22. this.failView = failView;
  23. }
  24. @Override
  25. protected ModelAndView handleRequestInternal(HttpServletRequest request,
  26. HttpServletResponse response) throws Exception {
  27. // TODO Auto-generated method stub
  28. String cardNo=request.getParameter("cardNo");
  29. String password=request.getParameter("password");
  30. Account account =getAccount(cardNo,password);
  31. Map<String ,Object> model=new HashMap<String,Object>();
  32. if(account !=null){
  33. model.put("account", account);
  34. return new ModelAndView(getSuccessView(),model);
  35. }else{
  36. model.put("error", "卡号和密码不正确");
  37. return new ModelAndView(getFailView(),model);
  38. }
  39. }//本应该这个方法写在模型层,这地方直接给放在了逻辑层这个地方偷懒了。
  40. public Account getAccount(String cardNo,String password){
  41. if(cardNo.equals("123")&&password.equals("123")){
  42. Account account =new Account();
  43. account.setCardNo(cardNo);
  44. account.setBalance(88.8f);
  45. return account;
  46. }else{
  47. return null;
  48. }
  49. }
  50. }

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.pb.web.controller.LoginController">
  10. <property name="successView" value="showAccount"></property>
  11. <property name="failView" value="login"></property>
  12. </bean>
  13. <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  14. <property name="mappings">
  15. <props>
  16. <prop key="/login.do">loginController</prop>
  17. </props>
  18. </property>
  19. </bean>
  20. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  21. <property name="prefix" value="/"></property>
  22. <property name="suffix" value=".jsp"></property>
  23. </bean>
  24. </beans>

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="cardNo"><br>
  15. 密码:<input type="text" name="password"><br>
  16. <input type="submit" value="登陆">
  17. </form>
  18. </body>
  19. </html>

showAccount.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. 卡号:${account.cardNo }<br>
  12. 密码:${account.password }<br>
  13. 钱数:${account.balance }<br>
  14. </body>
  15. </html>

Web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/j2ee"
  4. xmlns:javaee="http://java.sun.com/xml/ns/javaee"
  5. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  6. <welcome-file-list>
  7. <welcome-file>index.jsp</welcome-file>
  8. </welcome-file-list>
  9. <servlet>
  10. <servlet-name>Dispatcher</servlet-name>
  11. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  12. <init-param>
  13. <param-name>contextConfigLocation</param-name>
  14. <param-value>classpath:applicationContext.xml</param-value>
  15. </init-param>
  16. </servlet>
  17. <servlet-mapping>
  18. <servlet-name>Dispatcher</servlet-name>
  19. <url-pattern>*.do</url-pattern>
  20. </servlet-mapping>
  21. </web-app>
 

【转载】SpringMVC框架介绍的更多相关文章

  1. SpringMVC 框架介绍以及环境搭建

    目录 前端设计模式介绍 分析前端设计模式 Spring MVC简单介绍 Spring和Spring MVC的关系 配置Spring MVC的环境并简单测试 前端设计模式介绍 前端设计模式其实和前端没啥 ...

  2. SpringMVC框架介绍

     1. SpringMVC通过一套MVC注解,让POJO成为处理请求的控制器,而无须实现任何接口. 2.支持REST风格的URL请求. 3.采用了松散耦合可插拔组件结构,比其他MVC框架更具扩展性和灵 ...

  3. springMVC框架介绍以及运行流程(图解)

    1 Springmvc是什么? spring web mvc和struts2都属于表现层的框架,spring web mvc是spring框架的一部分(所以spring mvc与spring之间不需要 ...

  4. SpringMVC学习笔记:SpringMVC框架的执行流程

    一.MVC设计模式 二.SpringMVC框架介绍 三.SpringMVC环境搭建 四.SpringMVC框架的请求处理流程及体系结构

  5. Spring框架介绍和原理

    SpringMVC框架介绍 1) Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面. Spring 框架提供了构建 Web 应用程序的全功 ...

  6. SpringMVC由浅入深day01_1springmvc框架介绍

    springmvc 第一天 springmvc的基础知识 课程安排: 第一天:springmvc的基础知识 什么是springmvc? springmvc框架原理(掌握) 前端控制器.处理器映射器.处 ...

  7. myBatis+Spring+SpringMVC框架面试题整理

    myBatis+Spring+SpringMVC框架面试题整理(一) 2018年09月06日 13:36:01 新新许愿树 阅读数 14034更多 分类专栏: SSM   版权声明:本文为博主原创文章 ...

  8. YARN基本框架介绍

    YARN基本框架介绍 转载请注明出处:http://www.cnblogs.com/BYRans/ 在之前的博客<YARN与MRv1的对比>中介绍了YARN对Hadoop 1.0的完善.本 ...

  9. MJExtension框架介绍

    MJExtension框架介绍 标签: MJExtension 2015-05-01 08:22 1120人阅读 评论(0) 收藏 举报  分类: Foundation(14)  版权声明:本文为博主 ...

随机推荐

  1. 题解 P5718 【【深基4.例2】找最小值】

    题目传送门 思路 介绍一种新方法--sort排序,它的格式是这样的sort(a+1,a+n+1,cmp);,我们只需要把a数组排好序,然后输出第\(1\)个元素即可. 定义a数组与变量\(n\)并输入 ...

  2. ISCC2018 Reverse & Pwn writeup

    Reference:L1B0 Re RSA256 春秋欢乐赛原题..flag都不变的 给了三个加密文件和公钥证书public.key,可以使用openssl进行处理 $openssl rsa -pub ...

  3. LVM实现将2块磁盘总空间“合二为一”并挂载到同一目录

    需求场景 将MySQL主机的2块18T的数据盘空间全部"合并"后挂载到/mysql_data目录下,要求文件系统格式化为xfs:已有关键信息梳理如下: 需要挂载的数据盘 /dev/ ...

  4. 【巨杉数据库SequoiaDB】巨杉数据库荣获《金融电子化》“金融科技创新奖”

    巨杉助力金融科技创新 2019年12月19日,由<金融电子化>杂志社主办.北京金融科技产业联盟协办的“2019中国金融科技年会暨第十届金融科技及服务优秀创新奖颁奖典礼”在京成功召开.来自金 ...

  5. C++-POJ1502-MPI Maelstrom-[最短路][spfa][栈优化]

    我不理解为什么写dijkska就WA呢? atoi()是个好东西,给你个颜色,自己体会 疑惑!疑惑!疑惑! #include <queue> #include <cstdio> ...

  6. Web Workers - (Worker(专有) and SharedWorker(共享))

    Web Worker为Web内容在后台线程中运行脚本提供了一种简单的方法 线程可以执行任务而不干扰用户界面 可以使用XMLHttpRequest执行 I/O (尽管responseXML和channe ...

  7. linux基础安全

    一.用户防护 Chage  -l  用户名   查看用户账号有效期信息 Chage  -E   时间   用户名   给用户设置过期时间 /etc/login.defs  文件保存许多默认设置 Pas ...

  8. 安装 centos8.1

    阿里云镜像下载链接 http://mirrors.aliyun.com/centos/8.1.1911/isos/x86_64/ 选择 CentOS-8.1.1911-x86_64-dvd1.iso ...

  9. WSO2 ESB XML定义语法(3)

    6.Property Mediator 通过Synapse调解的每条消息都可以具有一组关联的属性.Synapse引擎和底层传输在处理的每条消息上设置了许多属性,用户可以操纵这些属性来修改消息流的运行时 ...

  10. xmlns:android="http://schemas.android.com/apk/res/android 这句话的作用

    xmlns:android="http://schemas.android.com/apk/res/android 有了这句话,你就可以alt+/作为提示,提示你输入什么,不该输入什么,什么 ...