1、web.xml配置文件
<context-param> <param-name>messagePrefix</param-name> <param-value>需要在tomcat启动时获取的字符串</param-value> </context-param>
2、普通java类
1 import org.springframework.web.context.ContextLoader; 2 import org.springframework.web.context.WebApplicationContext; 3 4 public class WebConfig{ 5 6 /** 7 * 获取web文件中配置的参数 8 * paramName参数名称 9 * */ 10 public static String getWebConfig(String paramName){ 11 WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); 12 String param = ""; 13 param = wac.getServletContext().getInitParameter(paramName); 14 if(param==null||"".equals(param)||"null".equals(param)){ 15 param=""; 16 } 17 return param; 18 } 19 20 }
普通java类在Tomcat启动时获取ServletContext
时间: 2024-10-13 12:05:14