参考文章出处:html
http://www.tutorialspoint.com/spring/spring_web_mvc_framework.htm
http://elf8848.iteye.com/blog/875830java
使用Spring的MVC框架能够灵活的开发web应用,减小应用的耦合性。MVC模式的目的就是把应用的几个方面分开来,好比输入逻辑,业务逻辑,用户界面逻辑。web
Model 封装了应用的数据,一般是由POJO组成。spring
View 的职责是把model的数据拿来输出成HTML供客户端浏览器解析。浏览器
Controller 负责处理用户请求,构建相应的model而且把model交给view使用。ruby
Spring web MVC框架的设计是围绕着一个 DispatcherServlet 来处理全部的HTTP requests 和 responses的。 下图所示为Spring Web MVC DispatcherServlet 的整个处理流程。mvc
DispatcherServlet处理HTTP请求的顺序以下:app
上述全部的组件,例如HandlerMapping, Controller, ViewResolver都是WebApplicationContext的一部分。而WebApplicationContext是由ApplicationContext扩展而来的,增长了一下web应用所必须的特性。 使用Spring MVC,配置DispatcherServlet是第一步。DispatcherServlet是一个Servlet, 因此能够配置多个DispatcherServlet。DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller(Action)来处理。框架
为了把request交给DispatcherServlet来处理,那么须要在web.xml中利用URL映射进行定义,以下所示,咱们定义了一个名字为HellWeb的servlet对应的DispatcerServlet的例子。jsp
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name> <servlet> <servlet-name>HelloWeb</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HelloWeb</servlet-name> <url-pattern>*.jsp</url-pattern> </servlet-mapping> </web-app>
web.xml 一般被放在目录 WebContent/WEB-INF 下,HelloWeb 这个DispatcherServlet 在初始化的过程当中, 框架会在web应用的WEB-INF文件夹下寻找名为[servlet-name]-servlet.xml 的配置文件, 生成文件中定义的bean。 在咱们这个例子当中,咱们的配置文件是HelloWeb-servlet.xml.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <context:component-scan base-package="com.ruby" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
默认的[servlet-name]-servlet.xml 文件会被用于bean的定义。咱们实例中servlet-name是HelloWeb,对应的bean配置文件是HelloWeb-servlet.xml
<context:component-scan...> 标签被用于激活Spring MVC 注解扫描,相似 @Controller, @RequestMapping这样的注解都会生效。
InternalResourceViewResolver 是定义如何解析view的名字。本例子中逻辑view名字hello被代理给了/WEB-INF/jsp/hello.jsp来实现。
DispatcherServlet 委派controllers来执行具体的功能性需求。 @Controller 注解代表这个类的角色是controller. @RequestMapping 注解用于将URL映射到实体类或者某个handler的方法上面。
@Controller @RequestMapping("/hello") public class HelloController{ @RequestMapping(method = RequestMethod.GET) public String printHello(ModelMap model) { model.addAttribute("message", "Hello Spring MVC Framework!"); return "hello"; }
}
这里@Controller 注解定义了这个类是一个Spring MVC的controller. 下面的@RequestMapping 注解代表这个controller的全部的处理方法都是关联到/hello 路径上面的. @RequestMapping(method = RequestMethod.GET) 注解用于声明方法printHello() 是controller默认的方法用于处理HTTP GET 请求. 一样,咱们也能够定义处理POST 请求的方法。
咱们能够用另一种形式写这个controller,以下
@Controller public class HelloController{ @RequestMapping(value = "/hello", method = RequestMethod.GET) public String printHello(ModelMap model) { model.addAttribute("message", "Hello Spring MVC Framework!"); return "hello"; } }
这个value 属性定义了handler方法对于的URL路径,method 属性定义了这个controller处理HTTP GET 请求. 有几点比较重要:
在service方法里面定义必要的业务逻辑。根据需求,能够在这个方法里面调用其余的方法。Based on the business
一个定义好的service 方法返回的是一个view的名字,这个view里面包含对应的model.这个例子中,返回的就是"hello" 这个逻辑view 的名字。
Spring MVC支持多种类型的view的呈现技术,包括JSPs, HTML, PDF, Excel, XML, Velocity templates, XSLT, JSON, Atom和RSS feeds, JasperReports等到。最经常使用的仍是用JSP temlaes加上JSTL。下面就是一个简单的/WEB-INF/jsp/hello.jsp的例子。
<html> <head> <title>Hello Spring MVC</title> </head> <body> <h2>${message}</h2> </body> </html>
这里的${message}属性是在controller里面已经被set过的。你能够在你的view里面显示更多的,相似message这样,被做为属性set进入model之中。
http://localhost:8080/HelloWeb/hello