我正在做一些JSP教程,我不明白bean类的意义。它只是获取和设置方法。我们为什么要使用它们?
I've bean doing some JSP tutorials and I don't understand what the point of a bean class is. All it is, is get and set methods. why do we use them?
public class UserData { String username; String email; int age; public void setUsername( String value ) { username = value; } public void setEmail( String value ) { email = value; } public void setAge( int value ) { age = value; } public String getUsername() { return username; } public String getEmail() { return email; } public int getAge() { return age; }}
和jsp是
<jsp:useBean id="user" class="user.UserData" scope="session"/> <HTML> <BODY> You entered<BR> Name: <%= user.getUsername() %><BR> Email: <%= user.getEmail() %><BR> Age: <%= user.getAge() %><BR> </BODY> </HTML>推荐答案
A Bean获得了Java一次编写,随处运行范例的所有好处。 可以控制暴露给应用程序构建器工具的Bean的属性,事件和方法。
A Bean obtains all the benefits of Java's "write-once, run-anywhere" paradigm. The properties, events, and methods of a Bean that are exposed to an application builder tool can be controlled.
Bean可能被设计为在不同的语言环境中正常运行,这使得它在全球市场中有用。
A Bean may be designed to operate correctly in different locales, which makes it useful in global markets.
可以提供辅助软件来帮助人配置Bean。只有在设置该组件的设计时参数时,才需要此软件。 不需要包含在运行时环境中。 Bean的配置设置可以保存在持久存储中,并在以后恢复。
Auxiliary software can be provided to help a person configure a Bean. This software is only needed when the design-time parameters for that component are being set. It does not need to be included in the run-time environment. The configuration settings of a Bean can be saved in persistent storage and restored at a later time.
Bean可以注册以接收来自其他对象的事件,并可以生成将发送给其他对象的事件。
A Bean may register to receive events from other objects and can generate events that are sent to other objects.
- Bean的优点
使用 scriptlet (那些< %%> 的东西)确实非常气馁,因为taglibs诞生了(比如 JSTL )和EL(表达式语言,那些 $ {} 的东西)十多年前。 scriptlet 的主要缺点是:
The use of scriptlets (those <% %> things) is indeed highly discouraged since the birth of taglibs (like JSTL) and EL (Expression Language, those ${} things) over a decade ago. The major disadvantages of scriptlets are:
可重用性:您无法重用scriptlet。
Reusability: you can't reuse scriptlets.
可替换性:您不能将scriptlet抽象化。
Replaceability: you can't make scriptlets abstract.
OO-ability:你不能使用继承/组合。
OO-ability: you can't make use of inheritance/composition.
可调试性:如果scriptlet中途抛出异常,你得到的只是一个空白页面。
Debuggability: if scriptlet throws an exception halfway, all you get is a blank page.
可测试性:scriptlet不是单元可测试的。
Testability: scriptlets are not unit-testable.
可维护性:每个saldo需要更多时间来维护混杂/混乱/重复的代码逻辑。
Maintainability: per saldo more time is needed to maintain mingled/cluttered/duplicated code logic.
- 检查整个... BalusC的回答
更多推荐
豆子有什么意义?
发布评论