gpt4 book ai didi

java - I18N 属性上的 org.springframework.context.NoSuchMessageException

转载 作者:行者123 更新时间:2023-11-30 06:23:08 28 4
gpt4 key购买 nike

我试图通过部署一个简单的动态Web项目来理解Spring框架中国际化(I18N)的概念。我遇到了一个无法解决的问题。以下是使用的 jar 文件:

*commons-logging.jar
*org.springframework.asm-3.0.1.RELEASE.jar
*org.springframework.asm-sources-3.0.1.RELEASE.jar
*org.springframework.beans-3.0.1.RELEASE.jar
*org.springframework.beans-sources-3.0.1.RELEASE.jar
*org.springframework.context-3.0.1.RELEASE.jar
*org.springframework.context-sources-3.0.1.RELEASE.jar
*org.springframework.core-3.0.1.RELEASE.jar
*org.springframework.core-sources-3.0.1.RELEASE.jar
*org.springframework.expression-3.0.1.RELEASE.jar
*org.springframework.expression-sources-3.0.1.RELEASE.jar

以下是属性文件:

Data_ka.properties

label=ಹೆಸರು

Data_hi.properties

label=नाम्

XML 文件:

spring.xml

<!-- spring - I18N deomo -->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="resources/Data" />
</bean>
</beans>

JSP 文件:

index.jsp

<%@page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@page import="java.util.Locale"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%
String lang= request.getHeader("accept-language");
Locale locale = new Locale(lang);
ApplicationContext ap = new ClassPathXmlApplicationContext("resources/spring.xml");
String value = ap.getMessage("label",null,locale);
%>

<form action="./hello">
<%= value %> :<input type="text" name="name" />
<input type="submit" value="submit">
</form>

我的控制台中出现如下错误:

Dec 09, 2017 10:45:49 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_151\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_151/bin/server;C:/Program Files/Java/jre1.8.0_151/bin;C:/Program Files/Java/jre1.8.0_151/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\oraclexe\app\oracle\product\11.2.0\server\bin;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Java\jdk1.8.0_73\bin;C:\apache-ant-1.9.6-bin\apache-ant-1.9.6\bin;C:\WINDOWS\system32\config\systemprofile\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files (x86)\Skype\Phone\;C:\apache-opennlp-1.7.2\bin;C:\Program Files\MySQL\MySQL Utilities 1.6\;%JAVA_HOME%\bin;%HADOOP_HOME%\bin;%HADOOP_BIN_PATH%;C:\Users\Nithin\Anaconda3;C:\Users\Nithin\Anaconda3\Scripts;C:\Users\Nithin\Anaconda3\Library\bin;C:\Users\Nithin\AppData\Local\Microsoft\WindowsApps;;C:\eclipse-jee-neon-2-win32-x86_64\eclipse;;.
Dec 09, 2017 10:45:50 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Spring1_IOC7_I18N' did not find a matching property.
Dec 09, 2017 10:45:53 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-80"]
Dec 09, 2017 10:45:54 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Dec 09, 2017 10:45:54 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 5091 ms
Dec 09, 2017 10:45:54 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Dec 09, 2017 10:45:54 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.34
Dec 09, 2017 10:46:01 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [3,560] milliseconds.
Dec 09, 2017 10:46:03 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-80"]
Dec 09, 2017 10:46:03 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Dec 09, 2017 10:46:03 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 9342 ms
Dec 09, 2017 10:46:19 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1a35458e: startup date [Sat Dec 09 22:46:19 IST 2017]; root of context hierarchy
Dec 09, 2017 10:46:20 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [resources/spring.xml]
Dec 09, 2017 10:46:20 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@61e5c0d: defining beans [messageSource]; root of factory hierarchy
Dec 09, 2017 10:46:20 PM org.springframework.context.support.ResourceBundleMessageSource getResourceBundle
WARNING: ResourceBundle [resources/Data] not found for MessageSource: Can't find bundle for base name resources/Data, locale en-us,en-in;q=0.7,en;q=0.3
Dec 09, 2017 10:46:20 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Spring1_IOC7_I18N] threw exception [An exception occurred processing JSP page /index.jsp at line 11

8: String lang= request.getHeader("accept-language");
9: Locale locale = new Locale(lang);
10: ApplicationContext ap = new ClassPathXmlApplicationContext("resources/spring.xml");
11: String value = ap.getMessage("label",null,locale);
12: %>
13:
14: <form action="./hello">


Stacktrace:] with root cause
org.springframework.context.NoSuchMessageException: No message found under code 'label' for locale 'en-us,en-in;q=0.7,en;q=0.3'.
at org.springframework.context.support.AbstractMessageSource.getMessage(AbstractMessageSource.java:135)
at org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:1161)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:75)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
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:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

最佳答案

Locale locale = new Locale("lang");

应该是

Locale locale = new Locale(lang);

不带引号

关于java - I18N 属性上的 org.springframework.context.NoSuchMessageException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47730803/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com