天天看点

springmvc知识整体概览

Springmvc是什么?

官方文档给出这样的解释:

The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet

that dispatches requests to handlers, with configurable handler mappings, view resolution, locale

and theme resolution as well as support for uploading files. The default handler is based on the

@Controller and @RequestMapping annotations, offering a wide range of flexible handling methods.

With the introduction of Spring 3.0, the @Controller mechanism also allows you to create RESTful

Web sites and applications, through the @PathVariable annotation and other features.

首先Springmvc是一个严格按照mvc模式设计的一个框架,是目前最主流的mvc框架之一,而且可以与Spring框架完美整合,在Spring3.0之后,可以说是超越了Struts2框架,而且对于Springmvc,他还支持基于REST风格的URL请求。它通过一套mvc注解,让POJO成为处理请求的控制器,而无需实现任何接口。

Springmvc学什么?

由于Springmvc是基于mvc模型来的,所以我们可以整体的来了解mvc模型,对于知识点,我们应该更多的关注【@RequestMapping注解】【对于请求参数的处理】【模型数据处理】【视图和视图解析器】【RESTful风格】【Springmvc标签库】【静态资源处理】【数据转换/数据格式化/数据校验】【JSON数据处理】【国际化处理】【文件的上传】【拦截器】【异常处理】

Springmvc处理请求过程

这里是官网的一张图片

springmvc知识整体概览

解释:用户发送请求(IncomingRequest),被前段控制器(FontController)拦截,然后进行请求处理,找到与之对应的控制器(Controller),在这个Controller中进行业务处理,最后返回的为一个数据模型(Createmodel),然后,将我们的数据模型再返还给Fontcontroller,我们的前端控制器根据数据模型中的逻辑视图名,选择相应的视图进行渲染(View template),最后在经过我们的Controller返还给我们的用户。

Springmvc注意事项

1、 对于jar包,我们需要在spring的基础上添加,我们的Springmvc的相关jar包,因为Springmvc属于spring的一个子模块,所以Springmvc的基础相关jar包都在spring的jar文件中。

2、在使用Springmvc的标签时,需要进行导入相关的标签库。

3、在使用JSTL标签库(JSP标准标签库)时,我们需要引入【jstl.jar】和 【standard.jar】这两个jar包。

4、在进行json数据处理时我们用的是Jackson框架,我们需要引入关于他的相关jar包【jackson-annotations-2.1.5.jar】【jackson-core-2.1.5.jar】【jackson-databind-2.1.5.jar】

5、对于数据的验证,springmvc有自己的验证框架,但是同时也支持JSR303数据验证,在利用JSR303数据验证时,我们需要加入相关jar包,搜索【hibernate-validator-5.0.0.CR2-dist】进行下载相关jar包,【hibernate-validator-5.0.0.CR2】【hibernate-validator-annotation-processor-5.0.0.CR2】【classmate-0.8.0】【el-api-2.2】【javax.el-2.2.4】【javax.el-api-2.2.4】【jboss-logging-3.1.1.GA】【validation-api-1.1.0.CR1】,这里我们需要注意的是jar包之间的冲突,因为在tomcat中也是存在【el-api】包的。其中【el-api-2.2】【javax.el-2.2.4】【javax.el-api-2.2.4】可以放到tomcat所对应的lib目录中。

6、在文件上传中,Springmvc并没有给我们装配【MultiparResolver】,所以我们要进行配置,并且加入相关jar包【commons-io-2.0.jar】和【commons-fileupload-1.2.1.jar】

继续阅读