博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring+SpringMVC+MyBatis深入学习及搭建(十三)——SpringMVC入门程序(二)
阅读量:6240 次
发布时间:2019-06-22

本文共 16510 字,大约阅读时间需要 55 分钟。

1.非注解的处理器映射器和适配器

1.1非注解的处理器映射器

前面我们配置的org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping是非注解的处理器映射器。这种映射器其实有一个问题就是要一个bean里面配置一个url。

现在我们再配置另一个非注解映射器

org.springframework.web.servlet.handler.SimpleUrlHandlerMapping,它会将url和handler集中进行配置。

ItemsController1
ItemsController1

疑问:可以同时配置这两个映射器吗?

解答:多个映射器可以并存,前端控制器判断url能让哪些映射器映射,就让正确的映射器处理。

1.2非注解的处理器适配器

前面我们配置的org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter是一个非注解的适配器。它要求你编写的Handler要实现Controller接口。

再说另一个非注解的适配器:

org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter。它要求编写的Handler实现HttpRequestHandler接口。

public class ItemsController2 implements HttpRequestHandler{    @Override    public void handleRequest(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        //调用service查找数据库,查询商品列表,这里使用静态数据模拟        List
itemsList=new ArrayList
(); Items items_1=new Items(); items_1.setName("联想笔记本"); items_1.setPrice(6000f); items_1.setDetail("ThinkPad T430 联想笔记本电脑!"); Items items_2 = new Items(); items_2.setName("苹果手机"); items_2.setPrice(5000f); items_2.setDetail("iphone6苹果手机!"); itemsList.add(items_1); itemsList.add(items_2); //设置模型数据 request.setAttribute("itemsList", itemsList); //设置转发的视图 request.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(request, response); // 从上边可以看出此适配器器的handleRequest方法没有返回ModelAndView,可通过response修改定义响应内容,比如返回json数据:// response.setCharacterEncoding("utf-8");// response.setContentType("application/json;charset=utf-8");// response.getWriter().write("json串"); }}

在springmvc.xml中配置:

2.DispatcherServlet.properties

前端控制器从上边的文件中加载处理映射器、适配器、视图解析器等组件,如果不在springmvc.xml中配置,则使用默认加载的。

3.注解的处理器映射器和适配器

在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping注解映射器。

在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping注解映射器。

在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适配器。

在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注解适配器。

3.1配置注解映射器和适配器

3.2开发注解Handler

使用注解的映射器和注解的适配器。(注解的映射器和注解的适配器必须配对使用,理解为不能使用非注解映射器进行映射)

/** * 开发注解Handler * 使用@Controller标明它是一个控制器 * @author Joanna.Yan *  */@Controllerpublic class ItemsController3{        //商品查询列表    //@RequestMapping实现对queryItems方法和url进行映射,一个方法对应一个url    //一般建议将url和方法名写成一样,方便维护    @RequestMapping("/queryItems")    public ModelAndView queryItems() throws Exception{        //调用service查找数据库,查询商品列表,这里使用静态数据模拟        List
itemsList=new ArrayList
(); Items items_1=new Items(); items_1.setName("联想笔记本"); items_1.setPrice(6000f); items_1.setDetail("ThinkPad T430 联想笔记本电脑!"); Items items_2 = new Items(); items_2.setName("苹果手机"); items_2.setPrice(5000f); items_2.setDetail("iphone6苹果手机!"); itemsList.add(items_1); itemsList.add(items_2); //返回ModelAndView ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("itemsList", itemsList); //指定视图 modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp"); return modelAndView; } //定义其它方法 //商品添加 //商品修改}

3.3在spring容器中加载Handler

使用组件扫描器省去在spring容器配置每个controller类的繁琐。使用<context:component-scan/>自动扫描标记@Controller的控制类。

3.4 部署调试

访问:http://localhost:8080/SpringMVC/jyqueryItems.action

注意:在测试过程中单个配置注解的Handler运行没有问题,在配置组件扫描器运行时,启动Tomcat报错:

严重: Context initialization failedjava.lang.IllegalArgumentException    at org.springframework.asm.ClassReader.
(Unknown Source) at org.springframework.asm.ClassReader.
(Unknown Source) at org.springframework.asm.ClassReader.
(Unknown Source) at org.springframework.core.type.classreading.SimpleMetadataReader.
(SimpleMetadataReader.java:52) at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80) at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101) at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76) at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:298) at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300) at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300) at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:230) at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:153) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:130) at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:285) at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461) at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661) at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517) at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458) at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138) at javax.servlet.GenericServlet.init(GenericServlet.java:158) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5266) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5554) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1258) at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1918) at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)六月 14, 2017 4:43:03 下午 org.apache.catalina.core.ApplicationContext log严重: StandardWrapper.Throwablejava.lang.IllegalArgumentException at org.springframework.asm.ClassReader.
(Unknown Source) at org.springframework.asm.ClassReader.
(Unknown Source) at org.springframework.asm.ClassReader.
(Unknown Source) at org.springframework.core.type.classreading.SimpleMetadataReader.
(SimpleMetadataReader.java:52) at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80) at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101) at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76) at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:298) at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300) at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300) at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:230) at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:153) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:130) at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:285) at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461) at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661) at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517) at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458) at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138) at javax.servlet.GenericServlet.init(GenericServlet.java:158) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5266) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5554) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1258) at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1918) at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)六月 14, 2017 4:43:03 下午 org.apache.catalina.core.StandardContext loadOnStartup严重: Servlet [springmvc] in web application [/SpringMVC] threw load() exceptionjava.lang.IllegalArgumentException at org.springframework.asm.ClassReader.
(Unknown Source) at org.springframework.asm.ClassReader.
(Unknown Source) at org.springframework.asm.ClassReader.
(Unknown Source) at org.springframework.core.type.classreading.SimpleMetadataReader.
(SimpleMetadataReader.java:52) at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80) at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101) at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76) at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:298) at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300) at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300) at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:230) at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:153) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:130) at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:285) at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461) at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661) at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517) at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458) at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138) at javax.servlet.GenericServlet.init(GenericServlet.java:158) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5266) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5554) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1258) at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1918) at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
View Code

原因是:我的Tomcat的jdk是1.8,项目的jdk版本是1.7,两者不一样造成的。记得要统一版本!

4.前端控制器源码分析

通过前端控制器源码分析springmvc的执行过程。

 第一步:前端控制器接收请求

调用doDispatch

 第二步:前端控制器调用处理器映射器查找Handler

 

第三步:调用处理器适配器执行Handler,得到执行结果ModeAndView

第四步:视图渲染,将model数据填充到request域。

视图解析,得到view:

调用view的渲染方法,将model数据填充到request域。

渲染方法:

5.视图解析器配置前缀和后缀

springmvc.xml中:

程序中不用指定前缀和后缀:

//指定视图//        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");        //下边的路径,如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀,修改为        modelAndView.setViewName("items/itemsList");

 

转载地址:http://akdia.baihongyu.com/

你可能感兴趣的文章
转发:修饰符
查看>>
【转载】Linux下configure命令详细介绍
查看>>
图片中转站
查看>>
DSP c6678的启动方式
查看>>
【Linux】解决Android Stadio报错:error in opening zip file
查看>>
功能(一):添加影像服务图层
查看>>
选择伊始
查看>>
PHP中继承
查看>>
总结各种容器特点
查看>>
SQL Server高级查询
查看>>
13-Flutter移动电商实战-ADBanner组件的编写
查看>>
ubuntu 16.04 启用root用户方法
查看>>
阿里巴巴矢量图标库
查看>>
南阳理工904
查看>>
1. Two Sum
查看>>
Tomcat学习总结(10)——Tomcat多实例冗余部署
查看>>
2017书单
查看>>
Redis学习总结(1)——Redis内存数据库详细教程
查看>>
python 生成器与迭代器
查看>>
VS2017 调试期间无法获取到变量值查看
查看>>