Spring MVC新手教程(一)


直接干貨

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


















免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM