Task: Assign JavaScript parameters to a property in a bean. It can be done in a convenient way with pe:remoteCommand and pe:assignableParam.
AssignableParam specifies a name of the parameter and an EL expression for a corresponding Java method (setter) the parameter will be applied to.
It allows to attach a converter or define it as usual in JSF by converter attribute.
- - -
Source
<p:growl id="growl" showDetail="true"/>
<pe:remoteCommand id="applyDataCommand" name="applyData" process="@this" update="subject date circle growl"
actionListener="#{remoteCommandController.parametersAssigned}">
<pe:assignableParam name="subject" assignTo="#{remoteCommandController.subject}"/>
<pe:assignableParam name="date" assignTo="#{remoteCommandController.date}">
<f:convertDateTime type="both" pattern="MM/dd/yyyy HH:mm:ss"/>
</pe:assignableParam>
<pe:assignableParam name="circle" assignTo="#{remoteCommandController.circle}">
<pe:convertJson/>
</pe:assignableParam>
</pe:remoteCommand>
<script type="text/javascript">
circle = {
radius: 50,
backgroundColor: "#FF0000",
borderColor: "#DDDDDD",
scaleFactor: 1.2
};
circle2 = {
radius: 32,
backgroundColor: "#FF0320",
borderColor: "#DDFFFD",
scaleFactor: 1.6
};
</script>
<h:outputLabel for="subject" value="Subject: "/>
<h:outputText id="subject" value="#{remoteCommandController.subject}"/>
<br/>
<h:outputLabel for="date" value="Date: "/>
<h:outputText id="date" value="#{remoteCommandController.date}"/>
<br/>
<h:outputLabel for="circle" value="Circle: "/>
<h:outputText id="circle"
value="#{remoteCommandController.circle.radius} - #{remoteCommandController.circle.backgroundColor} - #{remoteCommandController.circle.borderColor} - #{remoteCommandController.circle.scaleFactor}"/>
<br/><br/>
<p:commandButton value="Apply Data" type="button" onclick="applyData('hello world', '05/14/2007 12:55:42', JSON.stringify(circle))"/>
<p:commandButton value="Apply Second Data" type="button" onclick="applyData('hello user', '07/11/2001 11:55:42', JSON.stringify(circle2))"/>
@Named
@RequestScoped
public class RemoteCommandController {
private String subject;
private Date date;
private Circle circle;
public void printMethodParams(final String subject, final Date date, final Circle circle) {
final FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "ActionListener called", "Subject: "
+ subject + ", Date: " + date + ", Circle - backgroundColor: " + circle.getBackgroundColor());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void parametersAssigned() {
final FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "ActionListener called",
"Parameters assigned");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public String getSubject() {
return subject;
}
public void setSubject(final String subject) {
this.subject = subject;
}
public Date getDate() {
return date;
}
public void setDate(final Date date) {
this.date = date;
}
public Circle getCircle() {
return circle;
}
public void setCircle(final Circle circle) {
this.circle = circle;
}
}
public class Circle implements Serializable {
private static final long serialVersionUID = 20111020L;
private int radius;
private String backgroundColor;
private String borderColor;
private double scaleFactor;
public final int getRadius() {
return radius;
}
public final void setRadius(int radius) {
this.radius = radius;
}
public final String getBackgroundColor() {
return backgroundColor;
}
public final void setBackgroundColor(String backgroundColor) {
this.backgroundColor = backgroundColor;
}
public final String getBorderColor() {
return borderColor;
}
public final void setBorderColor(String borderColor) {
this.borderColor = borderColor;
}
public final double getScaleFactor() {
return scaleFactor;
}
public final void setScaleFactor(double scaleFactor) {
this.scaleFactor = scaleFactor;
}
}