JavaScript – compare two dates, validate date range
function validateDateRange(date1Id, date2tId, errMsg){
date1 = document.getElementById(date1Id).value;
date2 = document.getElementById(date2Id).value;
//alert(date1 + ‘<>’ + date2);
if(date1 != null && date2 != null){
if (Date.parse(date1) > Date.parse(date2)) {
document.getElementById(date2Id).value = date1;
alert(errMsg);
return false;
}
}
return true;
}
Get values using parameter passing with JSF and AJAX
EXAMPLE JSP:
<!DOCTYPE html PUBLIC „-//W3C//DTD HTML 4.01 Transitional//EN“ „http://www.w3.org/TR/html4/loose.dtd“>
<%– jsf:pagecode language=“java“ location=“/src/pagecode/cpc/jsf/bp/contextFilters/CtxFilterDocuments.java“ –%><%– /jsf:pagecode –%><%@ page
language=“java“ contentType=“text/html; charset=UTF-8″
pageEncoding=“UTF-8″%><%@ taglib prefix=“f“
uri=“http://java.sun.com/jsf/core“%>
<%@ taglib prefix=“h“ uri=“http://java.sun.com/jsf/html“%>
<%@taglib uri=“http://www.ibm.com/jsf/html_extended“ prefix=“hx“%><html>
<head>
<meta http-equiv=“Content-Type“ content=“text/html; charset=UTF-8″>
<title>Insert title here</title>
</head>
<hx:scriptCollector id=“scCtxFDocs“>
<f:subview id=“svCtxFDocs“>
<hx:panelBox styleClass=“panelBox“ id=“boxCtxFilterDocs“
layout=“pageDirection“ border=“0″ cellpadding=“0″ cellspacing=“0″>
<hx:panelBox styleClass=“panelBox“ id=“boxDocCriteria“
layout=“lineDirection“ align=“left“ valign=“top“ cellpadding=“0″
cellspacing=“0″>
<h:panelGrid columns=“1″>
<h:outputText styleClass=“outputText“ value=“Reg. ID“></h:outputText>
<h:inputText styleClass=“inputText“ id=“prmDocsRegId“></h:inputText>
</h:panelGrid>
<h:panelGrid columns=“1″>
<h:outputText styleClass=“outputText“ value=“Reg. Num“></h:outputText>
<h:inputText styleClass=“inputText“ id=“prmDocsRegNum“></h:inputText>
</h:panelGrid>
</hx:panelBox>
<hx:ajaxRefreshRequest target=“boxDocsResults“ id=“arrboxDocsResults“
params=“prmDocsRegId;prmDocsRegNum“></hx:ajaxRefreshRequest>
<hx:panelBox styleClass=“panelBox“ id=“boxDocsButtons“
layout=“lineDirection“ cellpadding=“0″ cellspacing=“0″>
<hx:commandExButton type=“button“ value=“Търси“
styleClass=“commandExButton“ id=“btnSearchDocsDialog“>
<hx:behavior event=“onclick“ behaviorAction=“get“
id=“bhv_btnSearchDocsDialog“ targetAction=“boxDocsResults“></hx:behavior>
</hx:commandExButton>
</hx:panelBox>
<hx:panelBox styleClass=“panelBox“ id=“boxDocsResults“ border=“0″
layout=“pageDirection“ cellpadding=“0″ cellspacing=“0″>
<h:outputText id=“prm1Label“ value=“prmDocsRegId value“ />
<h:outputText id=“prm1″ value=“#{mBResults.prm1}“ />
<h:outputText id=“prm1Label“ value=“prmDocsRegNum value“ />
<h:outputText id=“prm2″ value=“#{mBResults.prm2}“ />
</hx:panelBox>
</hx:panelBox>
</f:subview>
</hx:scriptCollector>
</html>
JAVA CLASS:
public class MBSearchedResults{
Map<String, String> ctxMap;
private String prm1;
private String prm2;
public MBSearchedResults() {
super();
this.ctxMap = FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap();
}
public String getPrm1(){
// Взимаме си параметрите
String pprmDocsRegId = this.ctxMap.get(„prmDocsRegId“);
if (pprmDocsRegId != null) {
this.prm1 = pprmDocsRegId;
}
return this.prm1;
}
public String getPrm2(){
String pprmDocsRegNum = this.ctxMap.get(„prmDocsRegNum“);
if (pprmDocsRegNum != null) {
this.prm2 = pprmDocsRegNum;
}
return this.prm2;
}
}
Java – Another enumeration example
public enum EnumRefValues {
ref_type_1(„sysNameRefType1″, „name1″),
ref_type_2(„sysNameRefType2″, „name2″),
ref_type_3(„sysNameRefType3″, „name3″);
private String sysName;
private String name;
private MapNomValues(String sysName, String name) {
this.sysName = sysName;
this.name = name;
}
private MapNomValues(String value) {
this.sysName = value;
}
public String getSysName() {
return this.sysName;
}
public String getName() {
return this.name;
}
}
Results:
ref_type_1.getName(); returns -> name1
ref_type_1.getSysName(); returns -> sysNameRefType1
Java – Clone objects – example
For objects cloning, you can use java.lang.Cloneable. It is not applicable for deep cloning (for deep cloning use Serialization), because you should control the overall clone process, but it works for simple cloning in most cases.
To use clone() method, you should implement java.lang.Cloneable and override clone(), because it is not visible. There is an example:
public abstract class Criteria implements Cloneable {
Criteria() {
super();
}
@Override
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
Java – Scanner example
package com.xxxxx.tests;
import java.util.Scanner;
public class ScannerExample{
/**
* @param args
*/
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println(„Write smthng: „);
String a = scan.next();
System.out.println(„Smthng: “ + a);
}
}
JSF – IBM DatePicker, choose first day of the week – example
Ако искате първият ден от седмицата в календара ви да е понеделник просто трябва да добавите /firstDay=“1″/.
<h:inputText id=“calExample“
styleClass=“inputText“
value=“#{mBean.dateRealBegin}“>
<hx:convertDateTime pattern=“#{mBean.dateFormat}“ />
<hx:inputHelperDatePicker id=“calExampleDatePicker“ firstDay=“1″ />
</h:inputText>
JSF – JWL tree () , how to get selected node value – example
<hx:panelBox styleClass=“panelBox“ id=“att_gridReqDocAttachmentTree“
width=“70%“ layout=“pageDirection“ align=“left“ valign=“top“>
<%– Имаме ManagedBean с property docAttachmentsRoot –%>
<odc:tree styleClass=“tree“ id=“stand_outDocAttTree“
rootVisibleFlag=“true“ enableSelect=“false“ height=“200″
value=“#{mBInnerDoc.docAttachmentsRoot}“
onnodehighlight=“return func_setSelectedAttachment(this, event);“>
<%– Имаме клас TaskObjectDocument, който има property document и метод getTaskObjectDocs (връща списък от същите обекти, които дървото обхожда рекурсивно и изчертава, както и getTaskObjSummary (Връща стринг, който се визуализира на екрана за всеки node)) –%>
<odc:treeNodeAttr attributeName=“taskObjSummary“
className=“cpc.common.xxxxx.TaskObjectDocument“
id=“stand_attRootNode“ referenceName=“document,taskObjectDocs“>
<%– Това е допълнителна колона, която ние ще скрием, използва се за идентифициране на node, на който е кликнато –%>
<odc:treeColumnData id=“hiddenTodId“ attributeName=“docId“></odc:treeColumnData>
</odc:treeNodeAttr>
<%– Втори елемент от дървото – document, който има метод getTreeName(), връща стринг, който се визуализира на екрана–%>
<odc:treeNodeAttr attributeName=“treeName“
className=“cpc.common.xxxxx.document.Document“
id=“stand_attDocs“>
<%– Това е допълнителна колона, която ние ще скрием, използва се за идентифициране на node, на който е кликнато –%>
<odc:treeColumnData id=“hiddenDocId“ attributeName=“id“></odc:treeColumnData>
</odc:treeNodeAttr>
<%– Използва се odc:treeTable, когато имаме повече от една колони –%>
<odc:treeTable id=“treeTableAtts“>
<odc:treeColumn id=“attCol1″ label=“"></odc:treeColumn>
<%– За да скрием втората колона, използваме: width=“0″ –%>
<odc:treeColumn id=“attCol2″ label=“" width=“0″></odc:treeColumn>
</odc:treeTable>
</odc:tree>
</hx:panelBox>
<%– Използваме скрито поле, където с помощта на JS, сетваме кликнатия node в ManagedBean-а –%>
<h:inputHidden id=“selectedAttachment“
value=“#{mBInnerDoc.selectedNode}“></h:inputHidden>
// get selected node value using JavaScript
function func_setSelectedAttachment(thisObj, thisEvent) {
valueToSet1 = thisEvent.eobject.eGet(„id“);
valueToSet2 = thisEvent.eobject.eGet(„docId“);
//Скрито поле, където пазим ид-то на избраният елемент от дървото, за да е достъпен в ManagedBean-а
hiddenObjId = func_getFieldIdByType(‘input’, ’svAttachments:selectedAttachment’);
hiddenObj = document
.getElementById(hiddenObjId);
if(valueToSet2 != null){
hiddenObj.value = valueToSet2;
}else{
hiddenObj.value = valueToSet1;
}
//alert(hiddenObj.value);
return true;
}
JavaScript – getSelectedCheckBox – example function
// returns true when checkBox given by Id is checked
function getSelectedCheckBoxById (cbId) {
cb = document.getElementById(cbId);
if(cbId && cb.checked){
return true;
}
return false;
}