使用JavaBeans的地方? [英] Places where JavaBeans are used?

查看:86
本文介绍了使用JavaBeans的地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是JavaBean?为什么需要它?既然我可以使用类和接口结构创建所有应用程序?我为什么需要豆子?你能给我一些例子,其中bean是必不可少的而不是类和接口吗?

What is a JavaBean and why do I need it? Since I can create all apps with the class and interface structure? Why do I need beans? And can you give me some examples where beans are essential instead of classes and interfaces?

请在下面的上下文中解释bean的必要性:

Please explain the essentiality of a bean in the below context:


  • Wep apps

  • 独立应用

推荐答案

它们通常只代表真实世界的数据。以下是Javabean的一个简单示例:

They often just represents real world data. Here's a simple example of a Javabean:

public class User implements java.io.Serializable {

    // Properties.
    private Long id;
    private String name;
    private Date birthdate;

    // Getters.
    public Long getId() { return id; }
    public String getName() { return name; }
    public Date getBirthdate() { return birthdate; }

    // Setters.
    public void setId(Long id) { this.id = id; }
    public void setName(String name) { this.name = name; }
    public void setBirthdate(Date birthdate) { this.birthdate = birthdate; }

    // Important java.lang.Object overrides.
    public boolean equals(Object other) {
        return (other instanceof User) && (id != null) ? id.equals(((User) other).id) : (other == this);
    }
    public int hashCode() {
        return (id != null) ? (getClass().hashCode() + id.hashCode()) : super.hashCode();
    }
    public String toString() {
        return String.format("User[id=%d,name=%s,birthdate=%d]", id, name, birthdate);
    }
}

实施可序列化本身并不是强制性的,但如果您希望能够在Java的内存中保留或传输Javabeans,则非常有用,例如在硬盘或网络上。

Implementing Serializable is not per se mandatory, but very useful if you'd like to be able to persist or transfer Javabeans outside Java's memory, e.g. in harddisk or over network.

例如,在DAO课程中,您可以使用它创建一个用户列表,其中存储 user 数据库中的表:

In for example a DAO class you can use it to create a list of users wherein you store the data of the user table in the database:

List<User> users = new ArrayList<User>();
while (resultSet.next()) {
    User user = new User();
    user.setId(resultSet.getLong("id"));
    user.setName(resultSet.getString("name"));
    user.setBirthdate(resultSet.getDate("birthdate"));
    users.add(user);
}
return users;

例如,在Servlet类中,您可以使用它来传输数据来自数据库到UI:

In for example a Servlet class you can use it to transfer data from the database to the UI:

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    List<User> users = userDAO.list();
    request.setAttribute("users", users);
    request.getRequestDispatcher("users.jsp").forward(request, response);
}

例如,您可以访问的JSP页面它遵循Javabean惯例的 EL 来显示数据:

In for example a JSP page you can access it by EL, which follows the Javabean conventions, to display the data:

<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Birthdate</th>
    </tr>
    <c:forEach items="${users}" var="user">
        <tr>
            <td>${user.id}</td>
            <td><c:out value="${user.name}" /></td>
            <td><fmt:formatDate value="${user.birthdate}" pattern="yyyy-MM-dd" /></td>
        </tr>
    </c:forEach>
</table>

这有意义吗?你知道,这是一种约定,您可以在任何地方使用存储传输访问数据。

Does it make sense? You see, it's kind of a convention which you can use everywhere to store, transfer and access data.

  • JavaBeans specification

这篇关于使用JavaBeans的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆