据说在Spring框架推出的早期,Spring凭借着AOP,Ioc的模式,配合MVC大量减轻了编写代码的工作强度,同时使用了大量的反射机制保证大量组件的可修改/替换性;
JavaEE和tomcat以及Spring的关系
首先是Spring,目前为止Spring已经包含了巨量的内容,从MVC到Cloud,几乎完成了一个完整的web开发框架,无论是all in one还是分布式,使用Spring都可以达到目的;
对于网络应用,核心的概念无非就是三个:请求,处理,存储;所有的web应用都是这三个概念的延伸;
在javaSE中,其实已经包含了套接字的内容,而javaEE提出了将socket封装成为servlet供程序员使用的想法,javaEE是一种规范。除了servlet还包含了诸如JDBC,JSP等等规范,但仅仅是规范;
Spring,是一种非侵入式的框架,Springframework本身几乎仅仅提供了两个功能,AOP和Ioc,甚至MVC都不在SpringFramework中包含,但是其实SpringMVC是依赖于SpringFramework的;
在不使用MVC的情况下,我们仍旧可以通过自定义的方式定义一些Servlet;也可以挨个处理请求的方式完成对指定Servlet的映射,同样也可以在Servlet中完成对JSP的处理;
至于tomcat,有人说他是一个服务器,有人说他是一个servlet容器,实际上…这两种说法都对…首tomcat确实是一个服务器,他能监听8080端口(默认),能对发送到主机的请求进行解析,然后让具体的servlet处理;既然需要Servlet处理,他必定也包含了Servlet,所以说他是一个Servlet容器…
再说到web.xml他是那个框架的内容?
web.xml是javaEE工程的的配置,或许web.xml是javaEE规范中的一部分…而tomcat是一个实现了部分JavaEE的规范;
这个web.xml可以干什么??
web工程夹加载web.xml的顺序是:ServletContext->context-prarm->listener->filter->servlet;
web.xml中的元素
schema
<servlet><servlet-name>servleA</servlet-name><servlet-class>xxx.path.xxx.AServlet</servlet-class></servlet>
对于servlet来说,还需要一个映射器…这个映射器其实是负责对应URL的,在使用了MVC的情况下,MVC会负责的dispatcherServlet会负责转发所有的内容;但是如果没有使用MVC,那么就需要明确的指出servlet-mapping
<servlet-mapping><servlet-name>servletA</servlet-name><url-pattern>/A</url-pattern></servlet-mapping>
mapping will help you finish “Mapping” work,when browser send a http requset to server whit request message;tomcat will get this request and check whit every servlet-mapping to find corresponding servle-name ,then tomcat will search real servlet by servlet-name;
This process in Spring Boot or Spring,you can choice use annotations (@getRequest(“path”) or @postRequest(“path”)),but tomcat is still work whitout this annotations,xml file(servlet-class tag) can tell tomcat where is the process to handle the requset;
If you use MVC,you need just config a DispatcherServlet ;it is still a servlet;but this servlet can receive all request messages;And MVC provide 2 waies to mapping servlet class: annotation and configuration;
MVC need a new xml configuration file,even though you choose to use MVC,the DispatcherServlet is still a servlet mapping which map root request;so Spring MVC need a independent xml configuration file to tell MVC how to process request form DispatcherServle;
MVC’s configuration need config 3 beans at least; include URLHandlerMapping ControllerHandlerAdapter ResourceViewResolver ; and if you dont want to use annotation,you should config controller beans in this independent configuration file like:
<bean id="/hello" class="xxx.path.xxx.Controller">
this bean record the controller class which corresponding url requset need;
Of course,if you choose use annotations in you project,Controller’s bean can be ignored;just use annotation in your class;
