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 ...
随机推荐
- git-更改本地和远程分支的名称
git branch -m old_branch new_branch # Rename branch locally git push origin :old_branch # Delete the ...
- Ruby 遍历多个数组
puts("----------------------------------------") puts(" 多重指定 test") ...
- [Swift通天遁地]七、数据与安全-(18)使用Swift实现原生的MD5加密
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- HTML--form表单中的label标签
小伙伴们,你们在前面学习表单各种控件的时候,有没有发现一个标签--label,这一小节就来揭晓它的作用. label标签不会向用户呈现任何特殊效果,它的作用是为鼠标用户改进了可用性.如果你在 labe ...
- jmeter中对于各类时间格式的设置
最普通的设置为使用 函数助手中的__time, 设置好需要使用的类型,并设置接收参数即可 YMD = yyyyMMdd HMS = HHmmss YMDHMS = yyyyMMdd-HHmmss 第二 ...
- 如何读取 Json 格式文件
Json 源文件代码: [ { "Id": "0", "Name": "书籍", "Detail": ...
- Java内存泄漏及对象引用的4种类型
转自: http://www.cnblogs.com/qq78292959/archive/2011/07/25/2116123.html 总结: 引用分类: 强引用,弱引用,软引用,虚引用.虚引用必 ...
- C# 文件操作【转】
本文也收集了目前最为常用的C#经典操作文件的方法,具体内容如下:C#追加.拷贝.删除.移动文件.创建目录.递归删除文件夹及文件.指定文件夹下面的所有内容copy到目标文件夹下面.指定文件夹下面的所有内 ...
- easyui form.rest和clear 重置表单和清除表单数据区别
easyui中的一般我们在新增和编辑的时候 都是用一个form表单 那新增的时候 需要重置下表单内容,一般用 $('#EditForm').form('reset'); 大部分时候没问题,但是如果表单 ...
- HTML 表单 存为EXCEL文件时 中文显示乱码
在做宣传品发放系统时,需求要把数据库查询的记录生成表单并转存excel文件. 在转存的EXCEL文件中文显示乱码,表格和其他字符正常,检查后发现是创建EXCEL文件打开模式不对 之前: myfile ...