如何创建 Java 自定义 Web 控件?
这个问题最初是在我的脑海中出现的,“我可以在 Servlet 中使用 AWT 控件吗?”,这将表明我对这个主题的无知。
我对 JAVA 技术很陌生,但经过一些阅读后,我似乎了解 AWT 控件直接连接操作系统 GUI 元素,因此无法在 Servlet 中使用或扩展 JPanel、JButton 等以注入到 JSP 中并让浏览器呈现这些控件(替代方案可能是在 JSP 中嵌入小程序,但我不想这样做)。
我正在寻找一种使用 JSP 和 Servlet 构建自定义可重用 Web 控件的方法。
这通常是如何完成的?您能提供一些示例/链接吗?
编辑:这是我对 Google 应用程序引擎进行的测试运行的一部分 - 因此对我来说探索 Google Web Toolkit 可能是有意义的 - 任何有关该方向的指示也将不胜感激。
任何帮助表示赞赏!
This question was originary in my head as "Can I use AWT controls in a Servlet?", which will show all my ignorance on the subject.
I am new to JAVA technologies but after a bit of reading, I seem to understand AWT controls directly hook up the OS GUI elements so there is no way to use or extend JPanels, JButtons and so forth in a Servlet to be injected in a JSP and let the browser render those controls (an alternative could probably be embedding an applet in a JSP but I don't wanna do that).
I am looking for a way of building custom re-usable web controls using JSPs and Servlets.
How is this usually done and can you provide some samples/links?
EDIT: This is part of a test run I am giving to the Google Application Engine - so it would probably make sense for me to explore the Google Web Toolkit - any pointers in that directions would be appreciated as well.
Any help appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AWT 是特定于操作系统的部分UI 渲染在桌面上,而不是在 JSP、Servlet 等所在的 Web 端。 更具体一点,比如 Swing (其中有 JPanel、JButton)等等您提到的 UI 组件)和 SWT 目前基于 AWT 并在它渲染 UI 并使其按预期工作。
不幸的是,所有这些意味着您不能在网页上使用基于 AWT 的组件,因为网页(通常)与平台无关,因为它们无法准确决定 UI 部分的呈现方式,只有一个一堆标记,被视为一种请求 Web 浏览器做 Web 设计者希望的事情,但不能 100% 保证最终结果是设计者想要的。
为了在 Java 的 Web 端实现 Swing/AWT 类型的 UI 创建,需要进行大量的重新发明轮子,因为它是一个聪明的模型,就像您似乎已经知道的那样 Google Web Toolkit 试图尽其所能,使 Web 看起来更像桌面应用程序,而实际上它只是自动执行底层所需的 JavaScript Ajax 来制作网页其行为就像桌面应用程序一样。 另一个框架是 Tapestry,我个人没有使用过它,但有些人认为它也是一个不错的选择。
当然还有我个人最喜欢的 Apache Wicket,它可以让您真正分离 Java 代码和标记而且它的行为也与 Swing UI 代码非常相似! 事实上,对于最简单的事情来说,Swing 的 UI 组件类存在大量名称冲突。 假设您熟悉编写桌面应用程序 UI 代码,我强烈推荐 Wicket,它抽象出无聊乏味的部分(Servlet、URL 解析、页面可书签性、安全性...),并将它们替换为类似的事件驱动模型(但不等于)到 Swing 的 EDT ,其中桌面 UI 的魔力通常会发生。
虽然这完全偏离了您正在寻找的内容,但使用 Wicket,您可以创建这样一组 POJO Web 组件,您可以在任何地方重用它们,从而获得您所要求的内容。 不过需要警告的是,Wicket 假设您真的知道如何使用 Java 进行编码,一些可笑的简单事情一开始可能会很乏味,但最终您应该对所得到的感到非常满意。
AWT is the OS-specific part of UI rendering on desktop, not on the Web side of things in which JSP, Servlets etc. live. A bit more specifically, things like Swing (which has those JPanels, JButtons and so on you mentioned as UI components) and SWT are currently based on AWT and work on top of it to render the UI and allow it to work as expected.
Unfortunately all this means you can't use AWT based components on Web pages since, well, Web pages are (usually) platform agnostic in the sense that they don't get to decide exactly how parts of the UI are rendered, there's just a pile of markup which is treated as a sort of plea to the Web browser to do things the Web designer hopes for without 100% quarantee that the end result will be what the designer wanted.
There's been a lot of reinventing the wheel to achieve Swing/AWT kind of UI creation on the Java's Web side since it's a clever model, like you seem to already know Google Web Toolkit tries to do its part to make Web seem more like a desktop application while in reality it merely automates the needed JavaScript Ajax underneath to make the web page behave as if it was a desktop application. One another framework for this is Tapestry which I haven't personally used but some think it's a decent choice too.
And then there's of course my personal favorite Apache Wicket which allows you to have a true separation between Java code and markup and it behaves quite similarly to Swing UI code too! In fact there's a whole bunch of name collisions with Swing's UI component classes for the most simple things. Assuming you're any familiar with coding a desktop application UI I strongly recommend Wicket, it abstract away the boring and tedious parts (Servlets, URL resolving, page bookmarkability, security...) and replaces them with an event-driven model similar (but not equal) to Swing's EDT which is where the desktop UI magic would normally happen.
While this is going completely away from what you're looking for, with Wicket you can create such a set of POJO Web components that you can reuse them just about anywhere and thus get what you asked for. A word of warning though, Wicket assumes you really know how to code with Java and some laughably easy things may be tedious at first but in the end you should be quite happy with what you got.
在 JSP 中,您可能正在寻找自定义标签。 自定义标签是创建用于显示 JSP 页面的可重用代码组件的方法。 有一些非常好的框架,例如 struts2 框架中的框架 或显示标签库。
但您可以自己编写或使用新功能扩展现有的。
In JSP you are probably looking for Custom Tags. Custom Tags are ways to create re-usable code components to be used in the display of JSP pages. There are some very nice ones out there such as those found in the struts2 framework or the display tags library.
But you can write your own or extend the existing ones with new functionality.