作下记录,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基本配置的更多相关文章

  1. .net(C#)在vs2010版本下的MVC如何配置才能切换静态页面(html)

    由于vs2010用的人比较多,虽然建mvc项目vs2010可能还不成熟,但鉴于每个人的成长有限,每个地方的资源有限,最主要的是为了解决问题,所以先不管那么多了. 用vs2010为公司网站建站,要求js ...

  2. atitit.spring3 mvc url配置最佳实践

    atitit.spring3 mvc url配置最佳实践 1. Url-pattern  bp 1 2. 通用星号url pattern的问题 1 3. Other code 1 4. 参考 2 1. ...

  3. Spring MVC 事务配置

    Spring MVC事务配置 要了解事务配置的所有方法,请看一下<Spring事务配置的5种方法> 本文介绍两种配置方法: 一.      XML,使用tx标签配置拦截器实现事务 一.   ...

  4. spring mvc+myBatis配置详解

    一.spring mvc Spring框架(框架即:编程注解+xml配置的方式)MVC是Spring框架的一大特征,Spring框架有三大特征(IOC(依赖注入),AOP(面向切面),MVC(建模M- ...

  5. Windows Server2008 R2 MVC 环境配置

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  6. Maven 工程下 Spring MVC 站点配置 (三) C3P0连接池与@Autowired的应用

    Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 前两篇文章主要是对站点和数据库操作配置进行了演示,如果单 ...

  7. Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作

    详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...

  8. Maven 工程下 Spring MVC 站点配置 (一)

    最近,查找一些具体资料时,虽然会有很多,但是系统的却很少,尤其是对maven 下 spring mvc 站点搭建的配置,总是说的很多但让新手一目了然的步骤却少之又少. 对此闲暇时整理了一下,做了一套较 ...

  9. spring原拦截器配置与新命名空间mvc:interceptors配置拦截器对照与注意事项

    原先,我们是这么配置拦截器的 <bean id="openSessionInViewInterceptor"class="org.springframework.o ...

随机推荐

  1. E20170706-sl

    erode    vt.     侵蚀,腐蚀 vi.     逐渐毁坏; 削弱,损害; thin  adj.     薄的; 瘦的; 细的; 稀少的; laptop  n.     便携式电脑;

  2. Speed Limit

    http://poj.org/problem?id=2017 #include<stdio.h> int main() { int n,mile,hour; ) { ,h = ; whil ...

  3. P4046 [JSOI2010]快递服务

    传送门 很容易想出\(O(n^3m)\)的方程,三维分别表示某个快递员现在在哪里,然后直接递推即可 然而这样会T,考虑怎么优化.我们发现每一天的时候都有一个快递员的位置是确定的,即在前一天要到的位置. ...

  4. [Swift通天遁地]五、高级扩展-(5)获取互补色、渐变色、以及图片主题颜色

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. redis-缓存穿透,缓存雪崩,缓存击穿,并发竞争

    目录 缓存穿透 定义 解决方案 利用互斥锁 采用异步更新策略 使用布隆过滤器 空置缓存 缓存雪崩 定义 解决方案 给缓存的加一个随机失效时间 使用互斥锁 双缓存策略 缓存击穿 定义 解决方案 使用互斥 ...

  6. SQL Server语言 函数以及SQL编程

    1.数学函数:操作一个数据,返回一个结果 --去上限: ceiling ☆select ceiling(price) from car --去下限:floor ☆select floor(price) ...

  7. 网上流行的学生选课相关的50个常用sql语句

    学生表 Student(S#,Sname,Sage,Ssex) 教师表 Teacher(T#,Tname) 课程表 Course(C#,Cname,T#) 学生成绩表 SC(S#,C#,score) ...

  8. js基础---object对象

    //**********************************复杂JSON举例**************************************** var Jsondata={d ...

  9. 关于华为手机Log.d打印不出来log的问题

    http://blog.csdn.net/picasso_l/article/details/52489560     拨号,进入后台设置,进行操作.

  10. 影响ERP成功实施的因素及实施方法

    一.影响ERP实施的因素 1.企业自身管理和认识上的问题.在ERP实施过程中没有用变革管理的理念和方法来策划和管理ERP的实施是导致ERP失败的主要原因. ERP作为一种管理工具他的实施本身就是操作手 ...