1.mcv框架要作哪些事情html
(a)將url映射到java類或者Java類的方法java
(b)封裝用戶提交的數據web
(c)處理請求---調用相關的業務處理,封裝響應的數據spring
(d)將封裝的數據進行渲染,jsp,html等express
2.SpringMvc是一個輕量級的基於請求響應的mvc框架mvc
3.爲何要學習SpringMvcapp
性能比較好框架
簡單、易學jsp
與Spring無縫結合(使用Spring 的 IOC,AOP)性能
可以進行簡單Junit測試
支持Restful風格
異常處理
本地化、國際化
數據驗證、類型轉換等
攔截器
等等
-------使用的人和公司多最主要
4.簡單瞭解結構
5.作一個簡單的SpringMvc 例子 (如下例子爲 annotation例子)
(a)導入相關jar包
commons-logging-1.1.3.jar
spring-aop-4.2.2.RELEASE.jar (註解的時候須要)
spring-beans-4.2.2.RELEASE.jar
spring-context-4.2.2.RELEASE.jar
spring-context-support-4.2.2.RELEASE.jar
spring-core-4.2.2.RELEASE.jar
spring-expression-4.2.2.RELEASE.jar
spring-web-4.2.2.RELEASE.jar
spring-webmvc-4.2.2.RELEASE.jar
(b)配置分發器
<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:mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
(c)添加SpringMvc配置文件 默認在web-inf下添加mvc.xml
(d)編寫controller
package com.spring.hello; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import org.springframework.stereotype.Controller; @Controller public class HelloController { @RequestMapping("/hello") public ModelAndView hello(HttpServletRequest req,HttpServletResponse resp) { ModelAndView mv=new ModelAndView(); mv.addObject("msg", "第一個annotation"); mv.setViewName("Hello"); return mv; } }
(e)編寫SpringMvc文件即 mvc.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" 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"> <!-- 配置渲染器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <!--配置結果視圖的前綴 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!--配置結果視圖的後綴 --> <property name="suffix" value=".jsp"/> </bean> <!-- 配置請求和處理器 --> <context:component-scan base-package="com.spring.hello"></context:component-scan> </beans>