Java EE 6:JSF 与 Servlet + JSP。我应该费心学习JSF吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2701094/
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 EE 6: JSF vs Servlet + JSP. Should I bother learning JSF?
提问by Thang Pham
I am trying to get familiar with Java EE 6 by reading http://java.sun.com/javaee/6/docs/tutorial/doc/gexaf.html. I am a bit confused about the use of JSF.
Usually, the way I develop my Web App would be, Servlet would act like a controller and JSP would act like a View in an MVC model. So Does JSF try to replace this structure? Below are the quote from the above tutorial:
我试图通过阅读http://java.sun.com/javaee/6/docs/tutorial/doc/gexaf.html来熟悉 Java EE 6 。我对JSF的使用有点困惑。
通常,我开发 Web 应用程序的方式是,Servlet 充当控制器,而 JSP 充当 MVC 模型中的视图。那么 JSF 是否尝试替换这种结构?以下是上述教程的引用:
Servlet are best suited for service-oriented App and control function of presentation-oriented App like dispatching request
JSF and Facelet are more appropriated for generating mark-up like XHTML, and generally used for presentation-oriented App
Servlet 最适合面向服务的 App 和面向展示的 App 的控制功能,如调度请求
JSF 和 Facelet 更适合生成像 XHTML 的标记,一般用于面向展示的 App
Not sure if I understand the above quote too well, they did not explain too well what is service-oriented vs presentation-oriented.
不确定我是否太理解上面的引用,他们没有很好地解释什么是面向服务和面向演示。
A JavaServer Faces application can map HTTP requests to component-specific event handling and manage components as stateful objects on the server.
JavaServer Faces 应用程序可以将 HTTP 请求映射到特定于组件的事件处理,并将组件作为服务器上的有状态对象进行管理。
Any knowledgeable Java developer out there can give me a quick overview about JSF, JSP and Servlet? Do I integrate them all, or do I use them separated base on the App? if so then what kind of app use JSF in contrast with Servlet and JSP
任何有知识的 Java 开发人员都可以给我一个关于 JSF、JSP 和 Servlet 的快速概述吗?我是将它们全部集成,还是在应用程序的基础上单独使用它们?如果是这样,那么与 Servlet 和 JSP 相比,什么样的应用程序使用 JSF
A JavaServer Faces application can map HTTP requests to component-specific event handling and manage components as stateful objects on the server.
JavaServer Faces 应用程序可以将 HTTP 请求映射到特定于组件的事件处理,并将组件作为服务器上的有状态对象进行管理。
Sound like what servlet can do, but not sure about manage components as stateful objects on the server
. Not even sure what that mean? Thanks in advance.
听起来像 servlet 可以做什么,但不确定manage components as stateful objects on the server
. 甚至不确定那是什么意思?提前致谢。
采纳答案by BalusC
JSF basically enables you to develop a web application with only model objects (JavaBeans) and views (JSP/XHTML pages). With "plain vanilla" JSP/Servlet you'll have to bring in a lot of code to control, preprocess, postprocess, gather data, validate, convert, listen, etc the HTTP request and response. And then I'm not talking about refactoring it to a high (abstract) degree so that you can also end up the same way as JSF does (just a JavaBean class and a JSP/XHTML page per use case).
JSF 基本上使您能够开发仅包含模型对象 (JavaBean) 和视图(JSP/XHTML 页面)的 Web 应用程序。使用“普通”JSP/Servlet,您必须引入大量代码来控制、预处理、后处理、收集数据、验证、转换、侦听等 HTTP 请求和响应。然后我不是在谈论将它重构到高(抽象)程度,以便您也可以像 JSF 一样结束(每个用例只是一个 JavaBean 类和一个 JSP/XHTML 页面)。
I've posted a more detailed answer on the subject before here: What is the difference between JSF, Servlet and JSP?
我之前在这里发布了关于这个主题的更详细的答案:JSF、Servlet 和 JSP 之间有什么区别?
回答by Thorbj?rn Ravn Andersen
JSF provide an abstraction layer with several responsibilities, but most important it handles all the messy details of HTML forms and transferring data back and forth between web pages and Java POJO beans (getX, setX methods), which is notoriously difficult to do right.
JSF 提供了一个具有多项职责的抽象层,但最重要的是它处理 HTML 表单的所有杂乱细节,并在网页和 Java POJO bean(getX、setX 方法)之间来回传输数据,众所周知,这很难做到正确。
It also provides navigation and in the latest incarnation in Java EE 6 rudimentary AJAX support is available allowing for simple updates of the web page as the user inputs data.
它还提供导航,并且在 Java EE 6 的最新版本中,基本的 AJAX 支持允许在用户输入数据时对网页进行简单的更新。
You might find it easier to think of it as a way to avoid writing JavaScript yourself.
您可能会发现将其视为一种避免自己编写 JavaScript 的方法更容易。
回答by Bozho
In JSF uses one specific Servlet (the Faces Servlet) to handle all incoming requests and dispatch them to the appropriate beans.
在 JSF 中,使用一个特定的 Servlet(Faces Servlet)来处理所有传入的请求并将它们分派到适当的 bean。
JSF is a component-based MVC framework, while JSP is a view technology.
You canuse JSP with JSF, although Facelets is the preferred view technology.
JSF 是基于组件的MVC 框架,而JSP 是一种视图技术。
您可以将 JSP 与 JSF 一起使用,尽管 Facelets 是首选的视图技术。
回答by ring bearer
JSF Framework targets to simplify development integration of web-based user interfaces. As @bozho stated you can mix JSP and JSF. However, the "view" component in JSF is facelets - which can be viewed as little UI widgets, which are more or less self contained with respect to DHTML styling and JavaScript event generation and call back.
JSF 框架旨在简化基于 Web 的用户界面的开发集成。正如@bozho 所说,您可以混合使用 JSP 和 JSF。然而,JSF 中的“视图”组件是 facelets - 可以将其视为小的 UI 小部件,就 DHTML 样式和 JavaScript 事件生成和回调而言,它们或多或少是自包含的。
"Should I bother learning.. ?"
“我应该费心学习吗..?”
Not sure. I haven't seen JSF picking up that much steam even though it was around (Atleast in theory) for last 5 years.
没有把握。尽管 JSF 在过去 5 年里(至少在理论上)已经存在,但我还没有看到它获得如此大的动力。
回答by Chris
If you like XML choose JSF. In case that you are an actionlistener fan doPost,doGet etc choose Servlet and JSP.
如果您喜欢 XML,请选择 JSF。如果您是动作监听器的粉丝 doPost、doGet 等,请选择 Servlet 和 JSP。