SpringMVC是spring框架的一个模块,SpringMVC和Spring无需通过中间层进行整合。
SpringMVC是基于mvc的web框架。
SpringMVC的框架:
处理器映射器(handlerMapping):根据请求的url,找到对应的handler。
处理器适配器(handlerApapter):执行处理器映射器找到的handler
视图解析器(view resolver):进行视图解析,返回view
第一步:发起请求到前端控制器(DispatcherServlet)
第二步:前端控制器请求处理器映射器(HandlerMapping)查找handler,可以根据xml配置、注解进行查找
第三步:处理器映射器HandlerMapping向前端返回handler
第四步:前端控制器调用处理器适配器(HandlerApapter)去执行handler
第五步:处理器适配器去执行handler
第六步:handler执行完,给处理器适配器返回ModelAndView。
第七步:处理器适配器向前端控制器返回ModelAndView
ModelAndView是SpringMVC框架的一个底层对象,包括了Model和View
第八步:前端控制器请求视图解析器(view resolver)去进行视图解析
视图解析:根据逻辑视图名解析成真正的视图(jsp)。
第九步:视图解析器向前端控制器返回view
第十步:前端控制器进行视图渲染
视图渲染:将模型数据(在ModelAndView中)填充到request域
第十一步:前端控制器向用户响应结果
组件:
1、前端控制器(DispatcherServlet),一般不需要程序员开发
作用:接收请求,响应结果,相当于转发器,中央处理器。
有了DispatcherServlet,减少了其他组件之间的耦合性。
2、处理器映射器(HandlerMapping),不需要程序员开发
作用:根据请求的url,查找对应的handler
3、处理器适配器(HandlerAdapter)
作用:按照特定的规则(HandlerAdapter要求的规则),执行Handler
注意:编写Handler时按照HandlerAdapter的要求去做,这样适配器才可以去正确执行handler
4、处理器Handler,需要程序员开发
5、视图解析器(View resolver),不需要程序员开发
作用:进行视图解析,根据逻辑视图名解析成真正的视图(View)
6、视图(View),需要程序员开发jsp
View是一个接口,实现类支持不同的View类型(jsp、freemarker、pdf....)
2、配置前端控制器
前端控制器是一个servlet,所以在web.xml中配置前端控制器
配置servlet:
<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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TestSpringMVC1display-name>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>/WEB-INF/springmvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>*.actionurl-pattern>
servlet-mapping>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
web-app>
3、配置处理器映射器,配置处理器适配器,配置视图解析器
配置handler:将编写的handler在spring容器中加载。
配置视图解析器:view是jsp,所以需要配置解析jsp的视图解析器。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">
<bean name="/query.action" class="com.chen.controller.QueryHander">bean>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">bean>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">bean>
beans>
4、编写Hander
Hander需要实现Controller接口,才能由SimpleControllerHandlerAdapter来执行:
package com.chen.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
public class QueryHander implements org.springframework.web.servlet.mvc.Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("WEB-INF/pages/helloworld.jsp");
return modelAndView;
}
}
helloworld.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title heretitle>
head>
<body>
<h1>HelloWorld!h1>
body>
html>
1、配置前端控制器:
<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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVCAnnotationdisplay-name>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>/WEB-INF/springmvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>*.actionurl-pattern>
servlet-mapping>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
web-app>
2、使用注解的映射器和注解的适配器。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">
<mvc:annotation-driven>mvc:annotation-driven>
<context:component-scan base-package="com.chen.controller">context:component-scan>
beans>
3、handler
package com.chen.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class QueryHander{
@RequestMapping("/query.action")
public ModelAndView query() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("WEB-INF/pages/helloworld.jsp");
return modelAndView;
}
}
helloworld.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title heretitle>
head>
<body>
<h1>HelloWorld! springMVC annotation!h1>
body>
html>
1、配置视图解析器前缀和后缀
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/">property>
<property name="suffix" value=".jsp">property>
bean>
2、在handler中,省略路径的前缀和后缀
package com.chen.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
public class QueryHander implements org.springframework.web.servlet.mvc.Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView modelAndView = new ModelAndView();
//下面的路径,如果在试图解析器中配置了jsp路径的前缀和后缀。可修改为helloworld。
// modelAndView.setViewName("WEB-INF/pages/helloworld.jsp");
//上面的路径配置可以不再程序中指定jsp的前缀和后缀。
modelAndView.setViewName("helloworld");
return modelAndView;
}
}