这次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).
Example:
...
<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.
Note:
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> .