直接干货html
model 考虑给用户展现什么。关注支撑业务的信息构成。构建成模型。前端
control 调用业务逻辑产生合适的数据以及传递数据给视图用于呈献;java
view怎样对数据进行布局,以一种优美的方式展现给用户;web
MVC核心思想:业务数据抽取和业务数据呈献相分离。spring
看看Spring MVC官网给的图:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.htmlexpress
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.spring-mvc
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).mvc
处理完后,还会返回结果给Front controller。而后前端控制器再去和View交互,最后response给用户。是否是很是其余MVC框架很是像呢?比方Struts 2.0 app
当中大概设计到这些概念(看不懂不要紧,后面文章会解释):先看个脸熟框架
DispatchServlet
Controller
HandlerAdapter
HandlerInterceptor
HandlerMapping
HandlerExecutionChain
ModelAndView
ViewResolver
View
好了,是否是已经火烧眉毛了呢?如下给个高速入门的实例,很是easy,仅仅有一个java文件。两个配置文件和一个终于的jsp文件:
<?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>
<?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 } }
<%@ 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>
@RequestMapping("/hello") @RequestMapping("/mvc")注解,会告诉Spring MVC这里是Controller,当前端控制器发送来的请求符合这些要求时。就交给它处理。最后会返回home.jsp,哪里的home.jsp?
看mvc-dispatcher-servlet.xml中的最后一部分,对视图解析器的配置。
你看会了吗?欢迎谈论 http://blog.csdn.net/code_7