springmvc是spring框架的一個模塊,是一個基於MVC的web框架,springmvc和spring無需通過中間層整合。
springmvc原理
第一步:發起請求到前端控制器(DispatcherServlet)
第二步:前端控制器請求HandlerMapping查找Handler(可以根據xml配置、註解進行查找)
第三步:處理器映射器HandlerMapping向前端控制器返回Handler
第四步:前端控制器調用處理器適配器去執行Handler
第五步:處理器適配器執行Handler
第六步:Handler執行完成給適配器返回ModelAndView,ModelAndView是springmvc框架的一個底層對象,包含Model和View
第七步:處理器適配器向前端控制器返回ModelAndView
第八步:前端控制器請求視圖解析器去進行視圖解析,根據邏輯視圖名解析成真正的視圖(jsp)
第九步:視圖解析器向前端控制器返回View
第十步:前端控制器進行視圖渲染,視圖渲染將模型數據(在ModelAndView對象中)填充到request域
第十一步:前端控制器向用戶響應結果
組件
1.前端控制器DispatcherServlet
作用:接收請求、響應結果,相當於轉發器
2.處理器映射器HandlerMapping
作用:根據請求的url查找Handler
3.處理器適配器HadlerAdapter
作用:按照特定規則去執行Handler
注意:編寫Handler時按照HandlerAdapter的要求去做,這樣適配器纔可以正確的執行Handler
4.視圖解析器ViewResolve
作用:進行視圖解析,根據邏輯視圖名解析成真正的視圖
配置前端控制器
在/WEB-INF/web.xml中配置,前端控制器是一個servlet
!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet.class</servlet-class>
<!-- contextConfigLocation配置springmvc加載的配置文件(配置處理器映射器,適配器等)
如果不配做,默認加載/WEB-INF/servlet名稱-servlet.xml(springmvc-servlet.xml)-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 第一種:*。action 訪問以.action結尾由DispactherServlet進行解析
第二種:/ 所有訪問的地址都DispactherServlet進行解析 ,對於靜態文件解析需要配置不讓DispatcherServlet解析,可以實現RESTful風格
-->
<url-pattern>*。action</url-pattern>
</servlet-mapping>
Handler
需要實現Controller接口,
才能由org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter適配器執行。
實現類:
public class ItemsController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
List<Items> list = new ArrayList<Items>();
Items item1 = new Items();
item1.setName("abc");
list.add(item1);
ModelAndView modelAndView = new ModelAndView();
//相當於request的setAttribute
modelAndView.addObject("ItemsList", list);
//指定視圖
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return modelAndView;
}
}
配置處理器映射器,適配器和視圖解析器
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 配置Handler -->
<bean name="/items.action" class="springmvc.ItemsController"></bean>
<!-- 處理器映射器
1.將bean的name作爲url進行查找 配置Handler時指定beanname(url)-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 2.簡單url配置映射器 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props >
<prop key="/items1.action">items</prop>
<prop key="/items2.action">items</prop>
</props>
</property>
</bean>
<!-- 處理器適配器 所有適配器都實現HandlerAdapter接口-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 視圖解析器
解析jsp默認使用jstl標籤,classpath下要有jstl包-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>
//注:多種處理器映射器可以並存
HttpRequestHandlerAdapter
http請求處理器適配器,所有實現了HttpRequestHandler接口的Bean通過此適配器進行適配
配置:
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
Controller實現類:
public class ItemsController2 implements HttpRequestHandler {
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Items> list = new ArrayList<Items>();
Items item1 = new Items();
item1.setName("abc");
list.add(item1);
request.setAttribute("itemsList2", list);
request.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(request, response);
}
//此方法可以通過response來設置響應的數據格式,比如響應Json數據
}
如果不在自已創建的springmvc.xml中配置處理器映射器、適配器和視圖解析器,
前端控制器默認從
/org/springframework/web/servlet/DispatcherServlet.properties
文件中加載處理。
註解的處理器映射器、適配器
在spring3.1之前使用
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
註解映射器
在spring3.1之後使用
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
註解映射器
在spring3.1之前使用
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
註解適配器
在spring3.1之後使用
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
註解適配器
配置:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 註解驅動代替上面的註解映射器和適配器的配置
mvc:annotation-driven默認加載很多參數綁定方法,實際開發使用這個標籤 -->
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="springmvc"></context:component-scan>
註解實現類:
@Controller
public class ItemsController3 {
//url映射
@RequestMapping("/query.action")
public ModelAndView query() throws Exception{
List<Items> list = new ArrayList<Items>();
Items item1 = new Items();
item1.setName("abc");
list.add(item1);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList3", list);
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return modelAndView;
}
}
視圖解析器前綴和後綴
配置:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前綴 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 後綴 -->
<property name="suffix" value=".jsp"></property>
<!-- JstlView表示JSP的模板頁面需要使用JSTL標籤庫 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
RequestMapping註解
定義Controller方法對應url,進行處理器映射使用
1.窄化請求路徑:
@Controller
//爲了對url進行分類管理,定義根路徑
@RequestMapping("/items")
public class ItemsController3 {
2.限制http請求方法:
//只能執行post請求方式
@RequestMapping(value="/items" ,method={RequestMethod.POST})
public class ItemsController3 {
Controller方法返回值
1.返回ModelAndView
2.返回String,表示返回邏輯視圖名
真正的視圖(jsp路徑)=前綴+邏輯視圖名+後綴
redirect重定向:return “redirect:query.action”
forward轉發:return “forward:query.action”
3.返回void
使用request轉發,response重定向和指定響應結果