为什么 EL 函数应该来自具有非 void 返回类型的 Java 方法?
我遇到一种情况,我认为使用作为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
打印
<
可能来自其他地方,或者是 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).
您需要的机制比 EL 更老 - 它被称为 scriptlet:
在早期的 JSP 中,另一种语法用于仅打印值(注意等号):
因为人们认为,当有命令时,维护代码更容易/查询分离,引入EL作为查询模型的一种方式(=读取bean的属性)。由于 scriptlet 可用于满足仅需要“运行一些代码”的罕见情况,因此向 EL 添加此类功能是没有意义的:我们将有两种方法来实现相同的事情,并且我们将打破 EL 的概念表达式始终具有值和类型。
所以:
The mechanism you need is older than EL - and it is called a scriptlet:
In early JSP another syntax was used to just print values (notice the equals sign):
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: