当我从资源lblFathersOrGuardianName
文件中读取名称mysqlmaven.properties
时,我收到以下错误
Mar 04, 2014 2:53:21 PM com.sun.faces.application.ActionListenerImpl processAction
SEVERE: java.lang.ExceptionInInitializerError
javax.faces.el.EvaluationException: java.lang.ExceptionInInitializerError
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
at javax.faces.component.UICommand.broadcast(UICommand.java:311)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ExceptionInInitializerError
at com.uk.mysqlmaven.jsf.beans.RegistrationBean.validationRegistration(RegistrationBean.java:198)
at com.uk.mysqlmaven.jsf.beans.RegistrationBean.submitRegistrationAction(RegistrationBean.java:189)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
... 24 more
Caused by: java.lang.NullPointerException
at java.util.ResourceBundle$CacheKey.calculateHashCode(ResourceBundle.java:593)
at java.util.ResourceBundle$CacheKey.<init>(ResourceBundle.java:522)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1259)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:721)
at com.uk.mysqlmaven.util.ResourceKeys.<init>(ResourceKeys.java:12)
at com.uk.mysqlmaven.util.ResourceKeys.<clinit>(ResourceKeys.java:8)
... 34 more
Mar 04, 2014 2:53:21 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
WARNING: #{registrationBean.submitRegistrationAction}: java.lang.ExceptionInInitializerError
javax.faces.FacesException: #{registrationBean.submitRegistrationAction}: java.lang.ExceptionInInitializerError
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:114)
at javax.faces.component.UICommand.broadcast(UICommand.java:311)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: javax.faces.el.EvaluationException: java.lang.ExceptionInInitializerError
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
... 23 more
Caused by: java.lang.ExceptionInInitializerError
at com.uk.mysqlmaven.jsf.beans.RegistrationBean.validationRegistration(RegistrationBean.java:198)
at com.uk.mysqlmaven.jsf.beans.RegistrationBean.submitRegistrationAction(RegistrationBean.java:189)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
... 24 more
Caused by: java.lang.NullPointerException
at java.util.ResourceBundle$CacheKey.calculateHashCode(ResourceBundle.java:593)
at java.util.ResourceBundle$CacheKey.<init>(ResourceBundle.java:522)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1259)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:721)
at com.uk.mysqlmaven.util.ResourceKeys.<init>(ResourceKeys.java:12)
at com.uk.mysqlmaven.util.ResourceKeys.<clinit>(ResourceKeys.java:8)
... 34 more
javax.faces.FacesException: #{registrationBean.submitRegistrationAction}: java.lang.ExceptionInInitializerError
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:85)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: javax.faces.FacesException: #{registrationBean.submitRegistrationAction}: java.lang.ExceptionInInitializerError
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:114)
at javax.faces.component.UICommand.broadcast(UICommand.java:311)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
... 19 more
Caused by: javax.faces.el.EvaluationException: java.lang.ExceptionInInitializerError
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
... 23 more
Caused by: java.lang.ExceptionInInitializerError
at com.uk.mysqlmaven.jsf.beans.RegistrationBean.validationRegistration(RegistrationBean.java:198)
at com.uk.mysqlmaven.jsf.beans.RegistrationBean.submitRegistrationAction(RegistrationBean.java:189)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
... 24 more
Caused by: java.lang.NullPointerException
at java.util.ResourceBundle$CacheKey.calculateHashCode(ResourceBundle.java:593)
at java.util.ResourceBundle$CacheKey.<init>(ResourceBundle.java:522)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1259)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:721)
at com.uk.mysqlmaven.util.ResourceKeys.<init>(ResourceKeys.java:12)
at com.uk.mysqlmaven.util.ResourceKeys.<clinit>(ResourceKeys.java:8)
... 34 more
mysqlmaven.properties
#registration.xhtml
lblFirstName=First Name
lblMiddleName=Middle Name
lblLastName=Last Name
lblDateOfBirth=Date Of Birth
lblFathersOrGuardianName=Father's / Guardian Name
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.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) {
FacesContext.getCurrentInstance().addMessage(fathersOrGuardianName, new FacesMessage(FacesMessage.SEVERITY_ERROR, ResourceKeys.getInstance().get("lblFathersOrGuardianName"), "Fathers/GurdianName can't be empty."));
}
return Boolean.FALSE;
}
public void clearRegistrationAction(ActionEvent event) {
this.fathersOrGuardianName = "";
}
}
ResourceKeys.java
package com.uk.mysqlmaven.util;
import java.util.ResourceBundle;
public class ResourceKeys {
private static ResourceKeys instance = new ResourceKeys();
private final ResourceBundle resourceBundle;
private ResourceKeys() {
resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME);
}
/**
* Return singleton instance of this class
* @return singleton instance
*/
public synchronized static ResourceKeys getInstance() {
return instance;
}
private static String BUNDLE_NAME = "/MySqlMavenJSF/com/uk/mysqlmaven/resources/mysqlmaven";
/**
* Return value of the key
* @param key
* @return value
*/
public String get(String key) {
return resourceBundle.getString(key);
}
}
如何从java中的资源文件中获取名称。
mysqlmaven.properties
文件放在我的项目中,如下面的屏幕截图所示。
答案 0 :(得分:0)
mysqlmaven.properties
文件中未访问 ResourceKeys.java
文件路径。
要在properties
中的package
中访问Constants.java
文件,我在package com.uk.mysqlmaven.util;
public class Constants {
public class Resources {
public static final String BUNDLENAME_MYSQLMAVEN = "com.uk.mysqlmaven.resources.mysqlmaven";
}
}
中使用了以下路径。
<强> Constants.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);
}
}
<强> ResourceKeys.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 = "";
}
}
<强> RegistrationBean.java 强>
<!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>
<强> Registration.xhtml 强>
{{1}}