How to put current date in PrimeFaces inputMask? - jsf

I have a input in which I want current date to be displayed.
It looks like this:
<p:inputMask id="kalendarz" autocomplete="false" styleClass="#{cc.attrs.styleClass}" mask="99.99.9999"
value="#{cc.attrs.value}" required="#{cc.attrs.required and param['fillDataAction']==null}" disabled="#{cc.attrs.isDisabled}"
style="#{cc.attrs.style}" onchange="#{cc.attrs.onchange}" readonly="#{cc.attrs.readonly}" label="#{cc.attrs.label}"
onblur="#{cc.attrs.onblur}" onselect="#{cc.attrs.onselect}">
<f:converter converterId="#{cc.attrs.converter}">
</p:inputMask>
How can I display current date in this inputMask? I was trying using javascript to setAttribute placeHolder, but it doesn't work

try
#Named("cc")
#Viewscoped
public class YourBean implements Serializable{
....
#PostConstruct
public void init(){
...
this.attrs.setValue(new Date());//this should
...
}
...
}

Related

Trigger listener clicking on ace:textEntry

I'm using JSF 2.0 and I want to invoke a function defined in a Java controller when I click on an ace:textEntry.
I tried in this way:
<ace:textEntry readonly="true" value="#{myController.getValue()}"
onclick="#{myController.myFunc()}"/>
but when my page is open, the click event is called instantly.
So, I tried with:
<ace:textEntry readonly="true" value="#{myController.getValue()}">
<ace:ajax event="click" listener="#{myController.myFunc()}"/>
</ace:textEntry>
but my page is not rendered.
Is there another way to implement this behaviour ?
PS: I can use similar JSF components instead of ace:textEntry too.
First, you do not access getters directly in JSF for value backing - you access the property. Secondly, you should call the listener with the correct signature. To correct your example I would first rewrite the call like this,
<ace:textEntry readonly="true" value="#{myController.value}">
<ace:ajax event="click" listener="#{myController.myFunc}"/>
</ace:textEntry>
Then define MyController, like this;
#Named
#ViewScoped
public class MyController {
private value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public void myFunc(javax.faces.event.AjaxBehaviorEvent event) {
/* Do somethinig here... */
}
}

poll doesn't work with setted var

I want to use one html page with two ManagedBeans. Depending on the parameter in the URL, I want to set the bean name through JSTL variable. For example:
<c:set var="bean" value="#{webModule1}" scope="request" />
or
<c:set var="bean" value="#{webModule2}" scope="request" />
I have class which was use all this time, so for sure this works:
#ManagedBean(name = "webModule1")
#ViewScoped
public class WebModule1 implements Serializable {
protected WebModulesFormBean form;
#PostConstruct
public void init() {
if (!firstInit()){
this.form= new WebModulesFormBean();
}
//some code
}
public void process() {
if(this.form.isActive()){
//some code
}
}
}
And new one:
#ManagedBean(name = "webModule2")
#ViewScoped
public class WebModule2 extends WebModule1 {
public void process() {
if(this.form.isActive()){
//code with some changes
}
}
}
This solutions works with value attribute and form in not null, for example:
<p:treeTable value="#{bean.form.root}" var="node" id="modulesTree">
But I have problem with this piece of code:
<p:poll listener="#{bean.process()}" widgetVar="documentOutcome" autoStart="#{bean.form.start}" update="modulesTree" async="false"
interval="1" id="myPoll2" />
When listener is called, NullPointerException appears.
And this problem is with all p:polls (I have a few), so this is not a problem with method code.
Problem is that 'form' is null, although at the beginning variable 'form' is initialized and treeTable is shown at the page. So 'form' starts to be null when the listener is called.
Thanks!
I have a solution!
The problem was with:
<c:set var="bean" value="#{webModule1}" scope="request" />
scope="request" was not enough and in my understanding this scope meant that when poll was called, a new bean reference was created and form variable was null because it was not a firstInit().
scope="view" is the solution.
Thank you for comments!

I lost my data of sessionScope

I am having a problem with JSF 2.2, I have this struct:
Problem: I can not set variables (by sessionScope - MainBean) since catalog.xhtml (by requestScope/ViewScope - CatalogBean).
LoginBean: SessionScope
#ManagedBean(name="loginBean")
#SessionScoped
public class LoginBean {
private Session userLog;
MainBean: SessionScope
#ManagedBean
#SessionScoped
public class MainBean {
#ManagedProperty(value="#{loginBean}")
private LoginBean userLogin;
CatalogBean: ViewScope/RequestScope
#ManagedBean
#ViewScoped
public class CatalogBean {
#ManagedProperty(value="#{mainBean}")
private MainBean userManagerCatalog;
ItemBean: ViewScope/RequestScope
#ManagedBean
#ViewScoped
public class ItemBean {
#ManagedProperty(value="#{mainBean}")
private MainBean userManagerItem;
The problem is when I enter in item.xhtml (ItemBean) , (since catalog.xhtml) I am saving a variable in MainBean (SessionScope), with:
<p:commandLink action="item.xhtml?faces-redirect=true" actionListener="#{mainBean.setSelectedItem(it)}">
<p:graphicImage library="resources" name="images/folderNormal.png" style="width: 100; height: 100"/>
<f:setPropertyActionListener value="#{it}" target="#{mainBean.selectedItem}" />
</p:commandLink>
Data of Interest
*I had been to using two diferent forms (actionListener and setPropertyActionListener)
**I have to enter catalog to enter the item
*** I am bugging, this makes the change but the next, I see that the program lost this data
**** I had used in Catalog the property SessionScope (fuction well) but I can not change of catalog because the variable does not change its value.
Any help is welcome, thanks in advance

Convert Date on SelectManyCheckbox Selection

i have a simple problem but i didnot find a solution for it.
I have a simple p:selectCheckboxMenu and i want use the selectedDates after click on the button.
I tried it with f:convertDateTime
<h:form id="mainform">
<p:panelGrid columns="2">
<p:selectCheckboxMenu label="Date" value="#{myBean.selectedDates}">
<f:selectItems value="#{myBean.dates}" var="date" itemValue="#{date}" itemLabel="#{myBean.convertDate(date)}"/>
<f:convertDateTime type="date" pattern="dd-MM-yyyy"/>
</p:selectCheckboxMenu>
<p:commandButton value="Test" actionListener="#{myBean.printDates}"/>
</p:panelGrid>
but than i get an Error- Message: "Invaild Value".
Than i tried a Converter:
#FacesConverter("myDateConverter")
public class MyDateConverter extends DateTimeConverter{
public MyDateConverter(){
setPattern("MM/dd/yyyy");
}}
and
<p:selectCheckboxMenu label="Date" value="#{myBean.selectedDates}" converter="myDateConverter">
But same error message. When i use no converter i get "String"- Values in my Date-List because type erasure.
Question: How i get the selected dates as dates?
Here is my bean for completeness:
#ManagedBean(name = "myBean")
#ViewScoped
public class MyBean implements Serializable {
private List<Date> dates;
private List<Date> selectedDates;
private SimpleDateFormat dateFormat;
#PostConstruct
public void init() {
System.out.println("POST CONSTRUCT!");
dateFormat = new SimpleDateFormat("yyyy.MM.dd");
dates = new ArrayList<Date>();
dates.add(new Date());
}
/**
*
*/
public void printDates(){
for(Date d : selectedDates){
System.out.println(d);
}
}
/**
*
* #param date
* #return
*/
public String convertDate(Date date){
return dateFormat.format(date);
}
The converter is the source of the problem as it removes the time and then it uses the default time when converting back to Date.
You can use
< f:convertDateTime pattern="yyyy-MM-dd HH:mm:ss.SSS Z" />
or try with a < f:datetimeconverter >

Format date in <f:selectItem(s) itemLabel> using DateTimeConverter

I have a <h:selectOneMenu> that has <f:selectItems> with CategoryHistory objects loaded in it. I only show the Date date field as itemLabel.
That works but I want to format the date:
I created a converter that extends javax.faces.convert.DateTimeConverter and change the fields in the constructor. But my dates only show in default format :(
DateAndTimeConverter.java
import javax.faces.bean.ManagedBean;
import javax.faces.convert.Converter;
import javax.faces.convert.DateTimeConverter;
import javax.faces.convert.FacesConverter;
#FacesConverter(value = "dateAndTimeconverter")
#ManagedBean
public class DateAndTimeConverter extends DateTimeConverter implements Converter {
public DateAndTimeConverter(){
this.setDateStyle("short");
}
xhtml
<h:selectOneMenu valueChangeListener="#{admin.categoryHistoryListener}"
onchange="submit()" value="#{admin.categoryHistory.id}" converter="#{dateAndTimeconverter}">
<f:selectItems value="#{admin.categoryHistories}" var="n"
itemValue="#{n.id}" itemLabel="#{n.date}">
</f:selectItems>
</h:selectOneMenu>
It also doesn't work when I try:
<h:selectOneMenu valueChangeListener="#{admin.categoryHistoryListener}"
onchange="submit()" value="#{admin.categoryHistory.id}">
<f:converter converterId="dateAndTimeconverter"/>
<f:selectItems value="#{admin.categoryHistories}" var="n"
itemValue="#{n.id}" itemLabel="#{n.date}">
</f:selectItems>
</h:selectOneMenu>
CategoryHistory Has a Date date, and Long id +...
Thank you
Unfortunately, the JSF converters only applies on the input value, not on the input label.
You'll need to solve this other ways. E.g. a getter which uses SimpleDateFormat to format the date. Or if your environment supports EL 2.2, simply invoke the converter method directly (you've it as managed bean already):
<f:selectItems value="#{admin.categoryHistories}" var="n" itemValue="#{n.id}"
itemLabel="#{dateAndTimeconverter.getAsString(facesContext, component, n.date)}">
If you happen to use JSF utility library OmniFaces, then you can also use its of:formatDate() function. E.g.:
<f:selectItems value="#{admin.categoryHistories}" var="n" itemValue="#{n.id}"
itemLabel="#{of:formatDate(n.date, 'd MMM yyyy')}">
You can use a converter method in your bean, as:
public class Admin{
...
public String formatDate(Date fecha, String pattern) {
return (new SimpleDateFormat(pattern)).format(fecha);
}
...
}
And, in your xhtml page inside f:selectItems:
<f:selectItems value="#{admin.categoryHistories}" var="n"
itemValue="#{n.id}" itemLabel="#{admin.formatDate(n.date,'d MMM yyyy')}">
</f:selectItems>
Example
xhtml
<h:selectOneMenu value="#{tbMonitoreoController.fechaMonitoreo}">
<f:selectItems value="#{tbMonitoreoController.fechasMonitoreo}" />
Method in tbMonitoreoController
public SelectItem[] getFechasMonitoreo(){
Collection<Date> entities = getEjbFacade().getFechasMonitoreo();
return JsfUtil.getSelectItemsFechasMonitoreo(entities, true);
}
public static SelectItem[] getSelectItemsFechasMonitoreo(Collection<Date> listDate, boolean selectOne) {
int size = selectOne ? (listDate.size() + 1) : listDate.size();
SelectItem[] items = new SelectItem[size];
int i = 0;
if (selectOne) {
items[0] = new SelectItem(null, "---");
i++;
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
for (Date x : listDate) {
items[i++] = new SelectItem(x, simpleDateFormat.format(x));
}
return items;
}

Resources