Sending parameter to a Controller

一世执手 提交于 2019-12-10 17:48:01

问题


I got this:

<a4j:commandLink action="#{searchBean.someMethod}" reRender="search"><span><h:graphicImage value="/home/img/icons/red.gif" width="12" height="12" /> Street</span></a4j:commandLink>

And on my Bean, I got a method:

public void someMethod(String string){
  doStruff();
}

Is it possible to send a String as parameter to my method?


回答1:


You can send param with <f:param> like this

<a4j:commandLink action="#{searchBean.someMethod}" reRender="search">
     <span>
         <h:graphicImage value="/home/img/icons/red.gif" width="12" height="12" />Street
     </span>
   <f:param name="stringParam" value="someString" /> 
</a4j:commandLink>

and then get it in you method using ActionEvent

public void someMethod(ActionEvent actionEvent) {
    String s = (String) actionEvent.getComponent().getAttributes().get("stringParam");
}



回答2:


You can also do it with a4j:actionparam.

<a4j:commandLink>
   <a4j:actionparam name="p1" value="hello"/>
</a4j:commandLink>

in the bean you only need a getter/setter, you don't need to retrieve the param yourself. a4j:actionparam does the assignment automatically.



来源:https://stackoverflow.com/questions/5109980/sending-parameter-to-a-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!