How to create a manifest file with Maven

This tutorial will show you how to use the maven-jar-plugin to create a manifest file, and package / add it into the final jar file. The manifest file is normally used to define following tasks : Define the entry point of the Application, make the Jar executable. Add project dependency classpath. When you run the …

Read more

ModSecurity exclude rules for editing posts and pages in WordPress

When editing post or page in WordPress, sometime the server’s firewall will block my IP address, and the log showed the following error: Terminal lfd: (mod_security) mod_security triggered by xx.xx.xx.xx : 5 in the last 300 secs The quick fix is to restart the modem or uses a VPN to get a new IP to …

Read more

How to create a jar file with Maven

In this tutorial, we will show you how to use Maven build tool, to create a single executable Jar, and how to deal with the project’s dependencies. Tools used : Maven 3.1.1 JDK 1.7 log4j 1.2.17 Joda-time 2.5 Eclipse 4.3 1. Create a simple Java project Create a Java project from the Maven quick start …

Read more

Java enum example

Some of the Java enum examples, and how to use it, nothing special, just for self-reference. Note Consider the Enum type if your program consists of a fixed set of constants, like seasons of the year, operations calculator, user status and etc. 1. Basic Enum UserStatus.java public enum UserStatus { PENDING, ACTIVE, INACTIVE, DELETED; } …

Read more

Spring @Autowired into JSF custom validator

Here’s the scenario, create a custom JSF validator, injects a bean via Spring’s @Autowired. UsernameValidator.java – Custom JSF validator package com.mkyong.user; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.validator.FacesValidator; import javax.faces.validator.Validator; import javax.faces.validator.ValidatorException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.mkyong.user.bo.UserService; @Component @Scope("request") @FacesValidator("UsernameValidator") public class UsernameValidator implements Validator { @Autowired UserService userService; @Override public …

Read more

How to get JSF id via jQuery

See a simple JSF example : <h:form id="signup-form"> <h:inputText id="email" value="#{beanBean.email}" /> </h:form> It will generates following HTML code : <input id="signup-form:email" type="text" /> Uses jQuery selector to get the email id, but failed. <script> jQuery(document).ready(function($) { $(‘#signup-form:email’).checkEmailFormat(); }); </script> Solution This is a well-known problem to integrate JSF and jQuery – the colon “:” …

Read more

PrimeFaces focus error field automatically

In PrimeFaces, if focus component <p:focus> is enabled : When page is loaded, it will focus on first visible input field; If validation failed, it will focus on first error field automatically. It works very nice, and a must use component in form handling. Just wonder why don’t make it enable by default? In this …

Read more

PrimeFaces + JSF Email validator example

To validate email, uses JSF <f:validateRegex>, and puts following regular expression. This regex should be able to validates most of the email format, and I’m using it for few projects. Email Regular Expression ^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$ P.S For detail explanation, refer to this how to validate email address with regular expression. In this tutorial, we will show …

Read more

PrimeFaces – Watermark on text input

In PrimeFaces, your can use <p:watermark> to display watermark effect on input field. This watermark component is using HTML5 placeholder attribute in supported browsers like Safari, Chrome and Firefox, and fall back to JavaScript solution for non-support browser like IE. Display watermark text in input field <p:inputText id="username" required="true" label="username" size="40" value="#{userBean.username}" /> <p:watermark for="username" …

Read more

How to access web server 8080 in Mac OS X

Mac OS x 10.8.3, running a Java’s web application on Tomcat, port 8080. Users from same network are able to access my web server via port 80, but failed on port 8080. Is there a firewall setting blocked the incoming connection via port 8080. Solution 1 The simplest solution is turn off your Firewall. Mac …

Read more

How to override PrimeFaces CSS?

Often times, you may need to override default PrimeFaces CSS with your pretty customize values. In this example, we will show you how to override PrimeFaces error message style. Debug with FireBug, the PF’s error messages style are from primefaces.css primefaces.css .ui-message-info, .ui-message-error, .ui-message-warn, .ui-message-fatal { border: 1px solid; margin: 0px 5px; padding: 2px 5px; …

Read more

Resource ordering in PrimeFaces

Since PrimeFaces 3.0, it provides a very customizable resource ordering. See following order : 1. “first” facet if defined. <f:facet name="first"> <!– load css, js or others –> </f:facet> 2. PrimeFaces – JSF registered CSS. 3. PrimeFaces – Theme CSS. 4. “middle” facet if defined. <f:facet name="middle"> <!– load css, js or others –> </f:facet> …

Read more

Spring 3 and JSR-330 @Inject and @Named example

Since Spring 3.0, Spring supports for the standard JSR 330: Dependency Injection for Java. In Spring 3 application, you can uses standard @Inject instead of Spring’s @Autowired to inject a bean. @Named instead of Spring’s @Component to declare a bean. Those JSR-330 standard annotations are scanned and retrieved the same way as Spring annotations, the …

Read more

How to inject null value in Spring

In Spring, you can uses this special <null /> tag to pass a “null” value into constructor argument or property. 1. Constructor Argument The wrong way to inject a null into constructor argument, a really common mistake, and nice try 🙂 <bean id="defaultMongoTypeMapper1" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper"> <constructor-arg name="typeKey" value="null" /> </bean> Correct way. <bean id="defaultMongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper"> <constructor-arg …

Read more

Spring Data MongoDB remove _class column

By default, SpringData’s MappingMongoConverter add an extra “_class” column for every object saved in MongoDB. For example, public class User { String username; String password; //…getters and setters } Save it MongoOperations mongoOperation = (MongoOperations)ctx.getBean("mongoTemplate"); User user = new User("mkyong", "password123"); mongoOperation.save(user, "users"); Result > db.users.find() { "_class" : "com.mkyong.user.User", "_id" : ObjectId("5050aef830041f24ff2bd16e"), "password" : …

Read more

MongoDB hello world example

A quick guide to show you how to do basic operations like create, update, find, delete record and indexing in MongoDB. This example is using MongoDB 2.0.7, running on Mac OS X 10.8, both MongoDB client and server console are run on localhost, same machine. 1. Install MongoDB Install MongoDB on Windows, Ubuntu or Mac …

Read more

Copy file to / from server via SCP command

SCP uses Secure Shell (SSH) to transfer data between client and remote server, it’s fast and secure. In this article, we will show you two common SCP copying examples : Copying data from your computer to remote server. Copying data from remote server to your computer. 1. Copying data to Remote Server Example 1.1 – …

Read more

Download PrimeFaces ShowCase and Source Code

The PrimeFaces showcase is showing how to use the entire 100+ PrimeFaces components, which is impressive! Actually, you can download the entire PrimeFaces showcase web application (in war) and source code, and deploy on your local server for testing or further study. Here we show you how. 1. Visit Maven Repository The entire PrimeFaces showcase …

Read more

How to create user defined properties in Maven

Custom properties or variables are useful to keep your Maven pom.xml file more easy to read and maintain. File : pom.xml <project> … <properties> <my.plugin.version>1.5</my.plugin.version> </properties> … </project> In above pom.xml, you can refer “my.plugin.version” via code ${my.plugin.version}. Example 1 A classic use case is used to define a jar or plugin version. <properties> <spring.version>3.1.2.RELEASE</spring.version> …

Read more

PrimeFaces compress and combine JavaScript and CSS

In this tutorial, we will show you how to use PrimeFaces extensions – Maven plugin, to compress and combine JavaScript and CSS files, a web resources optimization example, to make web site load faster. Tool tested : PrimeFaces 3.3 PrimeFaces-Extensions 0.5 Maven 3.0.3 Note This guide is example-driven, for detail explanation and all other plugin …

Read more

Eclipse code assist in Maven pom.xml

By default, code assist in editing a pom.xml is NOT supported in Eclipse IDE. It make Maven project hard to maintain, and who will remember who all those tags? Solution You need a pom.xml Maven editor, which is available in m2eclipse plugin. In Eclipse, menu, select “Help” -> “Install New Software”, and put following m2eclipse …

Read more

Remove all default CSS styling from PrimeFaces

By default, PrimeFaces will return total of four files, 2 css files and 2 Javascript files. <link type="text/css" rel="stylesheet" href="/primefaces/faces/javax.faces.resource/theme.css?ln=primefaces-aristo" /> <link type="text/css" rel="stylesheet" href="/primefaces/faces/javax.faces.resource/primefaces.css?ln=primefaces" /> <script type="text/javascript" src="/primefaces/faces/javax.faces.resource/jquery/jquery.js?ln=primefaces"></script> <script type="text/javascript" src="/primefaces/faces/javax.faces.resource/primefaces.js?ln=primefaces"></script> Some people don’t like the default PrimeFaces CSS file and wonder how to remove it? Solution You are allow to remove only the …

Read more

Create a custom theme in PrimeFaces

PrimeFaces is using jQuery ThemeRoller framework, and it comes with 30+ pre-defined themes, visit all available themes, before create a custom theme. In this tutorial, we will show you how to create a custom theme and apply it in PrimeFaces. 1. ThemeRoller CSS Framework Visit jQuery ThemeRoller, play around the value to customize your theme, …

Read more

Changing theme in PrimeFaces

PrimeFaces is using jQuery ThemeRoller CSS theme framework, and come with 30+ pre-designed themes that you can download and apply in seconds. In this tutorial, we will show you how to change a theme n PriceFaces. There are two ways to change a theme : Using Maven to download and apply. Download manually and apply. …

Read more

Generate getters and setters in Eclipse IDE

Still busy writing setters and getters method manually? Actually, Eclipse IDE is able to help you generate the setters and getters method automatically, and this feature is bundle with Eclipse IDE long time ago, surprisingly, many Java developers are not aware of it? See following steps to show you how. 1. Java Pojo A simple …

Read more

PrimeFaces : java.io.IOException: Not in GZIP format

Recently, testing on PrimeFaces’ idleMonitor component. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"> <h:head> </h:head> <h:body> <h1>PrimeFaces and idleMonitor</h1> <p:growl id="messages" showDetail="true" sticky="true" /> <p:idleMonitor timeout="10000" update="messages"> <p:ajax event="idle" listener="#{idleBean.idleListener}" update="messages" /> </p:idleMonitor> </h:body> </html> Problem After 10 seconds, console prompts java.io.IOException: Not in GZIP format? That’s weird, what GZIP to do iwith idleMonitor …

Read more

PrimeFaces code completion in Eclipse IDE

In Eclipse, as long as your JSF + PrimeFaces project with JSF facet enabled, the PrimeFaces code completion is available automatically. Figure : PrimeFaces code assist in Eclipse IDE Tools Tested : JSF 2.1.11 Eclipse 4.2 PrimeFaces 3.3 Figure : Steps to enable JSF facet in Eclipse IDE. For detail instruction to configure JSF facet, …

Read more

How to Solve Sudoku using Hadoop ?

Hadoop has provides an example jar file for test purposes. In this jar file there is an example to solve sudoku problems. 1. Input File To use this example we need to make an input file for the sudoku problem. In this file each sudoku cell can be either a number or ‘?’ with spaces …

Read more

PrimeFaces idleMonitor example

The idleMonitor component, monitor user action and fire when user goes idle or active again. By default, the idle time is set to 5 minutes (300000 ms), and you can customize the duration via timeout attribute like below : <!– fire if user idle for 10 seconds –> <p:idleMonitor timeout="10000" onidle="idleDialog.show()" /> In this tutorial, …

Read more

GAE : how to output log messages to a file

By default, all logging messages will output to log console. To change the logging settings, find this file – {Google App Engine SDK directory}\google\appengine\tools\dev_appserver_main.py. File : dev_appserver_main.py – Find following pattern #… import getopt import logging import os import signal import sys import tempfile import traceback logging.basicConfig( level=logging.INFO, format=’%(levelname)-8s %(asctime)s %(filename)s:%(lineno)s] %(message)s’) #… Output to …

Read more

How to find large file size on Mac OS X

In this tutorial, we show you two ways to find large files on Mac OS X. Built-in search tools Find command 1. Search Tools To open the search tools, do following : Mac OS X Desktop, run Finder. Press Command+F to open “Search Tools” (it also can be located from menu -> File -> Find). …

Read more

PrimeFaces hello world example

In this tutorial, we will show you how to create a JSF 2 + PrimeFaces wed project, the final output is display a “hello world” string in PrimeFaces editor component. Tools used : JSF 2.1.11 Primefaces 3.3 Eclipse 4.2 Maven 3 Tested on Tomcat 7 Note PrimeFaces only requires a JAVA 5+ runtime and a …

Read more

Linux Search and Replace example

I was recently in a situation of having to change some text that appeared in a bunch of config files that were inside nested sub-directories. Not having time to dust off 10-year old perl skills, I was looking for some shell commands that would work. Surprisingly, there are many examples on the web that do …

Read more

sed command hits “undefined label” error on Mac OS X

See following scenario, create a file, add content, search and replace it. $ touch testing.txt $ echo "this is mkyong.com" > testing.txt $ cat testing.txt this is mkyong.com $ sed -i ‘s/mkyong/google/g’ testing.txt sed: 1: "testing.txt": undefined label ‘esting.txt’ This sed -i ‘s/mkyong/google/g’ testing.txt command is working properly in Linux, but hits “undefined label” error …

Read more