a4j:jsFunction & a4j:actionparam

这次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> .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"For input string: "A4"的错误是由于尝试将一个包含非数字字符的字符串转换为int类型引起的。根据引用中的描述,你可能正在尝试将一个包含字符'A'的字符串转换为int类型,而这是不合法的。要解决这个问题,你需要确保要转换的字符串只包含数字字符。你可以使用字符串的replaceAll()方法来去除非数字字符,然后再进行转换。以下是一个示例代码: String input = "A4"; String numericInput = input.replaceAll("[^0-9]", ""); int result = Integer.parseInt(numericInput); 在这个示例中,我们使用了replaceAll()方法将字符串中的非数字字符替换为空字符串。然后,我们使用Integer.parseInt()方法将处理后的字符串转换为int类型。这样,就可以避免"For input string: "A4"的错误。" <span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [js使用小技巧](https://download.csdn.net/download/qq_17486363/7619983)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [报错:For input string](https://blog.csdn.net/xieyinlu/article/details/80271346)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值