一.Spring基本介绍:
SpringMVC有五大组件:DispatchServlet、HandlerMapping、ViewResolver、Controller、ModelAndView。他们之间的关系以及springMVC的工作流程如下图:
具体说明如下:
- 项目加载在tomcat容器中,tomcat会启动spring容器,加载spring的一些配置
- 用户发送请求到tomcat,交给DispatchServlet(前端控制器)去处理,它依据HandlerMapping的配置调用相应的Controller来处理业务
- Controller处理完业务后将结果封装成ModelAndView对象返回给DispatchServlet
- DispatchServlet再依据ViewResolver(视图解析器)的解析,调用相应的视图对象(比如某个JSP)来生成相应的页面,然后响应给用户。
二.编程步骤:
依据Springmvc工作流程来安排一下步骤:
- 导包,导入springMVC的jar包
- 如果是maven工程,则添加依赖,配置的pom.xml文件如下:
-
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.1.RELEASE</version> </dependency> </dependencies>
- 添加配置文件(springmvc.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" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"> </beans>
- 配置DispatchServlet(前端控制器)到web.xml
-
<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.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>
-
- 编写具体实现Controller接口的类处理业务逻辑,并返回ModelAndView对象
- 以返回一个视图名为"hello"的类为例
-
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("hello"); return new ModelAndView("hello"); } }
- 配置HandlerMapping到springmvc.xml文件中,配置请求路径与相应controller的对应关系
- 下面的例子会配置“/hello.do”与HelloController之间的对应关系
-
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/hello.do">HelloController</prop> </props> </property> </bean>
- 配置controller到springmvc.xml文件以便DispatchServlet查询调用;
- <bean id="HelloController" class="controller.HelloController"/>
- 配置ViewResolver(视图解析器)到springmvc.xml文件中,将ModelAndView对象映射到视图对象
-
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean>
-
- 编写具体的视图对象,如jsp页面,以便响应给用户
- 编写一个hello.jsp页面在WEB-INF目录下
-
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>hello</title> </head> <body> <h1>Hello,SpringMVC!</h1> </body> </html>
- 加载到tomcat容器运行,然后访问 http://localhost:8080/springmvc01/hello.do (注:springmvc01是你的工程名)运行界面如下: