JavaWeb development framework, you have to really know how much?

A long time ago to write such an article, and now the rise of Internet software and traditional software industry, so many people have become program ape, ape that is now flooding the program, the uneven level, many of them do not understand a nodding acquaintance principle, a departure from the idea of ​​software development!

Highlights:

Now we are using the web front page of such velocity template language development, abandoned jsp, this choice will give us java web developer what kind of benefits.

There is a very general framework of benefits:

First, it can speed up the development process, developers reuse code will save a lot of time and effort in similar projects. Framework provides pre-built modules of code to perform the tedious task.

Advantage of the framework is only one: that allows users to only care about the development of core business framework to help you shield a variety of technical problems with the existing technology business development independent.

Like jQuery, struts2 or springMVC good reason why this type of framework, that is, at this point they do good, that many programmers have been using it do not know the true face of the existing technology, so we want to understand struts2 better, more skilled and in-depth use, where we have to jump out struts2 technologies to struts2 source servlet technology, under the careful study of the characteristics of servlet, the only way we can learn to better struts2 framework.

JavaWeb development framework, you have to really know how much?

 

Earlier I mentioned the traditional servlet is defined according to the http protocol, it will be according to your request method (post or get way) to handle the user's request, but for a program developer, a request to a specific url , in fact, for that is a function provided by the external server, or the server is a server-side external action, if we use servlet development process we have to put http movements into specific business operation, which allows the program development cumbersome and enhance the difficulty of development, so struts2 replace the servlet javabean on the issue of shielding the servlet http request method of action and specific business transformation, javabean in each and every method can be a url for each request this inevitably reduces the difficulty of the problem developed.

jsp is actually a server-side technology rather than client-side technology, but it seems more like html technology, first developed in the jsp java code is written directly to the page where this harm everyone knows, after javaEE specification provides a custom label technology, using a similar way to parse html tags java code, struts2 framework provides a complete set of custom label technology for business development will be brought about a qualitative leap.

spring provides a way, this way is the spring to provide a container, we define dependencies of each object in the xml file, complete the construction of the container objects, java code when we need to use an instance when you can from container acquisition, the building was taken over operation of the spring container objects, so it is called inversion of control,

Inversion of Control, which means that originally belonged to build a java program function object handed over to take over the vessel,

Dependency injection is when you want to use an object when the container will inject it into the program, which is called dependency injection.

aop settlement procedures in the development of independent transactional, and core business issues

Spring action is being billed as decoupling between the program, to reduce the degree of coupling spring between the different modules, the reason is that in program development in the communication of information between the different modules is accomplished by passing the object, and the object smooth transfer is to a reasonable construction of a good object, and manage objects are built, will be able to manage object is passed, which is the benefits of spring to bring the system architecture design.

JavaWeb development framework, you have to really know how much?

 

Now do the project will use the framework

SSH (Struts + SpringMVC + spring + hibernate), SSM (Struts / springMVC + Spring + Mybatis), to do the project in order to better stratification of decoupling, you do not have a hierarchical framework can be fully able to solve, so why do not need multi-frame it?

而且现在公司很多web项目的控制层的技术框架由struts2迁移到springMVC,为什么我们现在做Java的web开发,会选择struts2或者springMVC这样的框架,而不是使用servlet加jsp这样的技术呢?特别是现在我们web的前端页面都是使用velocity这样的模板语言进行开发,抛弃了jsp,这样的选择又会给我们java的web开发带来什么样的好处,延着这个问题的思路,我又发现新的疑问,为什么现在很多java企业级开发都会去选择spring框架,spring框架给我们开发的应用带来了什么?这么一深入的想还真是不太想的明白,我似乎很难找到一串能让人完全信服的答案,最终我发现,这些我认为我很熟悉很常用的技术,其实还有很多让我陌生不解的地方,这些陌生和不解的地方也正是我是否能更高层次使用它们的关键,不过struts2,spring这样的技术经过这么多年的积累已经是相当庞大和复杂,它们的面很广,本人虽然已经用了它们很长时间,还是有很多技术不熟悉和不清楚.

使用框架有一个很笼统的好处:第一,可以加快开发进程,在类似项目中重用代码将为开发者节省大量的时间和精力.框架为执行繁琐的代码任务提供预建模块.这个好处放在哪一个框架都是很适用的.但是再往深层的想呢?

其实在软件里有很多优秀的框架,有一种类型的框架,它的特点是建立在一个现有技术的基础上,提供和现有技术一样业务功能的技术框架,这个新的技术框架比原技术更加易用,更加健壮同时功能更加强大,例如:jQuery,以及本文所要谈到的struts2和springMVC,深究这些框架都是相当之复杂,但是它们的优点其实只有一个:就是让使用者只关心核心业务的开发,框架帮你屏蔽原有技术跟业务开发无关的各类技术问题。像jQuery,struts2或springMVC这类框架之所以优秀,就是它们在这点上做的太好了,以至于很多使用它的程序员都已经不清楚原有技术的真实面目,因此我们要将struts2理解的更好,使用的更加熟练和深入,这里我们就要跳出struts2的技术,到struts2技术的源头servlet,仔细研究下servlet的特点,只有这样我们才能把struts2框架学的更好。

Servlet的作用

是接收浏览器传给服务端的请求(request),并将服务端处理完的响应(response)返回给用户的浏览器,浏览器和服务端之间通过http协议进行沟通,其过程是浏览器根据用户的选择将相关信息按http协议报文的规范组装请求的http报文,报文通过网络传输到指定的服务器,服务器通过特定的web容器接收这个报文信息,例如:tomcat,jetty,jboss这样的web容器,web容器会将http报文解析出来,如果是用户请求,最终解析出来的报文信息会用一个request对象存储起来,服务端使用这个request做完相应的处理后,服务端程序将结果信息封装到response对象里,然后将response对象交给web容器,web容器则把这个response对象转变为http协议的报文,并将报文回传给浏览器,浏览器最后解析这个响应报文,将最终结果展示给用户。

servlet接口

Web容器创造了servlet接口,servlet接口就是开发人员自己实现业务逻辑的地方,程序员开发servlet就好比做填空题,而填空题的语境或者说上下文提示就是由request和response对象,但是javaEE规范里的servlet接口很简单,就三个方法init,service和destory,但是这个接口太笼统,所以规范里还提供了一个HttpServlet类,这个类根据http请求类型提供了doGet,doPost等方法,servlet接口最大的特点就是根据http协议的特点进行定义,因此做servlet开发时候如果使用者对http协议特点不是特别熟悉,都会碰到或多或少令人迷惑的问题,特别是碰到一些复杂特殊的请求时候:例如文件上传,返回特殊的文件格式到浏览器,这时候使用servlet开发就不是很方便了,servlet开发还有个问题可能大家常常被忽视,就是请求的数据的类型转化。

数据的类型转化

http协议传输都是文本形式,到了web容器解析后也是文本类型,如果碰到货币,数字,日期这样的类型需要我们根据实际情况进行转化,如果页面传送的信息非常多,我们就不得不做大量类型转化,这种工作没有什么技术含量,是个体力活而且很容易导致程序错误。同时java的企业开发都是围绕javabean进行,类型转化好的数据还要封装到对应的javabean里,这种转来转去的事情对于项目开发绝对不是什么好事情,

DTO对象(数据传输对象)

所以古老的struts1为这种问题找到了一种解决方案,就是定义了一个DTO对象(数据传输对象),专门负责做这样的事情,不过到了struts2,整个替代servlet的action本身就是一个javabean。

javabean开发

Java的企业开发一个技术特点就是使用javabean进行的,struts2的特点之一就是它替代servlet的操作类就是一个典型的javabean,首先struts2框架将页面传输的数据进行类型转化和封装后将请求信息封装到了这个javabean的属性里,这样我们开发web程序时候就省去了烦心的类型转化和封装的问题,前面我讲到传统的servlet是根据http协议进行定义的,它会按你请求方式(post还是get方式)来处理用户的请求,但是对于一名程序开发人员而言,一个请求,具体到一个url,其实对于服务端而言就是服务端对外提供的一个功能,或者说是服务端对外的一个动作,如果我们使用servlet开发程序我们就得把http的动作转化为具体的业务动作,这就让程序开发变得繁琐,增强了开发的难度,所以struts2替代servlet的javabean就屏蔽了servlet里http的请求方式和具体业务动作转化的问题,javabean里的每一个方法都可以和每一个url请求一一对应,这必然减轻了开发的难度问题。

struts2的处理优势

Servlet另一个作用就是构造response对象,让页面获得正确的响应,其实现代的浏览器是一个多媒体工具,文字,图片,视屏等等东西都可以在浏览器里显示,资源的不同就会导致http响应报文的差别,如果我们使用servlet开发就要根据资源的不同在java程序里用硬编码的形式处理,这样的程序很难复用,而且如果程序员对某种资源的处理理解不到位,就会导致问题的出现,struts2通过配置文件的形式将这样的逻辑从java程序里剥离出来,使用配置的方式进行统一管理,这个做法和spring的AOP方式类似,这样就让结果处理方式更加统一,更加利于管理,同时也提升了程序的健壮性以及降低了开发的难度。

MVC开发模式

Servlet在MVC开发模式里就是其中C层即控制层,控制层就像俄罗斯的双头鹰(一个头向东看一个头向西看)一样,一个头向M层模型层看,一个头向V层视图层看,模型层也是用java编写的,控制层也属于服务端语言开发,所以M层和C层的沟通没有天然的障碍,但是和V层视图层就不一样了,这是一个跨语言的沟通,对于浏览器,它只懂得html,JavaScript和css,浏览器是理解不了java这种语言的东西,但是要让服务端的东西能被浏览器理解接受,我们就必须得把服务端的响应信息放到页面里,

jsp技术的由来

因此就需要一个技术把java的信息转化到html页面里,这就是javaEE规范里提供了jsp技术,jsp其实是一种服务端技术而非客户端技术,不过它看起来似乎更像html技术,最早的jsp开发里都是直接将java代码写到页面里,这种坏处谁都知道,之后javaEE规范提供了自定义标签技术,使用一种类似html标签的方式来解析java代码,struts2框架提供了一整套完整的自定义标签技术,这似乎听起来不算啥,但是它的作用非凡,因为自定义标签之所以叫自定义就是每个人都可以自己来定义,如果没有一个规范必然产生混乱,而且一套完善的自定义标签是个系统工程,一套完整的自定义标签相当于我们在自己定义一套新的开发语言,做程序的人听到这个一定就会明白开发一套完整的自定义标签的工作量和开发难度都是难以想象的,而且自定义标签都是和控制层紧密相连,其难度又会增加一个维度,所以struts2提供的自定义标签对于业务开发带来的将是质的飞越。

Servlet监听器和过滤器

Servlet里还有两个重要的技术:监听器和过滤器,对于监听器在web开发里使用的场景比较少,都是一些十分特别的情况才会使用,大部分web开发里可以忽略它的使用,我们用的最多的监听器可能就是对ServletContext创建和销毁的监听器,ServletContext是整个web应用的全局对象,它和Web应用的生命周期绑定在一起,因此使用这个监听器对Web应用的全局信息进行初始化和销毁操作,例如spring容器的初始化操作。比较有意思的是过滤器,

struts2特有功能拦截器

在struts2里有个拦截器,它们的作用相同都是用来拦截请求的,因为拦截器是struts2的特有功能,在struts2里使用拦截器自然比使用过滤器更顺手,其实拦截器所用的技术比过滤器更加先进,因为拦截器使用了反射技术,因此拦截器拦截的面更大,控制请求的能力更强,它能完成的任务也会更加的丰富多彩。

在我第一次接触struts2是在gxpt系统中,有人告诉我struts设计的一个目的就是想屏蔽在控制层里操作request和response对象,因为这两个http协议的儿子会造成web开发里思路的混乱,但是我在实际开发里却经常不自觉的使用这两个对象。struts2里用到了太多反射机制,特别是使用注解做配置,在java里反射的执行效率是非常低的,直接使用servlet一定能提升web应用的执行效率。其实这个倒很难做到,因为当时我没法在servlet里灵活的运用spring技术。

spring技术

spring技术可以说是java企业开发里最重要的技术,不过真的理解spring的作用和意义还真是一件麻烦的事情,很多人对spring理解其实都是停留在使用阶段(例如:声明式事务很好用等等),当今的spring技术生态环境里可谓是蔚为壮观,spring已经包罗万象,它的内容之多完全不亚于它的本源java语言了,而spring这么大的框都是建立在ioc和aop技术之上,只有深入理解了这两个技术我们才能明白为什么spring这个框能装的下那么多东西了。

ioc 技术控制反转/依赖注入

首先是ioc,ioc技术第一个解释叫做控制反转,它还有个解释就是依赖注入,这两个名字很难从字面理解,但是当你理解它的原理后就会发现它们的描述是何等准确。

Ioc技术的本质就是构建对象的技术

换句话说就是将一个类实例化成对象的技术,在java里实例化类通过new关键字进行的,每次new一个类都会产生一个新的实例对象,这么做视乎很浪费,有时这种浪费还挺危险,因为在程序开发时候我们常常只需要某个类永远只能产生一个的实例对象这个时候就得使用单例模式,此外在设计模式里还可以通过工厂方式产生对象,使用过spring的人看到上面的文字就知道了,

spring里bean的定义就和上面的内容一一对应,

l scope属性single产生单例对象

l prototype产生新对象

l bean还可以通过工厂方式产生对象

可以说spring的bean就是制造对象的工具。面向对象编程里对象相当于显示生活中的一个实体,例如我们有个对象作用是完成打猎的操作,那么打猎这个对象内部包含两个辅助对象:人和枪,只有人和枪赋予了打猎这个对象,那么打猎对象才能完成打猎的操作,但是构建一个人和枪的对象并不是看起来那么简单,这里以枪为例,要创造一把枪我们需要金属,需要机床,需要子弹,而机床和子弹又是两个新对象,这些对象一个个相互嵌套相互关联,大伙试想下如果我们在java代码里构建一个枪的对象那是何其的复杂,假如我们要构造的不是简单的枪对象而是更加复杂的航空母舰,那么构造这个对象的成本之高是让人难以想象的,怎么来消除这种对象相互嵌套相互依赖的关系了?

Spring容器

spring提供了一种方式,这种方式就是spring提供一个容器,我们在xml文件里定义各个对象的依赖关系,由容器完成对象的构建,当我们java代码里需要使用某个实例的时候就可以从容器里获取,那么对象的构建操作就被spring容器接管,所以它被称为控制反转,

控制反转的意思

就是本来属于java程序里构建对象的功能交由容器接管,

依赖注入的意思

就是当程序要使用某个对象时候,容器会把它注入到程序里,这就叫做依赖注入。

使用某个类提供的功能的两种方式

在java开发里我们想使用某个类提供的功能,有两种方式,

l 一种就是构造一个新的类,新的类继承该类

l 另一种方式则是将某个类定义在新类里,那么两个类之间就建立一种关联关系,spring的ioc容器就是实现了这种关联关系(记住不是继承关系哦)

那么某个类要被赋予到新类有哪些办法了?

一般只有两种:一种就是通过构造函数,一种就是通过setXXX方式,这也是spring容器使用到了两种标准的注入方式。

aop的思想

Whether the above said inheritance, or the way they are actually associated with the development of tools to enhance the ability of the target object, there is a proxy mode in design mode, proxy mode will inherit and association modes used together, is the inheritance mode and proxy mode complex patterns of association, but the role of this complex problem is not that the object injection, but to find a nanny or a secretary for the specific operation objects, which heads II and the novel, like the number two heads of foreign representatives specific examples of objects, inlet and outlet instance of an object is through the number two heads, because of the specific instance of an object is the head of One, One heads to a dry event, so some transactional, repetitive tasks such as bubble tea, arranged for a car, take the trouble to do this kind of work is the head of the Dajia One, but II heads to help solve, and this is the idea of ​​aop, aop settlement procedures developed in transactional, and unrelated to the core business problems, but these problems for the realization of the business scene it is necessary, in the actual development in aop also save One way to code.

Spring is the role of core technology is essentially a communication mechanism, spring always do our best for both sides to communicate information flow while reducing the cost of communication between the two sides, in reality institutions a good communicator is certainly the company's leadership, it will leadership and communication can mobilize the enthusiasm of various resources, good communication and leadership will do be tolerant to diversity, so that various people to follow him, so today is a big box spring, anything can be installed inside.

spring action

Spring like banks, it can not directly create material wealth, but all resources should be carried out by circulating it, it can control the direction of economic development, world back to the program,

Spring action is being billed as decoupling between the program, to reduce the degree of coupling spring between the different modules, the reason is that in program development in the communication of information between the different modules is accomplished by passing the object, and the object smooth transfer is to a reasonable construction of a good object, and manage objects are built, will be able to manage object is passed, which is the benefits of spring to bring the system architecture design.

JavaWeb development framework, you have to really know how much?

Guess you like

Origin www.cnblogs.com/1994jinnan/p/12287837.html