直接干货

model 考虑给用户展示什么。关注支撑业务的信息构成。构建成模型。

control 调用业务逻辑产生合适的数据以及传递数据给视图用于呈献;

view怎样对数据进行布局,以一种优美的方式展示给用户;

MVC核心思想:业务数据抽取和业务数据呈献相分离。

看看Spring MVC官网给的图:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

Spring’sweb MVC framework is, like many other web MVC frameworks, request-driven, designed around a central Servlet that dispatchesrequests to controllers and offers other functionality that facilitates thedevelopment
of web applications.
Spring’s DispatcherServlet however, doesmore than just that. It is completely integrated with the Spring IoC containerand as such allows you to use every other feature that Spring has.

Therequest processing workflow of the Spring Web MVC DispatcherServlet isillustrated in the following diagram.Thepattern-savvy reader will recognize that the DispatcherServlet is an expressionof the "Front Controller"
design pattern (this is a pattern thatSpring Web MVC shares with many other leading web frameworks).





简单理解就是:client发过来的请求,首先被交给叫做DispatcherServlet的前端控制器去处理,由它决定交给哪个Control去处理。

处理完后,还会返回结果给Front controller。然后前端控制器再去和View交互,最后response给用户。是不是非常其他MVC框架非常像呢?比方Struts 2.0

当中大概设计到这些概念(看不懂没关系,后面文章会解释):先看个脸熟

DispatchServlet

Controller

HandlerAdapter

HandlerInterceptor

HandlerMapping

HandlerExecutionChain

ModelAndView

ViewResolver

View

好了,是不是已经迫不及待了呢?以下给个高速入门的实例,非常easy,仅仅有一个java文件。两个配置文件和一个终于的jsp文件:

首先是web.xml

<?

xml version="1.0" encoding="UTF-8"?

>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Spring MVC</display-name> <servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatchServlet相应的上下文配置,默觉得/WEB-INF/$servlet-name$-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<!-- mvc-dispatcher拦截全部的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

然后是:mvc-dispatcher-servlet.xml

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!--激活@Required @Autowired,JSP250'S @PostConstruct, @PreDestroy @Resource等标注 -->
<context:annotation-config/> <!--DispatcherServlet上下文,仅仅搜索@Controller标注的类。不搜索其它搜索的类 -->
<context:component-scan base-package="com.xidian.mvcdemo.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> <!--启用HandlerMapping标签 -->
<mvc:annotation-driven/>
<!--ViewResovlver启用。视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!--存放jsp文件的目录位置 -->
<property name="prefix" value="/WEB-INF/jsps/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

HelloMvcConntroller.java

package com.xidian.mvcdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/hello")
//提示Spring MVC这是一个Controller,以及拦截根文件夹下的hello
public class HelloMvcConntroller { //host:port/hello/mvc
@RequestMapping("/mvc")
public String helloMvc(){
return "home"; //返回home.jsp
}
}

最后是一个home.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'home.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> </head> <body>
Hello Spring MVC <br>
</body>
</html>

能够这样理解程序之间的关系:首先在web.xml中我们用servlet拦截了全部请求交给Spring MVC的Dispatcher,然后它去找对应文件夹下的mvc-dispatcher-servlet.xml文件(也能够不设置,会在默认位置载入文件,代码中有说明,这里仅仅是帮助养成良好的文件归档习惯)。我们在对应的HelloMvcConntroller.java中加上的@Controller

@RequestMapping("/hello")  @RequestMapping("/mvc")注解,会告诉Spring MVC这里是Controller,当前端控制器发送来的请求符合这些要求时。就交给它处理。最后会返回home.jsp,哪里的home.jsp?

看mvc-dispatcher-servlet.xml中的最后一部分,对视图解析器的配置。

你看会了吗?欢迎谈论 http://blog.csdn.net/code_7

Spring MVC新手教程(一)的更多相关文章

  1. Spring MVC新手教程(二)

    第一篇文章宏观讲了Spring MVC概念,以及分享了一个高速入门的样例. 这篇文章主要来谈谈Spring MVC的配置文件. 首先来谈谈web.xml: web项目启动时自己主动载入到内存中的信息, ...

  2. Spring MVC 入门教程示例 (一)

    今天和大家分享下  Spring MVC  入门教程 首先还是从 HelloWorld  web 工程开始 -------------------------- 1.首先创建一个Maven Web工程 ...

  3. spring mvc入门教程 转载自【http://elf8848.iteye.com/blog/875830】

    目录  一.前言二.spring mvc 核心类与接口三.spring mvc 核心流程图 四.spring mvc DispatcherServlet说明 五.spring mvc 父子上下文的说明 ...

  4. spring MVC入门教程

    写一个spring mvc后台传值到前台的一个小例子. 分为以下几个步骤: 1.创建web项目. 导入项目包.具体有如下: spring-aop-4.0.4.RELEASE.jar spring-be ...

  5. 160506、Spring mvc新手入门(11)-返回json 字符串的其他方式

    Spring MVC返回 json字符串的方式有很多种方法,这里介绍最简单,也是最常使用的两种方式 一.使用  PrintWriter printWriter  直接输出字符串到返回结果中    不需 ...

  6. 使用idea创建spring mvc项目图文教程

    使用idea创建spring mvc项目图文教程 前言: 使用惯了eclipse的朋友,如果刚换成了idea或许有些不习惯.但是使用idea之后,就会love上idea了.本文将通过图文讲解怎么通过i ...

  7. Spring MVC【入门】就这一篇!

    MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1: 出现的弊端: JSP 和 Ja ...

  8. Spring MVC 简介及入门小例子

    说明:文章内容全部截选自实验楼教程[Spring MVC 简易教程] 一.什么是 Spring MVC Spring MVC 属于 SpringFrameWork 的后续产品,已经融合在 Spring ...

  9. IDEA中配置Maven+spring MVC+tomcat

    一:配置Maven安装教程如下: http://blog.csdn.net/qq_32588349/article/details/51461182 实际安装过程中,如果按照教程配置如下属性,最后创建 ...

随机推荐

  1. CF 351A - Jeff and Rounding DP

    http://codeforces.com/problemset/problem/351/C 题意:有2*n个浮点数a1,a2,a3...a2*n,把他们分成n队,对于每对<A,B>,对A ...

  2. 【转】Linux下history命令用法

    转自:http://blog.sina.com.cn/s/blog_5caa94a00100gyls.html 如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你 ...

  3. 关于学习C语言

    c语言作为一种计算机的语言,我们学习它,有助于我们更好的了解计算机,与计算机进行交流,因此,c语言的学习对我们尤其重要. 在这个星期里,我们专业的学生在专业老师的带领下进行了c语言程序实践学习.在这之 ...

  4. C#学习-处理Excel

    首先先了解下一个Excel文件的组成 1.一个Excel包含多个工作表(Sheet) 2.一个工作表(Sheet)包含多行(Row) 3.一行(Row)包含多个单元格(Cell)   如何判断一个单元 ...

  5. B树、B+树、红黑树、AVL树比较

    B树是为了提高磁盘或外部存储设备查找效率而产生的一种多路平衡查找树. B+树为B树的变形结构,用于大多数数据库或文件系统的存储而设计. B树相对于红黑树的区别 在大规模数据存储的时候,红黑树往往出现由 ...

  6. js基础---object对象

    //**********************************复杂JSON举例**************************************** var Jsondata={d ...

  7. Android Unable to add window -- token android.os.BinderProxy@3a067204 is not valid错误分析记录

    打开APP时,出现闪退的情况,查看android studio报错信息,主要为: Unable to add window -- token android.os.BinderProxy@3a0672 ...

  8. Linux学习(一)--基本概念

    一.Linux概述 Linux是一款全球性的免费的开源的操作系统平台,其特点是实现了多任务多用户处理,主要是依赖内核kernel shell,且占用资源少 (最小配置只要4Mb内存就能运行). 百度百 ...

  9. ajax 实现输入提示效果

    网站主页 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  10. html5——多媒体(二)

    基本方法 load() //重新加载视频 play() //播放 pause() //暂停 基本属性 currentTime //视频播放的当前进度. duration //视频的总时间 paused ...