Servlet 到 Springmvc 到 注解版的SpringMvc 到 SpringBoot web 开发的演变
1、学习总结
1、servlet是什么
servlet 是运行在服务器上的一个程序,用来接收和对请求作出响应。
servlet 继续演进到mvc
2、mvc是什么
model(dao,service) view(jsp) controller(servlet) 模型 视图 控制器 是一种架构规范
3、mvc 做了哪些事情
1、将url 映射到java 类或者java类的方法
2、封装用户提交的数据
3、处理请求-调用相关的业务处理--封装响应数据
4、将响应的数据进行渲染:jsp html 等表示层数据
5、请求的处理流程
url---servlet -- 到 请求的处理服务端(类或者方法)--返回一个modelAndView -- 渲染给前端页面。
6、servlet 和springmvc的区别
1、servlet 需要在web.xml中针对不同的请求路径配置不同的servlet 和servletmapping标签,servlet类里面需要指定重定向或者转发的页面
2、springmvc 需要只需要在web.xml中配置一个调度的servlet(DispatcherServlet),所有的请求都交给它来分发到不同的控制器,
同时呢所有的转发的页面做统一的处理,不用每个请求都写了。
7、springboot 配置SpringMvc
2、servlet的demo
web.xml中的配置,需要配置各种各样的 servlet,和servletmaping 标签。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>myServlet</servlet-name> <servlet-class>com.ali.gts.sofa.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <!--把请求映射到 类上 --> <!-- session 的配置--> <!--<session-config>--> <!--<session-timeout>15</session-timeout>--> <!--</session-config>--> <!----> <!--<welcome-file-list>--> <!--<welcome-file>index.jsp</welcome-file>--> <!--</welcome-file-list>--> </web-app>
3、代码链接
文件里面的
4、什么是SpringMvc,代码
Spring 和MVC整合的比较好,所以叫SpringMvc
4.1 web.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <!-- 声明Dispatcher,告诉Web容器我们将使用springMVC的DispatcherServlet --> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- DispatcherServlet在加载时会需要SpringMVC的配置文件,因此需要声明该配置文件的位置。 --> <!-- 如果不配置该项,则应用会默认的去WEB-INF下寻找名字为[servlet-name]-servlet.xml的文件。 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置url-pattern,设置为 / 拦截所有的URL请求,并映射到dispatcher --> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/h1</url-pattern> </servlet-mapping> <!--<servlet>--> <!--<servlet-name>myServlet</servlet-name>--> <!--<servlet-class>com.kuang.servlet.MyServlet</servlet-class>--> <!--</servlet>--> <!--<servlet-mapping>--> <!--<servlet-name>myServlet</servlet-name>--> <!--<url-pattern>/hello</url-pattern>--> <!--</servlet-mapping>--> </web-app>
4.2 springMvc的配置文件
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 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"> <!-- 扫描包交给spirng的ioc来管理,让指定目录下的注解生效 --> <context:component-scan base-package="com.kuang"/> <!-- 使用默认的 servlet handler, 过滤掉 .css .mp4 .js等的文件--> <mvc:default-servlet-handler/> <mvc:annotation-driven/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
4.3 控制器
@Controller public class Mycontroller { @RequestMapping("/h1") public String hello(){ return "hello"; } }
5、注解版的SpringMvc基于Servlet3.0
不用配置文件,不用web. xml,归根结底还是需要实现 ServletContainerInitializer
public class MyServletInit extends AbstractAnnotationConfigDispatcherServletInitializer{ /** * * 获取跟容器的配置类,以前是用Spring的配置文件中的<listener></listener> 标签来创建根容器的 * * @return */ @Nullable protected Class<?>[] getRootConfigClasses() { return new Class[]{RootConfig.class}; } /** * * * 获取Web容器的配置类,以前是根据Springmvc的配置文件来的 * @return */ @Nullable protected Class<?>[] getServletConfigClasses() { return new Class[]{AppConfig.class}; } /** * * * 获取servlet的映射信息的 * @return */ protected String[] getServletMappings() { //这里拦截所有的静态资源(xx.js,xx.png),但是不包括*.jsp文件 // /* 是拦截所有的,包括*.jsp文件 return new String[]{"/"}; } } /** * 父容器 * Spring容器不扫描controller,交给springmvc来扫描 * excludeFilters 指定那些不要扫描必须传一些Filter数组 type是指定按照那些规则来排除,这个根据注解来排除,标注了@Controller注解的类不扫描 */ @ComponentScan(value = "com.gts.sofa",excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})}) public class RootConfig { } /** * 子容器 * SpringMVC只扫描Controller * 必须设置useDefaultFilters=false 必须禁用默认的过滤规则 * includeFilters 意思是扫描的时候只扫描某些类型,根据注解来扫描,扫描那些用@Controller注解的类,并且禁用掉默认的。 */ @ComponentScan(value = "com.gts.sofa",includeFilters = {@ComponentScan.Filter(type= FilterType.ANNOTATION,classes = {Controller.class})}, useDefaultFilters = false) @EnableWebMvc public class AppConfig extends WebMvcConfigurerAdapter { //定制视图解析器 这里配置的路径不是resource下面的,是web目录下面的 @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp("/WEB-INF/views/",".jsp"); } } @Controller public class Mycontroller { @Autowired MyService myService; @RequestMapping("/hello") public String hello(){ String name = myService.sayHello("tomcat"); System.out.println(name); return "name"; } @RequestMapping("/hello1") @ResponseBody public String hello1(){ String name = myService.sayHello("tomcat"); System.out.println(name); return name; } }