基于 Java 组件与基于请求的框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1247627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Component based vs Request based frameworks
提问by user150014
I was recently asked in an interview - In java, how do you compare component based frameworks to request based frameworks? I explained EJB as an example of Component based framework and Struts as a request based framework but was not convinced if I did justice to the question.
我最近在一次采访中被问到 - 在 Java 中,你如何比较基于组件的框架和基于请求的框架?我将 EJB 解释为基于组件的框架的示例,将 Struts 解释为基于请求的框架,但我不相信我是否正确地解决了这个问题。
Any ideas on what interviewer meant and what should have been compared??
关于面试官的意思以及应该比较什么的任何想法?
regards, avajurug
问候, avajurug
回答by Nate
They were most likely looking for examples of webframeworks - for example, JSF is a component-based framework, and Struts is a request-based framework.
他们很可能在寻找Web框架的示例——例如,JSF 是一个基于组件的框架,而 Struts 是一个基于请求的框架。
Request-based frameworks generally make it clear through their APIs that they're working with parsing an HTML request / generating an HTML response, while Component-based frameworks attempt to abstract this away and treat the application as collections of components with renderers and actions to do things.
基于请求的框架通常通过它们的 API 明确表示它们正在解析 HTML 请求/生成 HTML 响应,而基于组件的框架试图将其抽象出来并将应用程序视为具有渲染器和操作的组件集合做事。
In my opinion, component-based web frameworks are more trouble than they're worth - their main purpose is usually to make the development of a web app "easier" for developers unfamiliar with web development, and closer to traditional desktop development. However, in practice, when something goes wrong, you need to develop custom components, you need to customize the framework for something that isn't "out of the box" functionality, etc. you need to understand both underlying "traditional" web development andhow the component-based framework abstracts it - and if you're an experienced web developer and have existing solutions, utilities, libraries or snippets that worked in "traditional" web development, you'll waste time re-implementing them to work within the component-based framework.
在我看来,基于组件的 web 框架比它们的价值更麻烦——它们的主要目的通常是让不熟悉 web 开发的开发人员“更容易”开发 web 应用程序,并更接近传统的桌面开发。但是,在实践中,当出现问题时,您需要开发自定义组件,您需要为不是“开箱即用”功能的东西自定义框架等。您需要了解底层的“传统”Web 开发以及基于组件的框架如何对其进行抽象 - 如果您是一位经验丰富的 Web 开发人员并且拥有适用于“传统”Web 开发的现有解决方案、实用程序、库或代码片段,那么您
回答by Joshua Partogi
Request based frameworkis a web framework that gets user's request then determine what the system should do and give back the response back to the user. So the flow is pretty much linear. You're thinking in actions: what does user want (request) -> what user will get back (response). An example of Request based framework is Struts. The modern Grailsis pretty much a Request based framework too.
基于请求的框架是一个 Web 框架,它获取用户的请求,然后确定系统应该做什么并将响应返回给用户。所以流量几乎是线性的。您正在考虑行动:用户想要什么(请求)-> 用户会得到什么(响应)。基于请求的框架的一个例子是Struts。现代Grails也几乎是一个基于请求的框架。
Component based frameworkis not like that. There is actually no clear sense of the flow from front to back. An example of it is not JSF, because in some way JSF is pretty much quite the same with Struts (since the creator of Struts and JSFis the same). A good example of Component based framework Tapestryand Wicket. The paradigm in these two framework is different. You don't think in actions or request-response, but components and components. You define a component in your application, and you tell what the component does. But the flow does not have to be linear as in Request based framework.
基于组件的框架不是那样的。从前到后的流动其实没有清晰的感觉。一个例子不是 JSF,因为在某些方面 JSF 与 Struts 几乎完全相同(因为Struts 和 JSF的创建者是相同的)。基于组件的框架Tapestry和Wicket 的一个很好的例子。这两个框架中的范式是不同的。您不考虑操作或请求响应,而是考虑组件和组件。您在应用程序中定义一个组件,然后告诉该组件做什么。但是流程不必像基于请求的框架那样是线性的。
回答by javance
JSF is component based java API, as said Nate, Struts is an action based framework, the Http requests are parsed, while processing the actions in the end the controler Servlet forwards the request to the JSP that will be responsible to generate the response. While in JSF which became a standard for component based web frameworks, there is actually no need to process the requests and the responses as all we need is to write JSP or XHTML pages, bind the components used in the page or the value they should render to properties in backing beans or managed beans and the FacesServlet (controller) does all the job to care about request parsing and redirecting to the JSP that will render the response based on navigation rules specified in faces-config.xml . So as you can see there is a big difference between Struts and JSF as JSF brings a component-event based approach while Struts is more close to the classic JSP/Servlet model. Another thing jpartogi have said the creator of JSF and struts is the same, I just want to mention that Struts is a framwork owned by Apache community while JSF is an API specified by JCP in the JSR-127 for the version 1.1 and JSR-252 for the version 1.2 and has different implementations (SUN-RI, Apache MyFaces...)
JSF 是基于组件的 java API,正如 Nate 所说,Struts 是一个基于动作的框架,解析 Http 请求,同时处理动作最终控制器 Servlet 将请求转发给 JSP,后者将负责生成响应。而在成为基于组件的 Web 框架标准的 JSF 中,实际上不需要处理请求和响应,因为我们只需要编写 JSP 或 XHTML 页面,绑定页面中使用的组件或它们应该呈现的值到支持 bean 或托管 bean 中的属性,并且 FacesServlet(控制器)完成所有工作来关心请求解析和重定向到 JSP,该 JSP 将根据在 faces-config.xml 中指定的导航规则呈现响应。因此,正如您所看到的,Struts 和 JSF 之间存在很大差异,因为 JSF 带来了基于组件事件的方法,而 Struts 更接近经典的 JSP/Servlet 模型。另一件事jpartogi说JSF和struts的创造者是一样的,我只想说Struts是Apache社区拥有的框架,而JSF是JCP在JSR-127中为1.1和JSR-252版本指定的API对于 1.2 版并具有不同的实现(SUN-RI、Apache MyFaces...)
回答by FURKAN ILGIN
Simply, If the framework has objects in java side for each web element so you can change attributes of it or add some functionality, it is component based framework but If framework does not provide objects and you have to put the response value of web element between its tags it is a request based framework.
简单地说,如果框架在每个 web 元素的 java 端都有对象,所以你可以改变它的属性或添加一些功能,它是基于组件的框架,但是如果框架不提供对象,你必须把 web 元素的响应值放在它的标签是一个基于请求的框架。