Spring MVC新手教程(一)

直接干货

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

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

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

  

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

 

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

Spring MVC新手教程(一)_第1张图片


          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"?

> Spring MVC mvc-dispatcher org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/configs/spring/mvc-dispatcher-servlet.xml 1 mvc-dispatcher /


然后是:mvc-dispatcher-servlet.xml



		 
  	 
  
		 
  	 
  
  		
   
  
  
  
  
  
	   
	  
	   
	   
  

  
 

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+"/";
%>



  
    
    
    My JSP 'home.jsp' starting page
    
	
	
	    
	
	

  
  
  
    Hello Spring MVC 

能够这样理解程序之间的关系:首先在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新手教程(一))