a4j:jsFunction & a4j:actionparam

本文介绍了一种在JSF项目中通过CommandLink激活ManageBean功能并执行JavaScript代码的方法。利用a4j:jsFunction组件,可以在完成服务器端操作后执行客户端脚本。文章详细解释了如何配置这一流程,包括定义无实际作用的JavaScript函数以确保oncomplete事件正确触发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这次JSF的项目中遇到一种特殊情况,在一个CommandLink的动作中先激活Manage Bean中的功能代码段,然后再执行一段脚本代码,查看了RichFaces的手册后决定使用a4j:jsFunction来实现,实现过程如下:

首先需要在页面中声明两个脚本函数:

......

<script type="text/javascript">
? function showContent(){
? ?alert("aaa"); ??
? }


? function [color=red]callScript[/color](){
? }
</script>

......


其中showContent是真正想执行的脚本代码,callScript没有实际用途,只是用来激活a4j:jsFunction功能,

CommandLink调用脚本callScript自动激活a4j:jsFunction:

......

<h:commandLink onmouseup="[color=red]callScript[/color]()"
? value="Test"/>


......

接下来就该添加a4j:jsFunction标签:

......

<a4j:jsFunction name="[color=red]callScript[/color]" action="#{testBean.unzipMmsZipFile}"
? oncomplete="showContent()" />

......

其中testBean为后台Manage Bean的实例,红色标注的部分必须相同,要不无法激活a4j:jsFunction部分的功能,有些人可能会想既然callScript脚本没有实际意义,为什么还要添加这段无用代码,直接把CommandLink和a4j:jsFunction中的callScript换成showContent不就行了,这样照样可以激活a4j:jsFunction的功能,如果只是为了让a4j:jsFunction触发Manage Bean中的某个事件的话这样确实可行,但是在本例中会出现问题,导致a4j:jsFunction中的oncomplete不能正常执行,所以在这里声明了一个无用的脚本函数,还有一点需要注意,在本例中最好使用action来激活Manage Bean中的功能代码,而不要使用actionListener,在我的测试中,利用actionListener能够正常激活Bean中的功能代码,但是有时会出现a4j:jsFunction中的oncomplete无法正常执行的情况,所有还是建议使用action

转自:http://dev.firnow.com/course/1_web/javascript/jsjs/2008828/138149.html

**********************************************************************************
http://docs.jboss.org/richfaces/latest_3_3_X/en/devguide/html/a4j_actionparam.html
**********************************************************************************
6.1.2.2. Details of Usage

The <a4j:actionparam> component has 3 main attributes:

*

"name" defines a name of this parameter
*

"value" defines initial value of this parameter or a value binding
*

"assignTo" defines updatable bean property. This property will be updated if the parent command component performs an actionEvent. The update occurs in the same phase as the actionEvent is processed (INVOKE_APPLICATION, PROCESS_VALIDATIONS, or APPLY_REQUEST_VALUES, depending on the values of the "immediate" and "bypassUpdates" attributes).

[b]Example:[/b]


...

<h:form id="form">

<a4j:commandButton value="Set Name to Alex" reRender="rep">

<a4j:actionparam name="username" value="Alex" assignTo="#{actionparamBean.name}"/>

</a4j:commandButton>

<br/>

<h:outputText id="rep" value="Name: #{actionparamBean.name}"/>

</h:form>

...


There is a managed bean:

...

public class ActionparamBean {

private String name = "John";


public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}

}

...


You can use <a4j:actionparam> not only with Ajax components, but with non-ajax command component also. This feature allows to update model values without invoking even a single line of Java code on the server side. The usage of this feature you can find at ActionParameter Usage page of RichFaces LiveDemo.

If you need to convert the value before the "Update Model" phase you can specify the converter in the "converter" attribute.

[b]Note:[/b]

The property is assigned with a parameter value on the "Update Model" phase. Therefore if the validation of the form is failed, this phase will be skipped and the property won't be updated.

It is possible to use JavaScript expression or function in the "value" attribute. In this case the "noEscape" attribute should be set to "true". The result of this JavaScript invocation is sent to the server as a value of <a4j:actionparam> .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值