博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SRPING MVC基本配置
阅读量:6435 次
发布时间:2019-06-23

本文共 3646 字,大约阅读时间需要 12 分钟。

作下记录,WEB.XML的配置及DispatcherServlet-context.xml的配置。

而后者的配置,有不同的形式,不要被迷惑。

WEB.XML

DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/spring/webcontext/DispatcherServlet-context.xml
DispatcherServlet
/

 

DispatcherServlet-context.xml

 

DispatcherServlet-context.xml

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.

 

 

转载地址:http://nnega.baihongyu.com/

你可能感兴趣的文章
oracle数据库的启动和停止
查看>>
《LoadRunner没有告诉你的》之七——使用 LoadRunner 连续长时间执行测试,如何保证参数化的数据足够又不会重复?...
查看>>
python easy_install django 安装
查看>>
读《图解HTTP》总结--第六章
查看>>
毕业就能拿到上万薪资的程序员他们都做了啥?
查看>>
最小的k个数
查看>>
iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
查看>>
程序员从零到月薪15K的转变,python200G资料分享
查看>>
DNS域名解析的知识了解
查看>>
部署社交网站
查看>>
CentOS下如何修改主机名
查看>>
“机器人商店”是什么?卖机器人的吗?
查看>>
SVN的代码正确提交方法
查看>>
js框架 vue
查看>>
tomcat关闭时进程未退出
查看>>
Git分支管理策略
查看>>
kali安装软件遇到的问题&解决
查看>>
Azure系列2.1.10 —— CloudBlobClient
查看>>
【04-20】httpclient处理302重定向问题
查看>>
OpenGLes2.0 什么是Pbuffer
查看>>