- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
action
有什么区别和 actionListener
,我应该什么时候使用 action
与 actionListener
?
最佳答案
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
的方法。争论。
<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 )。返回值
null
或
void
将让它返回到同一页面并保持当前 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 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
到处都是。这确实是错误的。不要以此为借口,自己也这样做。
listener
无关
<f:ajax>
的属性或不。如需解释和示例,请前往
Exception handling in JSF ajax requests .
关于jsf - action 和 actionListener 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3909267/
我试图了解 JSF 实现如何识别用户的各种可能操作。在我放在一起的简单应用程序中,我在 login.xhtml 页面中配置了以下字段。 用户名 - 输入字段 密码 - 密码字段 登录按钮 取消按钮 登
我已经开始学习 JSF,我想知道在我们的类路径中包含什么 JAR 以开始使用 JSF。是jsf-api或 jsf-impl ?或者我们必须同时包含两者?如果两者都是,那么为什么它们不合并? 最佳答案
我是 java server faces (JSF) 的初学者,我需要将文本输入的内容传递到第二页以显示它,同样适用于第二页:我想将单选按钮值传递到第三页。我搜索并尝试了很多但没有成功。例如我试过
我有一个 JSF 页面。我的 CommandButton 操作方法值取决于 bean 变量值。 例子: Bean headerBean 具有可变的 actionValue,值为“someBean.do
我有两个 JSF 页面,假设 A 和 B。从这两个页面 A 和 BI 可以导航到页面 C。现在页面 C 中有一个按钮(确定按钮),单击它应该导航回 A 或 B,具体取决于从哪里(A 或 B)调用 C
我可以在没有 JSTL 标签的情况下使用 JSF 执行条件逻辑吗? 例如,我制作了一个复合组件,并想说明,如果指定了“id”属性,则定义“id”属性,但如果未指定“id”属性,则不要指定“id”属性。
我有一个应用程序,用户可以在其中从我的应用程序的欢迎页面动态切换语言环境。我看到早期的开发人员(在没有太多文档的情况下继承了代码)已经从 ViewHandler 覆盖了以下三个方法,并告诉我这是动态切
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
有没有一种方法可以在我的 JSF 2.0 应用程序中处理浏览器刷新事件,以便在浏览器刷新页面时将用户导航到欢迎页面? 这让我想到了另一个问题,即如何在托管 bean 中进行页面导航? 干杯, 最佳答案
我有两页。搜索页面是第一个接受用户输入的页面。第二页显示数据表中的结果集。第二页有 3 个面板,用于结果集、更新和在单个页面中创建。根据单击的按钮,我将面板呈现为真和假。 . . . . . .
由于我们在 Asp.Net 中有 comparevalidator,我们在 JSF 中有什么来验证两个字段的值是否相同?我想验证密码和确认密码字段。 最佳答案 不,这样的验证器在基本的 JSF 实现中
我想构建一个自定义 JSF 组件。现在我从 oracle 阅读了一些文档并看到了一些代码示例。问题是我有点困惑: 似乎有两种方法可以使用 JSF 2.0+ 构建自定义组件。据我了解,自 JSF 2.0
我遇到了与 user1598186 在他的问题中提到的相同的问题:p:commandButton doesn't call bean's method in an page 但是,没有给出解决方案(
这个问题在这里已经有了答案: Ajax update/render does not work on a component which has rendered attribute (1 个回答)
是否有内置机制可以有条件地重定向到另一个 View ?如果他/她已经登录,我希望用户从登录页面重定向到“主页”。 我已经有两种基本方法,但对于第一种我不知道如何实现,第二种是一种肮脏的解决方法。 添加
如何在 JSF 中格式化值 我需要格式化一个数字,如:12345.67 到 12,345.67 可以用模式吗? 最佳答案 尝试使用: 关于jsf - 用逗号格式化为数字 JSF,我们在Sta
根据this blog JSF 正在走向无状态。使用 JSF 的全部意义不在于它使保存和恢复状态成为一件苦差事。 JSF 成为无状态的有什么意义?您能否提供一个有用的示例。 最佳答案 首先,我想澄清
我读到某个地方(不再找到它),可以在资源包中使用EL Expresions,然后在不更改* .xhtml文件的情况下使用它。 some.text=#{someBean.stepsLeft} more
我想看一个简单的登录应用程序,不像this那么简单尽管。 我想要实现的是对 JSF 的工作原理的理解,我开发了很多 ASP.NET,您可以在其中隐藏代码,并且您可以在其中检查是否在登录时创建了 ses
如果#{myBean.birthdate}是java.util.Calendar或java.util.Date类型,我可以格式化吗this 在 EL 本身内部可能使用现有函数,其输出类似于 DateF
我是一名优秀的程序员,十分优秀!