gpt4 book ai didi

jsf - action 和 actionListener 的区别

转载 作者:行者123 更新时间:2023-12-03 03:57:00 26 4
gpt4 key购买 nike

action有什么区别和 actionListener ,我应该什么时候使用 actionactionListener ?

最佳答案

Action 监听器
使用 actionListener如果你想要一个 Hook 之前 真正的商业行动得到执行,例如记录它,和/或设置附加属性(通过 <f:setPropertyActionListener> ),和/或访问调用操作的组件(可通过 ActionEvent 参数获得)。因此,纯粹是为了在调用实际业务操作之前进行准备。actionListener方法默认具有以下签名:

import javax.faces.event.ActionEvent;
// ...

public void actionListener(ActionEvent event) {
// ...
}
它应该声明如下,没有任何方法括号:
<h:commandXxx ... actionListener="#{bean.actionListener}" />
请注意,您不能通过 EL 2.2 传递其他参数。但是,您可以覆盖 ActionEvent参数完全通过传递和指定自定义参数。以下示例有效:
<h:commandXxx ... actionListener="#{bean.methodWithoutArguments()}" />
<h:commandXxx ... actionListener="#{bean.methodWithOneArgument(arg1)}" />
<h:commandXxx ... actionListener="#{bean.methodWithTwoArguments(arg1, arg2)}" />
public void methodWithoutArguments() {}
public void methodWithOneArgument(Object arg1) {}
public void methodWithTwoArguments(Object arg1, Object arg2) {}
请注意无参数方法表达式中括号的重要性。如果它们不存在,JSF 仍然会期望一个带有 ActionEvent 的方法。争论。
如果您使用的是 EL 2.2+,那么您可以通过 <f:actionListener binding> 声明多个 Action 监听器方法.
<h:commandXxx ... actionListener="#{bean.actionListener1}">
<f:actionListener binding="#{bean.actionListener2()}" />
<f:actionListener binding="#{bean.actionListener3()}" />
</h:commandXxx>
public void actionListener1(ActionEvent event) {}
public void actionListener2() {}
public void actionListener3() {}
请注意 binding 中括号的重要性属性。如果它们不存在,EL 会令人困惑地抛出 javax.el.PropertyNotFoundException: Property 'actionListener1' not found on type com.example.Bean ,因为 binding属性默认解释为值表达式,而不是方法表达式。添加 EL 2.2+ 样式的括号可以透明地将值表达式转换为方法表达式。另见 a.o. Why am I able to bind <f:actionListener> to an arbitrary method if it's not supported by JSF?

行动
使用 action如果您想执行业务操作并在必要时处理导航。 action方法可以(因此,不是必须)返回 String这将用作导航案例结果(目标 View )。返回值 nullvoid将让它返回到同一页面并保持当前 View 范围活着。空字符串或相同 View ID 的返回值也将返回到同一页面,但会重新创建 View 范围,从而销毁任何当前事件的 View 范围 bean,如果适用,则重新创建它们。 action方法可以是任何有效的 MethodExpression ,还有使用 EL 2.2 参数的参数,如下所示:
<h:commandXxx value="submit" action="#{bean.edit(item)}" />
用这种方法:
public void edit(Item item) {
// ...
}
请注意,当您的操作方法仅返回一个字符串时,您也可以在 action 中准确指定该字符串。属性。因此,这是完全笨拙的:
<h:commandLink value="Go to next page" action="#{bean.goToNextpage}" />
使用这种无意义的方法返回一个硬编码的字符串:
public String goToNextpage() {
return "nextpage";
}
相反,只需将该硬编码字符串直接放在属性中:
<h:commandLink value="Go to next page" action="nextpage" />
请注意,这反过来表明一个糟糕的设计:通过 POST 导航。这对用户和 SEO 都不友好。这一切都在 When should I use h:outputLink instead of h:commandLink? 中进行了解释并且应该被解决为
<h:link value="Go to next page" outcome="nextpage" />
另见 How to navigate in JSF? How to make URL reflect current page (and not previous one) .

f:ajax 监听器
从 JSF 2.x 开始,还有第三种方式, <f:ajax listener> .
<h:commandXxx ...>
<f:ajax listener="#{bean.ajaxListener}" />
</h:commandXxx>
ajaxListener方法默认具有以下签名:
import javax.faces.event.AjaxBehaviorEvent;
// ...

public void ajaxListener(AjaxBehaviorEvent event) {
// ...
}
在莫哈拉, AjaxBehaviorEvent参数是可选的,下面同样有效。
public void ajaxListener() {
// ...
}
但是在 MyFaces 中,它会抛出 MethodNotFoundException .当您想省略参数时,以下两种 JSF 实现都适用。
<h:commandXxx ...>
<f:ajax execute="@form" listener="#{bean.ajaxListener()}" render="@form" />
</h:commandXxx>
Ajax 监听器在命令组件上并不是很有用。它们在输入和选择组件上更有用 <h:inputXxx>/ <h:selectXxx> .在命令组件中,只需坚持 action和/或 actionListener为了清晰和更好的自我记录代码。此外,喜欢 actionListener , f:ajax listener不支持返回导航结果。
<h:commandXxx ... action="#{bean.action}">
<f:ajax execute="@form" render="@form" />
</h:commandXxx>
关于 execute 的解释和 render属性,前往 Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes .

调用顺序 actionListener s 总是在 action 之前被调用与它们在 View 中声明并附加到组件的顺序相同。 f:ajax listener总是在任何 Action 监听器之前调用。所以,下面的例子:
<h:commandButton value="submit" actionListener="#{bean.actionListener}" action="#{bean.action}">
<f:actionListener type="com.example.ActionListenerType" />
<f:actionListener binding="#{bean.actionListenerBinding()}" />
<f:setPropertyActionListener target="#{bean.property}" value="some" />
<f:ajax listener="#{bean.ajaxListener}" />
</h:commandButton>
将按以下顺序调用方法:
  • Bean#ajaxListener()
  • Bean#actionListener()
  • ActionListenerType#processAction()
  • Bean#actionListenerBinding()
  • Bean#setProperty()
  • Bean#action()

  • 异常处理 actionListener支持特殊异常: AbortProcessingException .如果此异常是从 actionListener 抛出的方法,然后 JSF 将跳过任何剩余的 Action 监听器和 Action 方法并直接继续呈现响应。您不会看到错误/异常页面,但是 JSF 会记录它。每当从 actionListener 抛出任何其他异常时,这也将隐式完成。 .因此,如果您打算由于业务异常而导致错误页面阻止该页面,那么您绝对应该在 action 中执行该作业。方法。
    如果使用 actionListener 的唯一原因是要有 void方法返回到同一页面,那么这是一个糟糕的方法。 action方法也可以完美返回 void ,与某些 IDE 通过 EL 验证让您相信的相反。请注意 PrimeFaces showcase此类例子随处可见 actionListener到处都是。这确实是错误的。不要以此为借口,自己也这样做。
    然而,在 ajax 请求中,需要一个特殊的异常处理程序。这与您是否使用 listener 无关 <f:ajax> 的属性或不。如需解释和示例,请前往 Exception handling in JSF ajax requests .

    关于jsf - action 和 actionListener 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3909267/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com