I am trying to highlight an p:inputText (used to hold an email address) if this value doesn't match the value which came from the database. Here is my java code :
private UIInput gUiInputemailAdd; //global variable
public void validateCredentials(ComponentSystemEvent event) {
UIComponent components = event.getComponent();
UIInput uiInputemailAdd = (UIInput) components.findComponent("emailAdd");//ID of textbox
gUiInputemailAdd = uiInputemailAdd;
// uiInputemailAdd.setValid(false);//works fine here(it highlights this particular textBox)
}
public boolean forgotPasswordValidator(UserDetailTO userDetailTO)
throws MessagingException {
if (!gEmailAddress.equalsIgnoreCase(userDetailTO.getEmailAddress())) {
System.out.println(gUiInputemailAdd.getValue().toString()); //works fine and print value
gUiInputemailAdd.setValid(false);//doesn't work here
}
}
Xhtml code:-
<p:outputLabel for="emailAdd" value="EmilAddresss" />
<p:inputText value="#{loginTo.emailAddress}" id="emailAdd"/>
Please help me on this I have been stuck here for so long.
Related
I'm setting an input component from my bean
In my bean
public void InicializarCronometro(ComponentSystemEvent e) {
HtmlForm form = (HtmlForm) e.getComponent();
UIInput input = new HtmlInputText();
input.setId("cronoInput"); // Must be unique!
input.setValueExpression("value", createValueExpression("#{jugarPartidoAdmBean.labelCrono}", String.class));
form.getChildren().add(input);
}
private ValueExpression createValueExpression(String valueExpression, Class<?> valueType) {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication().getExpressionFactory()
.createValueExpression(context.getELContext(), valueExpression, valueType);
}
In my xhtml file:
<b:navBar brandHref="indexAdm.xhtml" inverse="true" fixed="bottom" sticky="false">
<b:navbarLinks>
<h:form id="footForm" styleClass="navbar-form navbar-right">
<f:event type="postAddToView" listener="#{jugarPartidoAdmBean.InicializarCronometro}" />
<b:row>
<b:column col-xs="12" col-sm="3" col-md="4" />
<b:column col-xs="12" col-sm="3" col-md="8" offset="12">
<b:commandButton value="Comensar" look="success" action="#{jugarPartidoAdmBean.ComensarPartido()}" style="margin-left: 10%;" />
</b:column>
</b:row>
</h:form>
</b:navbarLinks>
</b:navBar>
When I press the button I call this method to start the crono
public void ComensarPartido() {
Cronometro crono = new Cronometro(0, 0);
crono.addObserver(this);
Thread t = new Thread(crono);
t.start();
}
The crono class works fine and in my beAN I implemente Observer and in the update method I have this:
#Override
public void update(Observable o, Object arg) {
this.setLabelCrono((String) arg);
//InicializarCronometro(new HtmlInputText());
System.out.println(this.getLabelCrono());
}
in the line this. setLbaelCrono((String) arg) I'm setting the counter 00:00, 00:01, 00:02... etc.
I want to update my input component to show the crono but I don't getting that result the input component remain in 00:00 that it's tha value of "labelCrono" by default.
What do I need to do to update the component in my view.??
I had this issue for a few hours and I surfed the net to try and find the solution but unfortunately I came up short.
Here is what I want to do.
I want to set p:outputText values when item in my autoComplete gets selected.
Here is the code:
<p:autoComplete
completeMethod="#{dynamicSearchBean.getCustomers}"
minQueryLength="1">
<p:ajax event="itemSelect"
listener="#{dynamicSearchBean.handleSelection}"
update="addName"/>
</p:autoComplete>
<h:outputText id="addName" value="#{dynamicSearchBean.firstName}"/>
And the backing bean:
public void handleSelection(SelectEvent event)
{
String value = (String) event.getObject();
System.out.println("selected "+value);
}//end method handleSelection
My autoComplete is working fine by getting values from DB but no event is being triggered when I select the value, and that is the main issue here.
Thanks for the help!
try this
public void handleSelection(SelectEvent event)
{
String value = (String) event.getObject();
this.firstName=value;
}//end method handleSelection
so I have an extendedDataTables as follows(Code is shortened to keep it straightforward).
<rich:extendedDataTable value="#{package.packageList}" var="o"
styleClass="listStyle" selection="#{package.selection}"
selectionMode="single">
<a4j:ajax execute="#form" event="selectionchange"
listener="#{package.selectionListener}" render=":res" />
<rich:column>
<f:facet name="header">Package ID</f:facet>
#{o.packageID}
</rich:column>
</rich:extendedDataTable>
So when the user clicks on this it outputs the details of it to another table inside the a4j:outputPanel that follows.
<a4j:outputPanel id="res">
<rich:panel header="Selected Package"
rendered="#{not empty package.selectionPackage}">
</rich:panel>
</a4j:outputPanel>
This part works. However this new detail now contains several variables and also an arrayList of objects with which I need to also be able to select. So this arrayList generates its own extendedDataTable which I need to then select and retrieve its information. This part is where it is breaking. What I have attempted so far looks as follows.
<a4j:outputPanel id="res">
<rich:panel header="Selected Package"
rendered="#{not empty package.selectionPackage}">
<rich:dataTable value="#{package.selectionPackage}"
var="extensionTable" styleClass="listStyle" selectionMode="single">
<rich:column>
<rich:extendedDataTable value="# {extensionTable.extensionList}"
var="selectedExtension" styleClass="listStyle" style="height:200px;"
selection="#{package.extensionSelection}" selectionMode="single">
<a4j:ajax execute="#form" event="selectionchange"
listener="#{package.selectionExtensionListener}" render=":extensions" />
<rich:column>
<f:facet name="header">Extension Add on list</f:facet>
#{selectedExtension.extensionName}"
</rich:column>
</rich:extendedDataTable>
</rich:column>
</rich:dataTable>
</rich:panel>
</a4j:outputPanel>
I have another a4j panel to handle the new extensions selected object. The problem is that my selectedExtensionListener is not being called. I do not know why, is what I am trying even possible. The selection is working as the rows are changing colour however the selectionExtensionListener is never being called. I have some system.out.printlnts in just to check and whereas the first listener is being called the second one is not. Forgive me if this is obvious or not even possible but I am relatively new to jsf. Any help would be greatly appreciated.
I will copy and paste my bean below to show you the backend of the code. I will exclude declaring all of my variables and populating my lists and objects etc as it will just clog up the code, if you need them just let me know and I will edit them in. (Sorry for formatting, I do not know if you can format code here?)
public class Package implements Serializable{
public void selectionListener(AjaxBehaviorEvent event) {
System.out.println("In selection listener");
UIExtendedDataTable dataTable = (UIExtendedDataTable) event.getComponent();
Object originalKey = dataTable.getRowKey();
selectionPackage.clear();
for (Object selectionKey : selection) {
dataTable.setRowKey(selectionKey);
if (dataTable.isRowAvailable()) {
selectionPackage.add((PackageClass) dataTable.getRowData());
}
}
dataTable.setRowKey(originalKey);
}
public void selectionExtensionListener(AjaxBehaviorEvent event) {
System.out.println("Testing 1");
UIExtendedDataTable dataTable = (UIExtendedDataTable) event.getComponent();
Object originalKey = dataTable.getRowKey();
selectionExtension.clear();
for (Object selectionKey : extensionSelection) {
dataTable.setRowKey(selectionKey);
if (dataTable.isRowAvailable()) {
System.out.println("Testing 2");
selectionExtension.add((ExtensionClass) dataTable.getRowData());
}
}
dataTable.setRowKey(originalKey);
}
public Collection<Object> getSelection() {
return selection;
}
public void setSelection(Collection<Object> selection) {
this.selection = selection;
}
public Collection<Object> getExtensionSelection() {
return extensionSelection;
}
public void setExtensionSelection(Collection<Object> extensionSelection) {
this.extensionSelection = extensionSelection;
}
public List<PackageClass> getPackageList() {
return packageList;
}
public void setPackageList(ArrayList<PackageClass> packageList) {
this.packageList = packageList;
}
public PackageClass getSelectionPackage() {
if (selectionPackage == null || selectionPackage.isEmpty()) {
return null;
}
return selectionPackage.get(0);
}
public void setSelectionPackage(ArrayList<PackageClass> selectionPackage) {
this.selectionPackage = selectionPackage;
}
public ArrayList<PackageClass> getSelectionPackages() {
return selectionPackage;
}
public ExtensionClass getSelectionExtension() {
if (selectionExtension == null || selectionExtension.isEmpty()) {
return null;
}
return selectionExtension.get(0);
}
public void setSelectionExtension(ArrayList<ExtensionClass> selectionExtension) {
this.selectionExtension = selectionExtension;
}
public ArrayList<ExtensionClass> getSelectionExtensions() {
return selectionExtension;
}
}
I have a JSF ajax keyup event linked to an event listner in a backing bean.
The code in the JSF file is like below.
<h:inputText id="txtDescription" value="#{institutionController.current.description}" disabled="#{institutionController.modifyControlDisable}" >
<f:ajax event="keyup" listener="#{institutionController.changeDetailsEvent}" />
</h:inputText>
The code in the backing bean is like below.
public void changeDetailsEvent(AjaxBehaviorEvent event) {
}
I want to achieve different logic depending on the key presses, like shown is pseudocode below.
public void changeDetailsEvent(AjaxBehaviorEvent event) {
If (event.key = Key.enter) {
do something;
} else if (event.key = Key.Escape) {
so something else;
} else {
do nothing;
}
}
Can someone please tell me how this is done in the backing bean?
The AjaxBehaviorEvent doesn't contain any information about the JavaScript event object. You need to pass the desired information along yourself. This can be achieved by a hidden input field whose value is to be prefilled by JavaScript. For example,
<h:inputText value="#{bean.input}" onkeyup="document.getElementById('#{keyCode.clientId}').value=event.keyCode">
<f:ajax event="keyup" execute="#this keyCode" listener="#{bean.listener}" />
</h:inputText>
<h:inputHidden id="keyCode" binding="#{keyCode}" value="#{bean.keyCode}" />
(please note that the id of the hidden field is included in execute so that it get submitted along on the ajax request, please also note that the binding is used to be able to dynamically obtain the generated client ID in document.getElementById() in order to set the key code value, you could alternatively also hardcode the client ID if it's fixed)
with
private String input;
private int keyCode;
public void listener() {
switch (keyCode) {
case 13:
// Enter key was pressed.
break;
case 27:
// Escape key was pressed.
break;
default:
// Other key was pressed.
break;
}
}
You can find an overview of all valid keyCode values in the Mozilla DOM reference.
I am having a problem with custom components in facelets. The first time that the page is rendered, the attributes are set properly on the component class. When a form is submitted however, the attributes are not set.
Here is the class that I am using to test this.
public class TestEcho extends UIData
{
/** Logger. */
private static Log log = LogFactory.getLog(TestEcho.class);
private String msg;
public TestEcho()
{
log.debug("Constructor.");
}
public void encodeEnd(FacesContext context) throws IOException
{
ResponseWriter writer = context.getResponseWriter();
writer.startElement("div", this);
writer.writeText("The value of msg is '" + msg + "'.", null);
writer.endElement("div");
}
public void setMsg(String msg)
{
log.debug("Setting msg to '" + msg + "'.");
this.msg = msg;
}
}
The component is used in the .xhtml page like this.
<h:form>
<v:testEcho msg="hello" />
<h:commandButton action="#{PictureManager.trigger}" value="Click" />
</h:form>
When the page renders for the first time, the component renders the following html code.
<div>The value of msg is 'hello'.</div>
When the button is clicked, it renders this.
<div>The value of msg is 'null'.</div>
From the log, you can see that the component is constructed again, but the attribute is not set.
13:23:42,955 DEBUG [TestEcho] Constructor.
13:23:42,955 DEBUG [TestEcho] Setting msg to 'hello'.
----- Button was pressed here -----
13:25:48,988 DEBUG [TestEcho] Constructor.
13:25:49,144 DEBUG [PictureManager] Button pressed.
From what I understand, facelets does all the wiring of attributes to components so I don't need a tag class, but I don't understand why the attribute would be set correctly the first time, but not the second time.
You must save your state by overriding the saveState and restoreState methods.
So, saveState must return a Serializable object (e.g. a JavaBean or Object[] array) containing the value in msg and whatever is returned by super.saveState. This object will be provided to restoreState where the method must restore msg from the object and pass the parent state to super.restoreState.
McDowell's answer did it. Just for completeness, here's the two methods I added.
public Object saveState(FacesContext context)
{
Object[] rtrn = new Object[2];
rtrn[0] = super.saveState(context);
rtrn[1] = msg;
return rtrn;
}
public void restoreState(FacesContext context, Object state)
{
Object[] values = (Object[]) state;
super.restoreState(context, values[0]);
msg = (String) values[1];
}