.作为SpringMVC入门 ,先以XML配置方式为例,后面会给户全注解的开发方式。
一.首先需要配置Web工程的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>LearnSpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置Spring IOC 配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 配置ContextLoaderListener用以初始化Spring IOC 容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置DispatcherServlet -->
<servlet>
<!-- 注意: Spring MVC 框架 会根据servlet-name配置 ,找到/WEB-INF/dispatcher-servlet.xml做为配置文件载入Web工程中 -->
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 使得 Dispatcher在服务器启动的时候就初始化-->
<load-on-startup>2</load-on-startup>
</servlet>
<!-- servlet拦截配置 -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
这个文件的配置内容:
系统变量contextComfigLocation的配置,它会告诉SpringMVC其SpringIoc的配置的文件在哪里,这样Spring就会找到这些配管文件去加载它们,如果是多个配置文件,可以使用逗号将它们分隔开来,并且它还能支持正则式匹配,进行模糊匹配。这样就更加灵活了,其默认值为/WEB-INF/applicationContext.xml。
ContextLoaderListener实现了接口 ServletContextListener,我们知道ServletContextListener的作用是可以在整个Web 工程前后加入自定义代码,所以可以在Web 工程初始化之前,它先完成对SpringIoC容器的初始化,也可以在Web 工程关闭之时完成SpringloC 容器的资源进行释放。
配置DispatcherServlet,首先是配置了servlet-name 为dispatcher,这就意味着需要一个/WEB-INF/dispatcher-serv 文件(注意,servlet-name 和文件名的对关系)与之对应,并且我们配置了在服务器启动期间就初始化它。
配置DispatcherServlet 拦截以后缀“do”结束的请求,这样所有以后缀“do”结尾的请求都会被它拦截。
最简单的入门例子中时不配置applicationContext.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
这样 Spring IoC容器就没有装载自己的类,这时,它还会加载一个/WEB-INF/dispatcher-servlet.xml文件,它是与SpringMVC配置相关的内容,所以有一定的内容:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 使用注解驱动 -->
<mvc:annotation-driven />
<!-- 定义扫描装载的包 -->
<context:component-scan base-package="com.*" />
<!-- 定义视图解析器 -->
<!-- 找到Web工程/WEB-INF/JSP文件夹,且文件结尾为jsp的文件作为映射 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<!-- 如果有配置数据库事务,需要开启注解事务的,需要开启这段代码 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager"/> -->
</beans>
这里的配置也比较简单:
- <mvc:annotation-driven />表示使用注解驱动SpringMVC
- 定义一个扫描的包,用它来扫描对应的包,用以加载对应的控制器和其他的一些组件
- 定义视图解析器,解析器中定义了前缀和后缀,这样视图就知道去Web工程的/WEB-INF/JSP文件中找到对应的JSP作为视图响应用户请求。
写一个controller:
package com.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
//注解@Controller表示它是一个控制器
@Controller
// 表明请求的URI在/briefness的时候才有该控制器响应
@RequestMapping("/briefness")
public class BriefnessController {
// 表明URI是/index的时候该方法才请求
@RequestMapping("/index")
public ModelAndView index() {
// 模型和视图
ModelAndView mv = new ModelAndView();
// 视图逻辑名称为index
mv.setViewName("index");
// 返回模型和视图
return mv;
}
}
首先注解@Controller是一个控制器.SpringMVC扫描的时候会把它作为控制器加载进来.然后,注解@RequestMapping指定了对应请求的URI,SpringMVC在初始化的时候就会将这些解析,存放起来,于是便有了HandlerMapping。当发生请求时,SpringMVC就会使用这些信息去找到对应的控制器提供服务.
方法定义返回ModelAndView,在方法中把视图名称定义为index,然后我们在配置文件中所配置的视图解析器,由于前缀/WEB-INF/jsp/,后缀.jsp,加上返回的视图逻辑名称为index,所以它会选择使用/WEB-INF/index.jsp作为最后的响应, 下面我们在该路径写一个index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>hello,Spring MVC</h1>
</body>
</html>
测试:http://127.0.0.1:8080/LearnSpringMVC/briefness/index.do
配置成功!