SpringMVC是隶属于Spring框架的一部分,主要是用来进行Web开发,是对Servlet进行了封装。SpringMVC是处于Web层的框架,所以其主要的作用就是用来接收前端发过来的请求和数据然后经过处理并将处理的结果响应给前端,所以如何处理请求和响应是SpringMVC中非常重要的一块内容。
咱们现在web程序大都基于三层架构来实现。
随着互联网的发展,上面的模式因为是同步调用,性能慢慢的跟不上需求,所以异步调用慢慢的走到了前台,是现在比较流行的一种处理方式。
介绍了这么多,对SpringMVC进行一个定义
因为SpringMVC是一个Web框架,将来是要替换Servlet,所以先来回顾下以前Servlet是如何进行开发的?
1.创建web工程(Maven结构)
2.设置tomcat服务器,加载web工程(tomcat插件)
3.导入坐标(Servlet)
4.定义处理请求的功能类(UserServlet)
5.设置请求映射(配置映射关系)
SpringMVC的制作过程和上述流程几乎是一致的,具体的实现流程是什么?
1.创建web工程(Maven结构)
2.设置tomcat服务器,加载web工程(tomcat插件)
3.导入坐标( SpringMVC +Servlet)
4.定义处理请求的功能类( UserControlle r)
5. 设置请求映射(配置映射关系)
6. 将SpringMVC设定加载到Tomcat容器中
打开IDEA,创建一个新的web项目,指定maven-archetype-webapp骨架
因为使用骨架创建的项目结构不完整,需要手动补全,将图中language-level改为8
将pom.xml中多余的内容删除掉,再添加SpringMVC需要的依赖
<?xml version="1.0" encoding="UTF-8"?> 4.0.0 com.itheima springmvc_01_quickstart 1.0-SNAPSHOT war javax.servlet javax.servlet-api 3.1.0 provided org.springframework spring-webmvc 5.2.10.RELEASE org.apache.tomcat.maven tomcat7-maven-plugin 2.1 80 /
说明: servlet的坐标为什么需要添加
@Configuration@ComponentScan("com.itheima.controller")public class SpringMvcConfig {}
@Controllerpublic class UserController { @RequestMapping("/save") public void save(){ System.out.println("user save ..."); }}
将web.xml删除,换成ServletContainersInitConfig
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer { //加载springmvc配置类 protected WebApplicationContext createServletApplicationContext() { //初始化WebApplicationContext对象 AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); //加载指定配置类 ctx.register(SpringMvcConfig.class); return ctx; } //设置由springmvc控制器处理的请求映射路径 protected String[] getServletMappings() { return new String[]{"/"}; } //加载spring配置类 protected WebApplicationContext createRootApplicationContext() { return null; }}
浏览器输入 http://localhost/save 进行访问,会报如下错误:
页面报错的原因是后台没有指定返回的页面,目前只需要关注控制台看 user save ... 有没有被执行即可。
前面我们说过现在主要的是前端发送异步请求,后台响应json数据,所以接下来我们把Controller类的save方法进行修改
@Controllerpublic class UserController { @RequestMapping("/save") public String save(){ System.out.println("user save ..."); return "{'info':'springmvc'}"; }}
再次重启tomcat服务器,然后重新通过浏览器测试访问,会发现还是会报错,这次的错是404
出错的原因是,如果方法直接返回字符串,springmvc会把字符串当成页面的名称在项目中进行查找返回,因为不存在对应返回值名称的页面,所以会报404错误,找不到资源。
而我们其实是想要直接返回的是json数据,具体如何修改呢?
@Controllerpublic class UserController { @RequestMapping("/save") @ResponseBody public String save(){ System.out.println("user save ..."); return "{'info':'springmvc'}"; }}
再次重启tomcat服务器,然后重新通过浏览器测试访问,就能看到返回的结果数据
至此SpringMVC的入门案例就已经完成。
注意事项
名称 | @Controller |
类型 | 类注解 |
位置 | SpringMVC控制器类定义上方 |
作用 | 设定SpringMVC的核心控制器bean |
名称 | @RequestMapping |
类型 | 类注解或方法注解 |
位置 | SpringMVC控制器类或方法定义上方 |
作用 | 设置当前控制器方法请求访问路径 |
相关属性 | value(默认),请求访问路径 |
名称 | @ResponseBody |
类型 | 类注解或方法注解 |
位置 | SpringMVC控制器类或方法定义上方 |
作用 | 设置当前控制器方法响应内容为当前返回值,无需解析 |
为了更好的使用SpringMVC,我们将SpringMVC的使用过程总共分两个阶段来分析,分别是 启动服务器初始化过程 和 单次请求过程
在入门案例中我们创建过一个 SpringMvcConfig 的配置类,再回想前面咱们介绍Spring的时候也创建过一个配置类 SpringConfig 。这两个配置类都需要加载资源,那么它们分别都需要加载哪些内容?
我们先来看下目前我们的项目目录结构:
controller、service和dao这些类都需要被容器管理成bean对象,那么到底是该让SpringMVC加载还是让Spring加载呢?
分析清楚谁该管哪些bean以后,接下来要解决的问题是如何让Spring和SpringMVC分开加载各自的内容。
在SpringMVC的配置类 SpringMvcConfig 中使用注解 @ComponentScan ,我们只需要将其扫描范围设置到controller即可,如
在Spring的配置类 SpringConfig 中使用注解 @ComponentScan ,当时扫描的范围中其实是已经包含了controller,如:
从包结构来看的话,Spring已经多把SpringMVC的controller类也给扫描到,所以针对这个问题该如何解决,就是咱们接下来要学习的内容。
概括的描述下咱们现在的问题就是 因为功能不同,如何避免Spring错误加载到SpringMVC的bean?
针对上面的问题,解决方案也比较简单,就是:
具体该如何排除:
方式一:修改Spring配置类,设定扫描范围为精准范围。
@Configuration@ComponentScan({"com.itheima.service","comitheima.dao"})public class SpringConfig {}
说明:
上述只是通过例子说明可以精确指定让Spring扫描对应的包结构,真正在做开发的时候,因为Dao最终是交给 MapperScannerConfigurer 对象来进行扫描处理的,我们只需要将其扫描到service包即可。
方式二:修改Spring配置类,设定扫描范围为com.itheima,排除掉controller包中的bean
@Configuration@ComponentScan(value="com.itheima", excludeFilters=@ComponentScan.Filter( type = FilterType.ANNOTATION, classes = Controller.class ))public class SpringConfig {}
如何测试controller类已经被排除掉了?
public class App{ public static void main (String[] args){ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); System.out.println(ctx.getBean(UserController.class)); }}
如果被排除了,该方法执行就会报bean未被定义的错误
注意:测试的时候,需要把SpringMvcConfig配置类上的@ComponentScan注解注释掉,否则不会报错
为啥需要注释掉呢?出现问题的原因是,
最后一个问题,有了Spring的配置类,要想在tomcat服务器启动将其加载,我们需要修改ServletContainersInitConfig
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer { protected WebApplicationContext createServletApplicationContext() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(SpringMvcConfig.class); return ctx; } protected String[] getServletMappings() { return new String[]{"/"}; } protected WebApplicationContext createRootApplicationContext() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(SpringConfig.class); return ctx; }}
对于上述的配置方式,Spring还提供了一种更简单的配置方式,可以不用再去创建 AnnotationConfigWebApplicationContext 对象,不用手动 register 对应的配置类,如何实现?
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer { protected Class<?>[] getRootConfigClasses() { return new Class[]{SpringConfig.class}; } protected Class<?>[] getServletConfigClasses() { return new Class[]{SpringMvcConfig.class}; } protected String[] getServletMappings() { return new String[]{"/"}; }}
名称 | @ComponentScan |
类型 | 类注解 |
位置 | 类定义上方 |
作用 | 设置spring配置类扫描路径,用于加载使用注解格式定义的bean |
相关属性 | excludeFilters:排除扫描路径中加载的bean,需要指定类别(type)和具体项(classes) includeFilters:加载指定的bean,需要指定类别(type)和具体项(classes) |
留言与评论(共有 0 条评论) “” |