I want to pass parameter node to the function doAction which will be invoked by the remote command, how can I pass it? Suppose node has attributes name and type and I want to use them in doAction, how can I pass the variables?
Thanks!
public static class node {
String name;
String type;
//setters getters etc...
}
<p:remoteCommand name="doWhatYouWant" action="#{managedBean.doAction}" />
<p:treeTable value="#{managedBean.tree}" var="node">
<p:column>
<p:commandLink value="Invoke action" onclick="doWhatYouWant([params...])" />
</p:column>
</p:tree>
public void doAction() {
// do something with var="node"
}
I solved this problem with
<p:remoteCommand name="doWhatYouWant" action="#{managedBean.doAction}" />
...
<p:commandLink value="Invoke action"
onclick="doWhatYouWant(
[{name:'n', value:'#{node.name}'},
{name:'t', value:'#{node.type}'}])" />
In the supporting bean:
public void doAction() {
FacesContext context = FacesContext.getCurrentInstance();
Map<String, String> map = context.getExternalContext().getRequestParameterMap();
String name = map.get("n"); // name attribute of node
String type = map.get("t"); // type attribute of node
...
}
Related
<h:outputLabel id="remainingDays" value="#{bean.DueDate}" title="#{bean.remainingDays}" >
<p:ajax listener="#{bean.listenerMethid}" update="remainingDays,remainingDays" process="remainingDays" event="mouseover"></p:ajax>
</h:outputLabel>
<p:tooltip for="remainingDays" id="tooltip" />
public void listenerMethod(AjaxBehaviorEvent event){
}
How can i get Duedate using AjaxBehaviorEvent inside the listenerMethod()
This should work as a general way to get the value through AjaxBehaviorEvent:
public void listenerMethod(AjaxBehaviorEvent event) {
String dueDate = (String) ((UIOutput)event.getSource()).getValue();
}
However, in your case, you can just access it through the varable (or getter) since it is in the same bean as the listenerMethod.
I have a checkbox component with a <f:attribute> and a <p:ajax listener>.
<h:selectManyCheckbox ...>
<p:ajax listener="#{locationHandler.setChangedSOI}" />
<f:attribute name="Dummy" value="test" />
...
</h:selectManyCheckbox>
I tried to get the <f:attribute> value of test inside the listener method as below:
public void setChangedSOI() throws Exception {
FacesContext context = FacesContext.getCurrentInstance();
Map<String, String> map = context.getExternalContext().getRequestParameterMap();
String r1 = map.get("Dummy");
System.out.println(r1);
}
However, it printed null. How can I get it?
Component attributes are not passed as HTTP request parameters. Component attributes are set as .. uh, component attributes. I.e. they are stored in UIComponent#getAttributes(). You can grab them through that map.
Now the right question is obviously how to get the desired UIComponent inside the ajax listener method. There are 2 ways for this:
Specify the AjaxBehaviorEvent argument. It offers a getComponent() method for the very purpose.
public void setChangedSOI(AjaxBehaviorEvent event) {
UIComponent component = event.getComponent();
String dummy = component.getAttributes().get("Dummy");
// ...
}
Use the UIComponent#getCurrentComponent() helper method.
public void setChangedSOI() {
UIComponent component = UIComponent.getCurrentComponent(FacesContext.getCurrentInstance());
String dummy = component.getAttributes().get("Dummy");
// ...
}
I am using RichFaces's ordering list to display a table custom Command objects to the user. The user uses a form to create new commands which are then added to the list. Here is the orderingList implementation:
app.xhtml
<rich:orderingList id="oList" value="#{commandBean.newBatch}" var="com"
listHeight="300" listWidth="350" converter="commandConverter">
<f:facet name="header">
<h:outputText value="New Batch Details" />
</f:facet>
<rich:column width="180">
<f:facet name="header">
<h:outputText value="Command Type" />
</f:facet>
<h:outputText value="#{com.commandType}"></h:outputText>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="Parameters" />
</f:facet>
<h:outputText value="#{com.parameters}"></h:outputText>
</rich:column>
<rich:column>
<h:commandButton value="Remove #{com.id} : #{com.seqNo}"
action="#{commandBean.remove(com.id,com.seqNo)}"
onclick="alert('id:#{com.id} seqNo:#{com.seqNo}');"/>
</rich:column>
My troubles began when I tried to implement a remove button which would send a command's ID and seqNo to the backing bean (cb) to be removed from the list. Here is the backing bean:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class CommandBean implements Serializable{
private static final long serialVersionUID = 1L;
private CommandType type;
private String parameters;
private List<Command> newBatch = new ArrayList<Command>();
private Set<Command> commandSet = new HashSet<Command>();
private String msg = "not removed";
public CommandType[] getCommandTypes() {
return CommandType.values();
}
public void addCommand(CommandType type, String parameters) {
newBatch.add(new Command(type, parameters));
}
CommandType getType() {
return type;
}
void setType(CommandType type) {
this.type = type;
}
String getParameters() {
return parameters;
}
void setParameters(String parameters) {
this.parameters = parameters;
}
public List<Command> getNewBatch() {
return newBatch;
}
public void setNewBatch(List<Command> newBatch) {
this.newBatch = newBatch;
}
public Set<Command> getCommandSet() {
return commandSet;
}
public void setCommandSet(Set<Command> commandSet) {
this.commandSet = commandSet;
}
String getMsg() {
return msg;
}
public void remove(Integer id, Integer seqNo) {
for(Command c : newBatch) {
if(c.getId() == id && c.getSeqNo() == seqNo) {
newBatch.remove(c);
msg = "removed " + c;
return;
}
}
msg = String.format("%d : %d", id,seqNo);
}
}
When the Command (com)'s id and seqNo are passed via #{cb.remove(com.id,com.seqNo)} they are both 0. I also read somewhere that null values are transformed to 0's, so that would explain it. I also tried to pass the Command object directly via #{cb.remove(com)} but the Command was null when bean tried to process it.
I'm betting there is something off with the scoping, but I am too new to JSF to figure it out...
UPDATE
I have eliminated the conflicting #Named tag and have updated the html to reflect the new name of the bean, namely commandBean. Still having issues though.
you can pass the two values as request parameters:
<h:commandButton ... >
<f:param name="id" value="#{com.id}"/>
<f:param name="seqNo" value="#{com.seqNo}"/>
</h:commandButton>
and get retrieve them in managed bean like this:
HttpServletRequest request = ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest());
System.out.println(request.getParameter("id"));
System.out.println(request.getParameter("seqNo"));
You're trying to get a value from a variable that is used in a for-cycle after the cycle is over. The #action is being resolved on the server side, by the time the #var is null.
You can do this:
<a4j:commandButton action="#{commandBean.remove()}" … >
<a4j:param assignTo="#{commandBean.idToRemove}" value="#{com.id}"/>
</a4j:commandButton>
The a4j:param resolves the value on client side, when the button is clicked it sends it to the server.
i would be retrieve the select value of selectOneMenu , but when i execute the programme this exeception appear :
Etat HTTP 500 - /pages/T.xhtml #59,33 itemLabel="#{client2.nomClient}": Property 'nomClient' not found on type java.lang.String
this is my view:
<p:selectOneMenu value="#{ticketBean.maValeur}"
style="width:210px; height:20px; font-size:10px; font-weight:1px;"
required="true" requiredMessage="REQUIRED">
<f:selectItem itemLabel="Select Client" itemValue=""/>
<f:selectItems value="#{ticketBean.getMesElementsTest()}" var="client2" itemLabel="#{client2.nomClient}" itemValue="#{client2.nomClient}"
style="font-size:10px;">
</f:selectItems>
this is my class client :
package com.model;
public class Client {
private int idClient;
private String nomClient;
private String adresseClient;
private String telephoneClient;
private String mailClient;
// GETTERS && SETTERS
}
this is my fonction getMesElementsTest :
private static Map<String, Object> mesElementsTest;
private static ClientDaoImp clientDaoImp= new ClientDaoImp();
public static Map<String, Object> getMesElementsTest() {
mesElementsClient = new LinkedHashMap<String, Object>();
List<Client> clientlist = clientDaoImp.getAllClients();
Iterator<Client> i = clientlist.iterator();
while(i.hasNext()){
Client client=i.next();
mesElementsClient.put(client.getNomClient(),client.getNomClient());
}
return mesElementsClient;
}
You can use only the List, and have something like this:
...
public static List<Client> getMesElementsTest() {
return clientDaoImp.getAllClients();
}
...
on page:
...
<p:selectOneMenu value="#{ticketBean.maValeur}" style="..." required="true" requiredMessage="...">
<f:selectItem itemLabel="Select Client" itemValue=""/>
<f:selectItems value="#{ticketBean.getMesElementsTest()}" var="client2" itemLabel="#{client2.nomClient}" itemValue="#{client2.nomClient}" style="...">
</f:selectItems>
...
And, this form it's more efficient, beacause you dont need fill other data-structure.
Hope it helps
I have problem with parameter at my web application. At some page (index.xhtml) i have:
...
<f:metadata>
<f:viewParam name="backurl"/>
</f:metadata>
<h:form>
<h:outputLabel value="backurl: #{backurl}"/>
<h:commandButton image="/resources/graphics/poland.gif" action="#{userController.setLanguage('pl', param['backurl'])}"/>
</h:form>
setLanguage() method in userController managed bean:
public void setLanguage(String language, String backurl) {
setLang(new Locale(language));
...
}
when i run application and go to index.xhtml page i see backurl: /pages/login.xhtml
but at setLanguage method second parametr (backurl) is null when i click and debug application
Where is problem ?
why dont you use this?
<h:commandButton action="#{userController.setLanguage}">
<f:param name="param1" value="value1" />
<f:param name="backurl" value="#{backurl}" />
</h:commandButton>
and then in your method
public String setLanguage() {
Map<String,String> params =
FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String param1= params.get("param1");
String backurl= params.get("backurl");
}
#{userController.setLanguage('pl', backurl)}
this should work. (didn't test it)
there are many ways to pass parameters to a backing bean. Here is a useful article
You can store the view-param value in the managed-bean (provide accessors for the introduced backurl member, also).
<f:metadata>
<f:viewParam name="backurl" value="#{userController.backurl}" />
</f:metadata>
And then you can get rid of the second method parameter and get the backurl value from the variable in the managed-bean.
action="#{userController.setLanguage('pl')}"/>
The managed bean should look like this:
public class UserController {
private String backurl;
public String getBackurl() { return backurl; }
public void setBackurl(String backurl) { this.backurl = backurl; }
public void setLanguage(String language) {
setLocale(new Locale(language));
System.out.println(backurl); //this refers the variable in the managed-bean
}
}