

Tomcat是servlet的容器,想用HttpServlet需要导入tomcat jar包


下图是没用springmvc时的场景,首先在web.xml里面配置访问路径为/Hello然后


通过get请求去调用login方法最后重定向到index.jsp中


index.jsp里面的内容


重定向到index.jsp中

在控制台获取到username里面的内容

下图是用springmvc的场景
首先先创建maven项目
然后再添加mvc的依赖包在pom.xml里面


然后在web.xml里面配置分发控制器
url-pattern / 表示除了.jsp都拦截到控制器上

在web.xml文件里配置要启动的spring-mvc.xml文件,loaf-on-startup表示在tomcat服务器启动的时候就会创建bean(这样可以减少资源消耗)

首先tomcat启动会先初始化(init)DispatcherServlet,然后去加载spring-mvc.xml文件,然后假如用户去访问login,然后url-pattern是/就会被DispatcherServlet拦截到会分发到控制器里控制器会交给ioc处理




在创建bean组件的时候需要导入扫描包,通过注解创建bean



在index.jsp文件中配置信息

在helloController里面配置映射路径为/Hello

上图@RequestMapping简化了下图url-patttern一系列的设置

自动匹配方法中的name属性,在浏览器以get请求的方法在浏览器以参数名写name的值



springmvc运行流程

首先请求/Hello会被DispatcherServlet拦截下来


然后根据/Hello去项目里找


把方法拿到在Handler里,通过HandlerAdaper调用handler并且把handler封装到ModelAndView里面



、
把index.jsp封装到ViewReslover里面


最后进行视图渲染显示到客户端上
