An-Introduction-To-the-Spring-MVC-Framework:对Spring-MVC框架的介绍课件.ppt

上传人(卖家):晟晟文业 文档编号:4687768 上传时间:2023-01-01 格式:PPT 页数:34 大小:169.18KB
下载 相关 举报
An-Introduction-To-the-Spring-MVC-Framework:对Spring-MVC框架的介绍课件.ppt_第1页
第1页 / 共34页
An-Introduction-To-the-Spring-MVC-Framework:对Spring-MVC框架的介绍课件.ppt_第2页
第2页 / 共34页
An-Introduction-To-the-Spring-MVC-Framework:对Spring-MVC框架的介绍课件.ppt_第3页
第3页 / 共34页
An-Introduction-To-the-Spring-MVC-Framework:对Spring-MVC框架的介绍课件.ppt_第4页
第4页 / 共34页
An-Introduction-To-the-Spring-MVC-Framework:对Spring-MVC框架的介绍课件.ppt_第5页
第5页 / 共34页
点击查看更多>>
资源描述

1、An Introduction To the Spring M.V.C.FrameworkReference From Website ByTom KochanowiczOutline Where weve been M.V.C.Frameworks Why Use Spring Framework IoC(Inversion of Control)ExamplesWhere weve beenWeb based programming“The Servlet Way”JSP or HTML Form Submit to servlet Servlet Processes data&Outpu

2、ts informationWorks well for small applications but can quickly grow out of control because HTML,scrip-lets,java script,tag-libraries,database access,and business logic makes it difficult to organize.Put simply,lack of structure can cause a“soup”of different technologies.JSPs compile to Servlet“A Be

3、tter Way?”Separate the Data Access,Business Logic and Presentation using a M.V.C.Framework.Choose a M.V.C.framework:WebWork,Spring,Struts,Java-Server-Faces,Tapestry plus“many more”Things change:Struts,by far is the most popular;same creator of Struts(Craig McClanahan)is the co-spec leader of Java Se

4、rver Faces.Webwork evolved to Webwork2 Tapestry-has been around for awhile.Spring “newer”of the frameworks.Integrates well with any other framework or by itself.Why Spring Framework?All frameworks integrate well with Spring.Spring offers an open yet structured framework,using dependency-injection,a

5、type of inversion-of-control to integrate different technologies together.Consistent Configuration,open plug-in architecture Integrates well with different O/R Mapping frameworks like Hibernate Easier to test applications with.Less complicated then other frameworks.Active user community.Want to inte

6、grate your existing web-app with a Spring middle tier?Struts http:/ Work http:/ Tapestry http:/www.springframework.org/docs/integration/tapestry.html What if I like Microsoft.NET?Then try Spring Framework.NEThttp:/ I chose to learn the Spring framework Because of IoC/Dependency Injection you can eas

7、ily change configurations.Addresses end-to-end requirements,not just one part.Spring is well organized and seems easier to learn then struts.Portable across deployment environments.Integrates well with HibernateMeant to wet your appetite and note be compressive.Downsides:Not as mature as other frame

8、works(but very stable).Market share is small at this time,but rapidly growing.dependency-injection(Inversion-of-control)is a different way of thinking(This is actually a plus).Not a-lot of tool support for Spring yet.A plug-in for Eclipse is available.Depends on who you ask.Spring is not just a Pres

9、entation M.V.C.Framework:Persistence support:Spring also supports A JDBC Framework that makes it easier to create JDBC Apps.Supports O/R mapping Frameworks making it easier to use O/R tools like Hibernate&JDOSpring offers Connection Pooling for any POJO.Supports transaction frameworkHas good support

10、 for aspect-oriented-programmingPlus much more.What is dependency-injection&why use it?Dependency-injection(a type of IoC)is when you let your framework control your application.Spring links objects together instead of the objects linking themselves together.Spring object linking is defined in XML f

11、iles,allowing easy changes for different application configurations thus working as a plug in architecture.Dependency injection is where the control of the application is inverted to the framework.This control is configured in the framework with an XML file.Without Dependency-Injection/IoCObject AOb

12、ject BObject CcreatescreatesAn object creating its dependencies without IoC leads to tight object coupling.Object AObject BObject CsetB(IB)setC(IC)Object A contains setter methods that accept interfaces to objects B and C.This could have also been achieved with constructors in object A that accepts

13、objects B and C.With Dependency-Injection/IoCAllows objects to be created at higher levels and passed into object so they can use the implementation directlySpring supports two types of dependency injection“setter-based”and“constructor based”injectionCode Example of setter based injection:myemail.ad

14、dress *beans are accessed by there“bean name”Interpretation of the above code:Person person=new Person();person.setEmail(“myemail.address”);This code creates a Person object and calls the setEmail()method,passing in the string defined as a value.Constructor based injection /data/file1.data Interpret

15、ation of the above code:FileDataReader fileDataReader=new FileDataReader(“/data/file1.data”);DataProcessor fileDataProcessor=new DataProcessor(fileDataReader);Spring provides a JDBC Template that manages your connections for you.*Simple example of connecting to a datasource.*ProductManagerDaoJdbc im

16、plements ProductManagerDao public void setDataSource(DataSource ds)this.ds=ds;*No need to change java code when changing datasource;change in Spring bean XML file below.jdbc:mysql:/localhost/testSpring WebKey ConceptsSpring Web Controllers In an MVC architecture your controllers handle all requests.

17、Spring uses a DispatcherServlet”defined in the web.xml file to analyze a request URL pattern and then pass control to the correct Controller by using a URL mapping defined in a“Spring bean”XML file.Spring Web Container SetupIn your Web Container,the Spring“bean”XML file exists in the same directory

18、as your web.xml file with a“-servlet.xml”appended to it.webapps/tradingapp/WEB-INF/tradingapp-servlet.xml,web.xml)/classes/lib(all jar files)The dispatcher servlet is mapped to the name“tradingapp”so it knows to look in the“tradingapp-servlet.xml”file to look-up a URL-to-Controller match.Example of

19、web.xml file tradingappDispatcherServlettradingapp *.htm*Any URL ending with an“.htm”pattern is routed to the DispatcherServlet,the DispatcherServlet loads the tradingapp-servlet.xml file and routes the user to the correct controller.Our Demo Logon Form at URLhttp:/localhost/tradingapp/logon.htmThe

20、tradingapp-servlet.xml file a.k.a.Spring beans XML file is where the majority of your configuration is done.For example:If working with the URL:/logon.htmBecause the URL ends with.htm the DispatcherServlet loads the tradingapp-servlet.xml file to determine which controller to use.The tradingapp-serv

21、let.xml file uses Springs SimpleUrlHandlerMapping class to map the URL to a controller,in this case the LogonFormControllerNextwhat the tradingapp-servlet.xml looks like.tradingapp-servlet.xml truecredentials/propertycom.tradingapp.Credentialslogonportfolio.htmThis class extends Springs SimpleFormCo

22、ntrollerWhich defines a setSuccessView()methodIf it passes“validator”then successView,passes to portfolio.htm pageReview of the process so far User goes to this URL:http:/tradingapp/logon.htm Since the URL ends with“.htm”,the tradingapp-servlet.xml file is loaded to determine what controller to use.

23、The says to refer to the Since the LogonFormController extends SimpleFormController we can use the methods defined in the SimpleFormController class to do all kinds of form checking,e.g.validation.What our LogonFormController Looks Like.public class LogonFormController extends SimpleFormController p

24、ublic ModelAndView onSubmit(Object command)throws ServletException return new ModelAndView(new RedirectView(getSuccessView();Remember our tradingapp-servler.xml file?truecredentials/propertycom.tradingapp.Credentialslogonportfolio.htmIf no validation errors,go heresuccessView/portfolio.htmWhere do I

25、 go if there is a validation error in my logon page?tradingapp-servler.xmltruecredentials/propertycom.tradingapp.Credentialslogonportfolio.htm *Your LogonFormController will check the validation“first”without writing any additional code because your LogonFormController extends Springs SimpleFormCont

26、roller.Next:The LogonValidator implements Springs Validator interface.On error go back to formView,that is where you started.Logon page with error messageNext:code for LogonValidator implements Springs ValidatorExample code of validatortradingapp-servler.xmlcredentials/propertycom.tradingapp.Credent

27、ialslogonportfolio.htmpublic class LogonValidator implements Validator public void validate(Object obj,Errors errors)Credentials credentials=(Credentials)obj;if(credentials.getPassword().equals(guest)=false)errors.rejectValue(password,error.login.invalid-pass,null,Incorrect Password.);Next:Command/F

28、orm Backing BeanCommand/form backing beanCommand/Form Backing Bean is a POJOpublic class Credentials private String username;private String password;public String getPassword()return password;public void setPassword(String password)this.password=password;public String getUsername()return username;pu

29、blic void setUsername(String username)this.username=username;Next:Why its called a“command”or“form backing bean”Command/Form Backing Bean is a POJOlogon.htm formUsername:Password:public class Credentials private String username;private String password;public String getPassword()return password;publi

30、c void setPassword(String password)this.password=password;public String getUsername()return username;public void setUsername(String username)this.username=username;The logon form is“backed”by the Credentials bean and given a commandName of“credentials”defined in out springapp-servlet.xml file.“crede

31、ntials”will be our“command object”we will use to bind the form to the bean.Next:another look at springapp-servlet.xml filespringapp-servlet.xml filecredentials/propertycom.tradingapp.Credentialslogonportfolio.htmWe use the commandName“credentials”with Springs tag library,to bind the Credentials bean to the logon form.Next:Code that shows logon form binding to commandNamelogon form binding to commandName using Springs Tag LibraryDevX.com Stock-Trading System Logoninput type=text name=username Springs taglib has bound the bean to the form

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公、行业 > 各类PPT课件(模板)
版权提示 | 免责声明

1,本文(An-Introduction-To-the-Spring-MVC-Framework:对Spring-MVC框架的介绍课件.ppt)为本站会员(晟晟文业)主动上传,163文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。
2,用户下载本文档,所消耗的文币(积分)将全额增加到上传者的账号。
3, 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(发送邮件至3464097650@qq.com或直接QQ联系客服),我们立即给予删除!


侵权处理QQ:3464097650--上传资料QQ:3464097650

【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。


163文库-Www.163Wenku.Com |网站地图|