1.MVC设计模式
1.1分层设计:
数据库访问接口:DAO层
处理业务逻辑:Service层
数据实体:POJO
负责前段请求的接受并处理:Servlet
负责前段页面显示:JSP
1.2环境搭建
1.下载需要的jar文件
2.在web.xml中配置Servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 配置环境参数,指定spring配置文件所在目录 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- 配置spring的ContextLoaderListener监听器,初始化Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
1.3创建Spring MVC文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
"
>
<!-- 扫描 自动装填 -->
<context:component-scan base-package="cn.smbms.controller"/>
<mvc:annotation-driven/>
<!-- 完成对应的视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 静态资源 -->
<mvc:resources location="/statics/" mapping="/statics/**" />
<!-- <bean name="/index.html" class="cn.smbms.controller.IndexController"/> -->
</beans>
1.4创建Controller类
package cn.smbms.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import cn.smbms.pojo.User;
@Controller
public class IndexController {
private Logger logger =Logger.getLogger(IndexController.class);
@RequestMapping("/index")
public String index() {
logger.info("hello,SpringMVC");
return "index";
}
}
1.5创建视图
即我们的JSP页面
1.6部署运行
2.Spring MVC框架的请求处理流程及体系结构
1.客户端发出HTTO请求,Web应用服务器接受此请求,若匹配DispatcherServlet的请求映射路径(在web.xml中指定),则Web容器将该请求转交给DispatcherServlet处理.
2.DispatcherServlet接收到该请求后,请根据请求的信息,找对处理请求的处理器Handler(即我们以上的Controller类)
3.当DispatchersServlet根据HandlerMapping招待对应当前请求的Handler之后,通过HandlerAdapter对Handler进行封装,再以统一的适配器接口调用Handler.
4.在请求信息到达真正调用Handler的处理方法之前的这段时间内,Spring MVC会将信息以一定的方式转换并绑定到请求方法的入参中.
5.处理器完成业务逻辑处理之后将返回一个ModelAndView对象给DispatcherServlet,ModelAndView对象包括了逻辑视图名和模型数据信息.
6.ModelAndView对象中包含的是"逻辑视图名",通过ViewResolver将逻辑视图名解析为真正的视图对象View.
7.得到真正的对象视图view后,dispatcherServlet会对ModelAndView进行视图渲染.
8.最终客户端获得响应消息,根据配置返回一个html或xml或json格式的数据