Spring 3 MVC – Introduction to Spring 3 MVC Framework
2015-09-02 13:32
495 查看
In Spring Web MVC you can use any object as a command or form-backing object; you do not need to implement a framework-specific interface or base class. Spring’s data binding is highly flexible: for example, it treats type mismatches as validation errors that can be evaluated by the application, not as system errors. Thus you need not duplicate your business objects’ properties as simple, untyped strings in your form objects simply to handle invalid submissions, or to convert the Strings properly. Instead, it is often preferable to bind directly to your business objects.
Request Processing Lifecycle
Spring’s web MVC framework is, like many other web MVC frameworks, request-driven, designed around a central servlet that dispatches requests to controllers and offers other functionality that facilitates the development of web applications. Spring’s
Following is the Request process lifecycle of Spring 3.0 MVC:
The client sends a request to web container in the form of http request.
This incoming request is intercepted by Front controller (
With the help of Handler Mappings, the
The Controller tries to process the request and returns the Model and View object in form of
The Front Controller then tries to resolve the View (which can be JSP, Freemarker, Velocity etc) by consulting the View Resolver object.
The selected view is then rendered back to client.
Features of Spring 3.0
Spring 3.0 framework supports Java 5. It provides annotation based configuration support. Java 5 features such as generics, annotations, varargs etc can be used in Spring.
A new expression language Spring Expression Language SpEL is being introduced. The Spring Expression Language can be used while defining the XML and Annotation based bean definition.
Spring 3.0 framework supports REST web services.
Data formatting can never be so easy. Spring 3.0 supports annotation based formatting. We can now use the
Spring 3.0 has started support to JPA 2.0.
In above code snippet, we have configure
Once the
Note the above architecture diagram. The
Request Processing Lifecycle
Spring’s web MVC framework is, like many other web MVC frameworks, request-driven, designed around a central servlet that dispatches requests to controllers and offers other functionality that facilitates the development of web applications. Spring’s
DispatcherServletis completely integrated with Spring IoC container and allows us to use every other feature of Spring.
Following is the Request process lifecycle of Spring 3.0 MVC:
The client sends a request to web container in the form of http request.
This incoming request is intercepted by Front controller (
DispatcherServlet) and it will then tries to find out appropriate Handler Mappings.
With the help of Handler Mappings, the
DispatcherServletwill dispatch the request to appropriate Controller.
The Controller tries to process the request and returns the Model and View object in form of
ModelAndViewinstance to the Front Controller.
The Front Controller then tries to resolve the View (which can be JSP, Freemarker, Velocity etc) by consulting the View Resolver object.
The selected view is then rendered back to client.
Features of Spring 3.0
Spring 3.0 framework supports Java 5. It provides annotation based configuration support. Java 5 features such as generics, annotations, varargs etc can be used in Spring.
A new expression language Spring Expression Language SpEL is being introduced. The Spring Expression Language can be used while defining the XML and Annotation based bean definition.
Spring 3.0 framework supports REST web services.
Data formatting can never be so easy. Spring 3.0 supports annotation based formatting. We can now use the
@DateFimeFormat(iso=ISO.DATE)and
@NumberFormat(style=Style.CURRENCY)annotations to convert the date and currency formats.
Spring 3.0 has started support to JPA 2.0.
Configuring Spring 3.0 MVC
The entry point of Spring 3.0 MVC is theDispatcherServlet.
DispatcherServletis a normal servlet class which implements
HttpServletbase class. Thus we need to configure it in
web.xml.
<web-app> <servlet> <servlet-name>example</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app>
In above code snippet, we have configure
DispatcherServletin
web.xml. Note that we have mapped
*.htmlurl pattern with example
DispatcherServlet. Thus any url with
*.htmlpattern will call Spring MVC Front controller.
Once the
DispatcherServletis initialized, it will looks for a file names
[servlet-name]-servlet.xmlin
WEB-INFfolder of web application. In above example, the framework will look for file called
example-servlet.xml.
Note the above architecture diagram. The
WebApplicationContextspecified in above diagram is an extension of the plain
ApplicationContextwith some extra feature necessary for web applications. The
WebApplicationContextis capable of resolving themes and it is also associated with corresponding servlet. The
WebApplicationContextis bound in the
ServletContext, and by using static methods on the
RequestContextUtilsclass you can always look up the
WebApplicationContext.
相关文章推荐
- Java锁机制
- java时间操作
- java日志组件介绍(common-logging,log4j,slf4j,logback )
- Spring整合Struts
- Myeclipse 10 使用之修改字体
- Java 使用ZipOutputStream 进行打包操作
- Spring MVC Controller配置方式
- java 电话本
- JAVA多线程之内存可见性笔记
- eclipse连接手机一直连接不起
- Java加密技术(五)——非对称加密算法的由来DH
- JVM内存JAVA_OPTS参数说明
- java String.format() 字符串格式化
- Spring文件上传
- java中类名+方法名(){}什么意思?
- Struts2 ——应用demo及其常用配置参数
- spring 框架中实现单线程 并当 服务器故障的时候 关闭线程
- eclipse Generating Getters and Setters Failed
- eclipse 在Eclipse中关联源代码
- JAVA IO