我有以下文件结构:
projectname/war/i18n/strings.properties
projectname/war/i18n/strings_de.properties
projectname/war/i18n/strings_fr.properties
projectname/war/i18n/strings_es.properties
projectname/war/i18n/strings_it.properties
所以默认是英文。为了访问这些文件,我执行以下操作:
public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
{
String lang = request.getParameter("lang");
Locale locale = null;
if( lang.equals("DE"))
locale = new Locale("de");
else if( lang.equals("FR"))
locale = new Locale("fr");
else if( lang.equals("IT"))
locale = new Locale("it");
else if( lang.equals("ES"))
locale = new Locale("es");
URL[] urls = {new File("i18n/").toURI().toURL()};
ClassLoader loader = new URLClassLoader(urls);
ResourceBundle strings = ResourceBundle.getBundle("strings", locale, loader );
request.setAttribute("strings", strings );
但是,在运行此代码时,我得到
java.lang.NullPointerException
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1252)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:1028)
at de.projectname.server.pages.MyServlet.doGet(MyServlet.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:513)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
为什么?
编辑:这也不起作用:
locale = null;
ResourceBundle strings = ResourceBundle.getBundle("strings", locale );
同样的错误。