刚接触struts,按照书上的例子写了一个web应用,但是启动了tomcat服务器后却发现总是出现找不到resource
bundle的错误。经过反复核查,也修改了不少东西,包括一点马虎以及注意resource在包中的存放位置(在/WEB-
INF/classes下),但最终仍然无法解决这个问题,实在令人挠头。后来绝望时在web.xml配置文件中在配置action
时在<init-param>后面添加了<load-on-startup>1<load-on-startup>,问题竟然解决了!
个人理解是resource要在WEB应用启动时由WEB服务器加载(读取配置文件)到内存中,并将它们放在config
包中相关JavaBean类的实例中(<message-resoureces>和MessageResourcesConfig类相对应),但是由于没
有设置加载的优先级,导致了resource并没有被预先加载,因而出现了上述错误,因此,ActionServlet应该在启动
WEB应用前进行加载。
后来在servlet-2_4-spec中查到了关于<load-on-startup>的说明:
“The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application. The optional contents of these element must be an integer indicating the order in which the servlet should be loaded. If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses. If the value is a positive 128 integer or 0, the container must load and initialize the servlet as the application is deployed”
<load-on-startup>的内容是一个整数,代表了被加载时的优先级别。如果这个整数是负数,那么servlet容器可
以随时加载这个servlet而不受任何限制。