




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Spring MVC hello world annotation example(中英文对照)大白菜翻译BLOG:/xiaolincc26/homeIn thistutorial, it shows how to create a Spring MVC annotation-based hello world example.在这篇文中,展示的是如何创建一个Spring MVC基于注解的hello world例子。Note(注意)This annotation-based example is converted from the last Spring MVC hello world XML-based example. So, please compare and spots the different.这个基于注解的例子是基于Spring MVC hello world XML-based进行修改。因此,请比较和发现它们的不同地方。1.Spring DependencySpring依赖To use the Springs annotation, just include the core Spring libraries.使用Spring的注解功能,只要包含一个Spring核心库文件。org.springframeworkspring2.5.6org.springframeworkspring-webmvc2.5.62.Controller & Handler Mapping控制和处理的映射With Spring MVC annotation,基于Spring MVC注解1.Controller The controller class is no longer need to extends base controller likeAbstractController orSimpleFormController, just simply annotate the class with aController annotation.控制控制类无需再扩展基础控制像抽象控制或者SimpleFormController,只需要通过一个简单的注解Controller注释。2.Handler Mapping No more declaration for the handler mapping likeBeanNameUrlHandlerMapping,ControllerClassNameHandlerMapping orSimpleUrlHandlerMapping,all are replaced with a standardRequestMappingannotation.处理映射不再为处理映射做定义像BeanNameUrlHandlerMapping,ControllerClassNameHandlerMapping或者SimpleUrlHandlerMapping,取代它们全部的是一个标准的RequestMapping注解。HelloWorldCmon.controller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.servlet.ModelAndView;ControllerRequestMapping(/helloworld)publicclass HelloWorldControllerRequestMapping(method = RequestMethod.GET)public ModelAndView helloWorld()ModelAndView model =new ModelAndView(HelloWorldPage);model.addObject(msg, hello world);return model;If theRequestMappingis applied at class level (canapply at method level for multi-actions controller), it required to put aRequestMethodto indicate which method to handle the mapping request.如果 RequestMapping是应用于类一级(可以使用方法一级适用于multi-actions controller),它要求放置一个RequestMethod来表示处理方法的请求映射关系。In this case, if an URL “/helloworld”is requested, it will map to thisHelloWorldController, and handle the request withhelloWorld()method.既然这样,如果一个URL“/helloworld”发送请求,它将会映射到HelloWorldController,并且通过helloWorld()方法处理请求。3. View ResolverSadly, you are still required to configure the view resolver.令人遗憾的是,你依然需要配置veiwResolver。/WEB-INF/pages/.jsp4. ViewA simple JSP for demonstration.一个简单的JSP示例。Spring MVC Hello World Annotation Example$msg5. Components Auto Scanning自动扫描主件To make the annotation work, you have to enable theSprings auto component scanning feature through the element.为了使注释工作起来,你必须通过元素提供spring自动扫描主件功能。NoteYou may interest at this Spring auto scanning components article.你可能对Spring auto scanning components这篇文章感兴趣。6. Spring ConfigurationSee a complete Springconfiguration file.看一个完整的spring配置文件。mvc-dispatcher-servlet.xml/WEB-INF/pages/.jsp7. web.xmlNo exception, you are still required to configure the web.xml to enable the Spring MVC features.毫无疑问,你依然被要求配置web.xml才能使spring MVC发挥作用。web.xml.mvc-dispatcherorg.springframework.web.servlet.DispatcherServlet1mvc-dispatcher*.htmcontextConfigLocation/WEB-INF/mvc-dispatcher-servlet.xmlorg.springframework.web.context.ContextLoaderListener.8. DemoAccess it, URL :http:/localhost:8080/SpringMVC/helloworld.htm输入并运行URL:http:/localhost:8080/SpringMVC/helloworld.htmNoteIf you compare this Spring MVC annotation-based hello world example with thisXML-based example, you can see the annotation approach is more easier and flexible in wiring the controller class and URL handler mapping, be
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年侗族民族常识题库及答案
- 英语自荐真题及答案
- 2025年韩语概况试题及答案
- 工程营销方案案例模板(3篇)
- 工程裂缝修复方案(3篇)
- 医院医疗废物流失、泄漏、扩散和意外事故处置应急预案
- 2025年人格测试题目解析及答案
- 中医妇科学(月经病)练习试卷及答案
- 园林绿化专业知识技能考试试题及答案
- 泵车知识题库及答案
- 高二秋季开学第一课班会课件:启航高二把握未来
- 坐席岗位笔试题目及答案
- 2025年吉林省高考物理试卷(含答案解析)
- 2024陆上风电项目造价指标
- 生命教育 课件 .第一章 生命诞生
- 2025年安徽省农业职业技能大赛(水生物病害防治员)备赛试题库(含答案)
- HACCP体系评审表范本
- openEuler系统管理与服务器配置 课件 第8章网络连接
- 《民营经济促进法》解读与案例分析课件
- DG∕TJ 08-2035-2014 建设工程监理施工安全监督规程
- 江苏保安考试试题及答案
评论
0/150
提交评论