mysqlmaven.properties
file path is not accessed in ResourceKeys.java
file.
For accessing properties
file from package
in java I used below path in Constants.java
.
Constants.java
package com.uk.mysqlmaven.util;
public class Constants {
public class Resources {
public static final String BUNDLENAME_MYSQLMAVEN = "com.uk.mysqlmaven.resources.mysqlmaven";
}
}
ResourceKeys.java
package com.uk.mysqlmaven.util;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.faces.context.FacesContext;
public class ResourceKeys {
private static ResourceKeys instance = new ResourceKeys();
private ResourceBundle resourceBundle;
private ResourceKeys() {
}
/**
* Return singleton instance of this class
* @return singleton instance
*/
public synchronized static ResourceKeys getInstance() {
return instance;
}
/**
* Return value of the key
* @param key
* @param bundleName
* @return value
*/
public String get(String key, String bundleName) {
Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
resourceBundle = ResourceBundle.getBundle(bundleName, locale);
return resourceBundle.getString(key);
}
}
RegistrationBean.java
package com.uk.mysqlmaven.jsf.beans;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import com.uk.mysqlmaven.util.Constants;
import com.uk.mysqlmaven.util.ResourceKeys;
public class RegistrationBean {
private String fathersOrGuardianName;
public String getFathersOrGuardianName() {
return fathersOrGuardianName;
}
public void setFathersOrGuardianName(String fathersOrGuardianName) {
this.fathersOrGuardianName = fathersOrGuardianName;
}
public String submitRegistrationAction() {
if (validationRegistration()) {
return "views/home.faces?faces-redirect=true¶m=98";
} else {
return "";
}
}
public Boolean validationRegistration() {
if (this.fathersOrGuardianName.length() == 0) {
String commonName = ResourceKeys.getInstance().get("lblPleaseEnter", Constants.Resources.BUNDLENAME_MYSQLMAVEN);
String displayName = ResourceKeys.getInstance().get("lblFathersOrGuardianName", Constants.Resources.BUNDLENAME_MYSQLMAVEN);
FacesContext.getCurrentInstance().addMessage(fathersOrGuardianName, new FacesMessage(FacesMessage.SEVERITY_ERROR, commonName +" "+ displayName , "Fathers/GurdianName can't be empty."));
}
return Boolean.FALSE;
}
public void clearRegistrationAction(ActionEvent event) {
this.fathersOrGuardianName = "";
}
}
Registration.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui">
<f:view>
<h:head>
<title>Registration</title>
<f:loadBundle var="mysqlmaven" basename="com.uk.mysqlmaven.resources.mysqlmaven" />
<h:outputStylesheet name="style/mysqlmaven.css"></h:outputStylesheet>
</h:head>
<h:form id="registrationForm">
<table align="center">
<tr>
<td><h:panelGrid>
<h:messages id="registrationMessageId" errorClass="lblRed" warnClass="lblWarning" infoClass="lblGreen"></h:messages>
<h:message for="registrationMessageId"></h:message>
</h:panelGrid> <h:panelGrid columns="1" cellpadding="2" frame="hsides" border="0">
<h:panelGrid columns="2">
<h:panelGroup>
<p:outputLabel value="*" styleClass="lblRed"></p:outputLabel>
<p:outputLabel value="#{mysqlmaven.lblFathersOrGuardianName}"></p:outputLabel>
<p:inputText id="fathersOrGuardianNameId" label="#{mysqlmaven.lblFathersOrGuardianName}" value="#{registrationBean.fathersOrGuardianName}"></p:inputText>
</h:panelGroup>
</h:panelGrid>
</h:panelGrid> <h:panelGrid columns="2">
<p:commandButton id="submitId" value="Submit" title="Submit" actionListener="#{registrationBean.submitRegistrationAction}" update="registrationMessageId"></p:commandButton>
<p:commandButton id="clearId" value="Clear" actionListener="#{registrationBean.clearRegistrationAction}"></p:commandButton>
</h:panelGrid>
</td>
</tr>
</table>
</h:form>
</f:view>
</html>