什么是spring-mvc
实现了mvc结构的spring模块,spring-mvc模块封装了web开发中的常用功能,简化了web过程。
DispatcherServlet处理浏览器发来的请求
HandlerMapping将请求转给指定的controller
ModelAndView模型,封装业务处理结果和视图
1新建工程,导入jar包
2在src下添加spring mvc 的xml配置文件,文件名为spring-mvc.xml,该名称不固定。
spring-mvc.xml的代码后面统一给出。
3web.xml中,配置DispathcherServlet前端控制器组件,代码如下
<?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"> <display-name></display-name> <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:/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.form</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
此时,部署到tomcat并启动就会显示springmvc信息
4配置spring handlermapping组件,在spring-mvc.xml中添加配置信息。
5新建控制处理类HelloController。在spring-mvc.xml中配置该controller为bean。
package com.controller; 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{ @Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { System.out.println("处理hello.form请求"); ModelAndView mv = new ModelAndView("hello"); return mv; } }
6添加ViewResolver组件配置
此时,spring-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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 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-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 定义请求处理映射HandlerMapping --> <bean id = "handlerMapping" class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <!--指定请求和controller对应的关系 --> <property name = "mappings" ref = "urlMappings"/> </bean> <!-- 定义请求映射表 map --> <util:properties id="urlMappings"> <prop key="/hello.form">helloController</prop> </util:properties> <!-- 定义视图解析器viewResolver --> <bean id = "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name = "prefix" value = "/WEB-INF/jsp/"/> <property name = "suffix" value = ".jsp"/> </bean> <!-- 控制器bean配置 --> <bean id = "helloController" class = "com.controller.HelloController"/> </beans>
7新建视图组件hello.jsp,在/WEB-INF/jsp中,spring-mvc.xml中已经配置。
<%@page pageEncoding="utf-8" contentType="text/html;charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Spring Hello World!</title> </head> <body> hello Spring MVC,日向 2016-01-12,Life is a war,and I must win! </body> </html>
8测试
目录结构,期间许多libraries未必必要: