spring web mvc 是一種基於java的使用了請求-響應模型的輕量級web框架,它使用了web mvc的設計模式思想, 將web層的職責進行解耦, 簡化了咱們平常的開發流程。主要分爲如下部分:html
具體的執行步驟以下:前端
核心架構的具體流程步驟以下:java
在此咱們能夠看出具體的核心開發步驟:web
<?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>springMvc</display-name> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> ${message} </body> </html>
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 使用註解的包,包括子集 --> <context:component-scan base-package="springMvc" /> <!-- HandlerMapping --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <!-- HandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <!-- Controller --> <bean name="/hello" class="springMvc.HelloWorldController" /> <!-- 視圖解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp"></property> </bean> </beans>
package springMvc; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; public class HelloWorldController implements org.springframework.web.servlet.mvc.Controller { @Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { final ModelAndView mv = new ModelAndView(); mv.setViewName("index"); mv.addObject("message", "hello, spring MVC!"); return mv; } }
一、 首先用戶發送請求http://localhost:8080/springMvc/hello ——>web容器, 容器,web容器根據「/hello」路徑映射到DispatcherServlet(url-pattern爲/)進行處理;spring
二、 DispatcherServlet——>BeanNameUrlHandlerMapping進行請求處處理的映射,BeanNameUrlHandlerMapping將「/hello」路徑直接映射到名字爲「/hello」的Bean進行處理,即HelloWorldController,BeanNameUrlHandlerMapping將其包裝爲HandlerExecutionChain(只包括HelloWorldController處理器,沒有攔截器);設計模式
三、 DispatcherServlet——> SimpleControllerHandlerAdapter,SimpleControllerHandlerAdapter將HandlerExecutionChain中的處理器(HelloWorldController)適配爲SimpleControllerHandlerAdapter;api
四、 SimpleControllerHandlerAdapter——> HelloWorldController處理器功能處理方法的調用,SimpleControllerHandlerAdapter將會調用處理器的handleRequest方法進行功能處理,該處理方法返回一個ModelAndView給DispatcherServlet;spring-mvc
五、 hello(ModelAndView的邏輯視圖名)——>InternalResourceViewResolver, InternalResourceViewResolver使用JstlView,具體視圖頁面在/WEB-INF/jsp/index.jsp;tomcat
六、 JstlView(/WEB-INF/jsp/index.jsp)——>渲染,將在處理器傳入的模型數據(message=HelloWorld!)在視圖中展現出來;cookie
七、 返回控制權給DispatcherServlet,由DispatcherServlet返回響應給用戶,到此一個流程結束。
spring mvc使用了org.springframework.web.filter.CharacterEncodingFilter來解決中文亂碼問題, 只要在web.xml配相應的攔截器就能夠啦, 配置後全部的中文編碼都是utf-8
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
參考文檔: http://www.360doc.com/content/14/0331/09/16583880_365105789.shtml