在 JSP/Java 中,如何用 s.th. 替换 getter/setter?更通用?

发布于 2024-10-26 23:53:22 字数 1143 浏览 2 评论 0原文

我厌倦了一直在我的豆子中添加大量的吸气剂/吸气剂。 有没有一种简单的方法可以使用注释来摆脱这种愚蠢的工作? 或者任何其他方式?第二个示例是简短版本,我想运行它,因为不需要封装我的成员(尽管在另一个上下文中可能是必要的)。 在我的现实世界中,我必须访问大约 15 个类,每个类中有大约 10 个数据成员,这将产生 300 个无用的 setter/getter。

示例 TestPerson.java(有效):

public class TestPerson {
  public String firstName;
  public String lastName;
  public TestPerson() {
    firstName = "Hugo";
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}

示例 TestPerson.java(无效):

public class TestPerson {
  public String firstName;
  public String lastName;
  public TestPerson() {
    firstName = "Hugo";
  }
}

示例 test.jsp

<jsp:useBean id="testperson" class="test.TestPerson" scope="request" />
<html>
<head><title>Test</title></head>
<body>
<h2>Results</h2>${testperson.firstName}<br>
</body>
</html>

I am getting tired by adding tons of getters/setters all the time in my beans.
Is there a simple way to use annotations to get rid of this stupid work?
or any other way? The 2nd example is the short version, which I would like to run, since there is no need to encapsulte my members (though in another context it might be neccessary).
In my real world I have to access about 15 classes with about 10 data members in each class which would produce 300 useless setters/getters.

Example TestPerson.java (works):

public class TestPerson {
  public String firstName;
  public String lastName;
  public TestPerson() {
    firstName = "Hugo";
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}

Example TestPerson.java (does NOT work):

public class TestPerson {
  public String firstName;
  public String lastName;
  public TestPerson() {
    firstName = "Hugo";
  }
}

Example test.jsp

<jsp:useBean id="testperson" class="test.TestPerson" scope="request" />
<html>
<head><title>Test</title></head>
<body>
<h2>Results</h2>${testperson.firstName}<br>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

﹉夏雨初晴づ 2024-11-02 23:53:22

Project Lombok 解决了这个问题(以及更多),并且它支持 EclipseNetbeans

Project Lombok solves this (and much more), and it has support for both Eclipse and Netbeans.

风轻花落早 2024-11-02 23:53:22

只需让您的 IDE 自动生成它们即可。例如,在 Eclipse 中,定义一些属性,右键单击源,选择,然后生成 Getters 和 Setters

在此处输入图像描述

Just have your IDE to autogenerate them. In Eclipse for example, define some properties, rightclick source, choose Source and then Generate Getters and Setters.

enter image description here

烙印 2024-11-02 23:53:22

您是否考虑过使用 Groovy 脚本语言?它基于Java并生成Java字节码。

请参阅此链接:http://groovy.codehaus.org/Groovy+Beans getter 和设置器是隐式的。

Have you considered using the Groovy scripting language? It is based on Java and generates Java Bytecode.

See this link: http://groovy.codehaus.org/Groovy+Beans the getters and the setters are implicit.

ˇ宁静的妩媚 2024-11-02 23:53:22

还有 scala:

class foo {
   @BeanProperty
   var s1 : String,
   @BeanProperty
   var i1 : int
}

您可以使用 scala 编译器生成 java 字节代码,这将创建此类以及适当的私有字段以及 getter 和 setter 方法。

但是,假设您想坚持使用普通 java,我怀疑 java 很快就会看到一流的属性支持。最好的选择是某种代码生成。可以通过注释和注释处理程序生成这些,尽管这可能会给 Eclipse 带来影响。编辑:这就是 Lombok (如上所述)似乎正在做的事情。它在编译时根据注释为您生成源代码。

There's also scala as well:

class foo {
   @BeanProperty
   var s1 : String,
   @BeanProperty
   var i1 : int
}

You could use the scala compiler to generate java byte code, which would create this class, along with the appropriate private fields, and getter and setter methods.

However, assuming you want to stick with plain java, i doubt java will see first class property support any time soon. Your best bet is some kind of code generation. It would be possible to generate these via annotations and annotation handlers, though this might give eclipse fits. EDIT: This is what Lombok (as mentioned above) appears to be doing. It's generating source code for you at compile time based on the annotations.

挽梦忆笙歌 2024-11-02 23:53:22

尝试write-it-once。基于模板的代码生成器。您使用 Groovy 编写自定义模板,并根据 java 反射生成文件。这是生成任何文件的最简单方法。你可以通过生成AspectJ或java文件、基于JPA注释的SQL、基于枚举的插入/更新等来制作getters/settest/toString。

Try write-it-once. Template based code generator. You write custom template using Groovy, and generate file depending on java reflections. It's the simplest way to generate any file. You can make getters/settest/toString by generating AspectJ or java files, SQL based on JPA annotations, inserts / updates based on enums and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文