之前进公司就直接使用spring mvc做项目了,并没有系统学习过,后来再回头去总结的时候发现我只是在项目中会使用,但是有关spring mvc的配置,还有注解什么的想起来会感觉很混乱,趁着这几天不忙,好好地系统学习总结一下spring mvc。本文只是描述了如何创建一个简单spring mvc工程,有关spring mvc注解的内容请参看本人的另外一篇文章:
http://blog.csdn.net/lmb55/article/details/50879776
下面就以一个spring mvc小例子开始这次学习。
1、导入spring mvc相关的jar包;
2、在web.xml中加入如下有关spring mvc的配置:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/restservice/*</url-pattern>
</servlet-mapping>
注意:
(1)、DispatcherServlet是前端控制器,每一个请求最先访问的都是DispatcherServlet,配置在web.xml中,拦截匹配的请求,DispatcherServlet拦截匹配规则需要自己定义,把拦截下来的请求依据相应的规则分发给目标controller去处理。是配置spring mvc的第一步。
(2)、load-on-startup:表示启动容器时初始化该Servlet;
(3)、url-pattern:表示哪些请求交给Spring MVC处理, 本例配置中resetservice下的所有请求均交给spring mvc来处理。
自此请求已交给Spring MVC框架处理,因此我们需要配置Spring的配置文件,默认DispatcherServlet会加载WEB- INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件,因本配置给出了初始化参数init-param,所以会加载WEB-INF/spring/appServlet/servlet-context.xml。
3、配置spring的配置文件(在WEB-INF/spring/appServlet下配置名为servlet-context.xml的配置文件)
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 该配置的功能为:启动包扫描功能;下面会有详细介绍 -->
<context:component-scan base-package="com.hollycrm.hollyuniproxy.server.http,com.hollycrm.**.mvc" />
<!-- if you use annotation you must configure this setting-->
<mvc:annotation-driven />
<!-- 拦截有关静态资源的访问,在访问静态资源的时候就可以不经过controller。所有的页面引用到的"/resources/**"资源都到/resources/目录下面去找 -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/**" location="/" />
<!-- 如果当前视图是"/",则交给相应的视图解析器直接解析为视图 -->
<mvc:view-controller path="/" view-name="login"/>
<mvc:view-controller path="/welcome" view-name="home"/>
<!-- 重定向:如果当前视图 是"/",则重定向到/admin/index-->
<mvc:view-controller path="/" view-name="redirect:/admin/index"/>
<!-- 视图名称解析器 -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
注意:
1、命名空间<context:component-scan />
首先,如果要使注解工作,则必须配置component-scan 。该配置的功能为:启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean。例:
<context:component-scanbase-package="com.tgb.web"/>
base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。还允许定义过滤器将基包下的某些类纳入或排除。
Spring支持以下4种类型的过滤方式:
1) 注解org.example.SomeAnnotation 将所有使用SomeAnnotation注解的类过滤出来;
2) 类名指定org.example.SomeClass 过滤指定的类;
3) 正则表达式com.kedacom.spring.annotation.web..* 通过正则表达式过滤一些类;
4) AspectJ 表达式 org.example..*Service+ 通过AspectJ 表达式过滤一些类;
示例如下:
<context:component-scan base-package="com.hollycrm.hollyuniproxy.server.http,com.hollycrm.**.mvc">
<!-- 正则表达式的过滤方式 -->
<context:exclude-filter type="regex" expression="com.hollycrm.hollyuniproxy.server.http"/>
<!-- 注解的过滤方式 -->
<context:exclude-filter type="annotation" expression="com.hollycrm.Service"/>
<context:exclude-filter type="annotation" expression="com.hollycrm.Controller"/>
<context:exclude-filter type="annotation" expression="com.hollycrm.Repository"/>
</context:component-scan>
2、InternalResourceViewResolver:视图名称解析器,用于支持Servlet、JSP视图解析;prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),比如传进来的逻辑视图名为hello,则该该jsp视图页面应该存放在“WEB-INF/jsp/hello.jsp”;
今天先写到这儿,稍后新建一个spring mvc的工程传到资源页,再来补~