为什么 EL 函数应该来自具有非 void 返回类型的 Java 方法?

发布于 2024-11-27 04:36:06 字数 454 浏览 0 评论 0原文

我遇到一种情况,我认为使用作为 void java 方法的 EL 函数是个好主意。这是一个简单的 jsp,用于更新人们的名字和姓氏数据库。因此,有两个文本字段:名称和密码。用户输入它们并单击“提交”,然后使用 POST 将它们转发到另一个 jsp。这个jsp简单地调用了EL函数,我们称之为“put”,它带有两个参数,并且派生自java方法putPerson(String, String),该方法获取到数据库的连接并放置第一个和姓氏在适当的位置。现在,putPerson 无效,因为它所做的只是将字符串放入数据库中。那么,这是不好的编程习惯吗?如果是,为什么?在我看来,这是一个快速而优雅的解决方案,而且它有效。唯一的问题是你不能只通过说 ${put(first, last)} 来调用它,因为出于某种原因,它会打印出“<”,但这很容易解决。那么为什么他们建议不要使用返回类型为 void 的 EL 函数呢?

I have a situation in which I think it is a good idea to use an EL function that is a void java method. This is a simple jsp to update a database of people's first and last name. So, there are two text fields, name and password. The user enters them and clicks submit, and it forwards these with POST to another jsp. This jsp simply calls the EL function, let's call it "put" that takes two parameters and is derived from a java method putPerson(String, String) that gets a connection to the database and puts the first and last name in their proper places. Now, putPerson is void because all it does is put the strings in the database. So, is this bad programming practice, and if so, why? It seems to me to be a quick and elegant solution, and it works. The only problem is that you can't just call it by saying ${put(first, last)} because that prints out "<", for some reason, but that's easy enough to work around. So why do they recommend to not use EL functions with void return types?

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

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

发布评论

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

评论(2

り繁华旳梦境 2024-12-04 04:36:06

打印 < 可能来自其他地方,或者是 EL 解析器中的错误。但无论哪种方式,您都不应该在 EL 函数中这样做。

EL 函数是实用程序 - 例如 .substring().join() (数组)等。它们在您显示页面和需要一些更复杂的表示逻辑

您想要做的是一个典型的故事,它是在 servlet(或任何其他操作组件,如果您使用框架)中完成的。

Printing < would be coming from somewhere else, or is a bug in the EL parser. But either way, you should not do that in an EL function.

EL functions are meant to be utilities - for example .substring(), .join() (arrays), etc. And they are used while you are displaying the page and need some more complex presentation logic.

What you want to do is a typical story and it is done in a servlet (or any other action component, if you are using a framework).

柒夜笙歌凉 2024-12-04 04:36:06

您需要的机制比 EL 更老 - 它被称为 scriptlet:

<%
    whatever.putPerson("John", "Smith");
%>

在早期的 JSP 中,另一种语法用于仅打印值(注意等号):

<%=
    whatever.getVal();
%>

因为人们认为,当有命令时,维护代码更容易/查询分离,引入EL作为查询模型的一种方式(=读取bean的属性)。由于 scriptlet 可用于满足仅需要“运行一些代码”的罕见情况,因此向 EL 添加此类功能是没有意义的:我们将有两种方法来实现相同的事情,并且我们将打破 EL 的概念表达式始终具有值和类型。

所以:

  • 使用 EL 调用 void 方法就像...呃...使用 import 来运行代码:没有意义;
  • 这不是问题,因为您可以使用调用方法的机制:scriptlet。

The mechanism you need is older than EL - and it is called a scriptlet:

<%
    whatever.putPerson("John", "Smith");
%>

In early JSP another syntax was used to just print values (notice the equals sign):

<%=
    whatever.getVal();
%>

Because it was felt that it is easier to maintain code when there is a command / query separation, EL was introduced as a way to query model (= read properties of beans). Since scriptlets were available to cater for the rare cases when simply "running some code" would be necessary, adding such functionality to EL would have no sense: we would have two ways to achieve the very same thing and we would break the concept of EL expressions always having a value and a type.

So:

  • using EL for calling void methods is like... uhmpf... using import for running code: it makes no sense;
  • this is not a problem, since you can use the very mechanism that was made to call methods: a scriptlet.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文