SRPING MVC基本配置
作下记录,WEB.XML的配置及DispatcherServlet-context.xml的配置。
而后者的配置,有不同的形式,不要被迷惑。
WEB.XML
<servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servletclass>
    org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/spring/webcontext/DispatcherServlet-context.xml
    </param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
DispatcherServlet-context.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"
    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-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!-- HandlerMapping -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!-- HandlerAdapter -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!-- ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
DispatcherServlet-context.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"
    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-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <mvc:annotation-driven />
    <context:component-scan base-package="com.packt.webstore" />
    <bean    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
The first tag within the <beans> definition is <mvc:annotation-driven />. By this tag,we tell Spring MVC to configure the DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter, and ExceptionHandlerExceptionResolver beans.These beans are required for Spring MVC to dispatch requests to the controllers.
Actually <mvc:annotation-driven /> does many things behind the screen. It also enables support for various convenient annotations such as @NumberFormat and @DateTimeFormat to format the form bean fields during form binding. Similarly, we have the @Valid annotation to validate the controller method’s parameters. It also supports Java objects to/from an XML or JSON conversion via the @RequestBody and @ResponseBody annotations in the @RequestMapping or @ExceptionHandler method during form binding. We will see the usage of these annotations in later chapters. As of now, just remember thatthe <mvc:annotation-driven /> tag is needed to enable annotations such as @controller and @RequestMapping.
What is the purpose of the second tag, <context:component-scan>? You need a bit of background information to understand the purpose of the <context:component-scan> tag. The @Controller annotation indicates that a particular class serves the role of a controller. We already learned that the dispatcher servlet searches such annotated classes for mapped methods (the @RequestMapping annotated methods) to serve a web request. In order to make the controller available for searching, we need to create a bean for this controller in our web application context.
We can create beans for controllers explicitly via the bean configuration (using the <bean> tag—you can see how we created a bean for the InternalResourceViewResolver class using the <bean> tag in the next section), or we can hand over that task to Spring via the autodetection mechanism. To enable the autodetection of the @Controller annotated classes, we need to add component scanning to our configuration using the <context:component-scan> tag. Now, you finally understand the purpose of the <context:component-scan> tag.
Spring will create beans (objects) for every @Controller class at runtime. The dispatcher servlet will search for the correct request mapping method in every @Controller bean based on the @RequestMapping annotation, to serve a web request. The base-package property of a <context:component-scan> tag indicates the package under which Spring should search for controller classes to create beans:
<context:component-scan base-package="com.packt.webstore" />
The preceding line instructs Spring to search for controller classes within the com.packt.webstore package and its subpackages.
Tip
The <context:component-scan> tag not only recognizes controller classes, it also recognizes other stereotypes such as services and repository classes as well. We will learn more about services and repositories later.

SRPING MVC基本配置的更多相关文章
- .net(C#)在vs2010版本下的MVC如何配置才能切换静态页面(html)
		
由于vs2010用的人比较多,虽然建mvc项目vs2010可能还不成熟,但鉴于每个人的成长有限,每个地方的资源有限,最主要的是为了解决问题,所以先不管那么多了. 用vs2010为公司网站建站,要求js ...
 - atitit.spring3 mvc url配置最佳实践
		
atitit.spring3 mvc url配置最佳实践 1. Url-pattern bp 1 2. 通用星号url pattern的问题 1 3. Other code 1 4. 参考 2 1. ...
 - Spring MVC 事务配置
		
Spring MVC事务配置 要了解事务配置的所有方法,请看一下<Spring事务配置的5种方法> 本文介绍两种配置方法: 一. XML,使用tx标签配置拦截器实现事务 一. ...
 - spring mvc+myBatis配置详解
		
一.spring mvc Spring框架(框架即:编程注解+xml配置的方式)MVC是Spring框架的一大特征,Spring框架有三大特征(IOC(依赖注入),AOP(面向切面),MVC(建模M- ...
 - Windows Server2008 R2 MVC 环境配置
		
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
 - Maven 工程下 Spring MVC 站点配置 (三) C3P0连接池与@Autowired的应用
		
Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 前两篇文章主要是对站点和数据库操作配置进行了演示,如果单 ...
 - Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作
		
详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...
 - Maven 工程下 Spring MVC 站点配置 (一)
		
最近,查找一些具体资料时,虽然会有很多,但是系统的却很少,尤其是对maven 下 spring mvc 站点搭建的配置,总是说的很多但让新手一目了然的步骤却少之又少. 对此闲暇时整理了一下,做了一套较 ...
 - spring原拦截器配置与新命名空间mvc:interceptors配置拦截器对照与注意事项
		
原先,我们是这么配置拦截器的 <bean id="openSessionInViewInterceptor"class="org.springframework.o ...
 
随机推荐
- MSP430:定时器学习TimerA
			
4. 定时器TA 一.时钟源1.时钟源:ACLK/SMCLK 外部TACLK/INCLK2.分频:1/2/4/8 当 (注:TACLR 置位时,分频器复位) 二.计数模式通过设置MCx可以设置定时器的 ...
 - canvas做的时钟,学习下
			
canvas标签只是图形容器,您必须使用脚本来绘制图形. getContext() 方法可返回一个对象,该对象提供了用于在画布上绘图的方法和属性.——获取上下文对象. getContext(" ...
 - null, undefined,"",0,false是什么关系?
			
null本质上和0,"",false是一类东西,它们都表示一种数据类型的非值.正如0表示数字类型的非值,""表示字符类型的非值一样,null表示完全空的对象,即 ...
 - [Swift通天遁地]八、媒体与动画-(9)快速实现复合、Label、延续、延时、重复、缓冲、弹性动画
			
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
 - akka监控
			
使用akka系统时间就了,你就一定会想着监控的事儿.比如某个actor发送了多少消息.接收了多少消息.消息平均处理时间是多少,当前有多少个actor等等.本来我都用bytebuddy写了个简单的akk ...
 - 什么是mycat?
			
Mycat关键特性 关键特性 支持SQL92标准 支持MySQL.Oracle.DB2.SQL Server.PostgreSQL等DB的常见SQL语法 遵守Mysql原生协议,跨语言,跨平台,跨数据 ...
 - C语言常量
			
Constant包括4种类型: 整型 浮点型 枚举 字符型 #include <stddef.h> #include <uchar.h> int main() { /* Int ...
 - P3368 【模板】树状数组 2(树状数组维护差分序列)
			
题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数数加上x 2.求出某一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...
 - 将npm修改为cnpm
			
1.更改npm的源地址 检测是否更改成功 2.用cnpm代替npm npm常用命令: npm更新:npm install -g npm npm初始化生成package.json: npm init ...
 - 如何在Linuxt系统下运行maven项目
			
如何在Linuxt系统下运行maven项目 我们知道现在利用MAVEN来管理JAVA项目是非常常见的.比如公司一般都有一个自己的MAVEN仓库,通过MAVEN仓库来解决我们的项目依赖,更加方便的构建项 ...