Javabean如何得到当前的web路径。 JavaBean都没有上下文,从哪里获得web路径? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 JSP,Servlet在Web容器中运行,允许用户访问,可以得到上下文,顾可以的到web目录。javabean在不在Web容器中运行,也就是说不允许Web访问,也就没有web目录这一概念,你怎么得到web目录?你可以得到的是package路径。 小弟办法愚笨 但是能解决问题 public String getRealPath(){ String path1 = this.getClass().getResource("/").getPath(); String path2 = path1.substring(1 , path1.indexOf("/classes")); String realpath = path2 //System.out.println(path2); return realpath; } 监测 是否在 Web 中运行.是的话,就用上面的例子. 把 webapp 的路径写在一个配置文件里面,就可以了, 用这一句就可以了String path = System.getProperty("user.dir");path就是你的web路径 在bean中加参数例如getPath(HttpServletRequest request)就可以调用config.getServletContext().getRealPath("/"); 被抢先了,传入一个request就可以了啊 再bean里面我们一般是用把路径放在系统固定的参数里面例如再rdas.properties里面加IMPORT_PATH = /url/ 没有 Context 就让给一个 Request 参数,或者全局配置监听器启动是把一些只有处于 Context 才能访问到的全局参数放入一个 static 域中, 其他的 JavaBean 只要检测一个 ServletContext initialized 标记,检测到了就表示 WebApp 已经启动了,可以取 static 变量,而且它应该是有效的,即使是 null 。public class ConfigListener implements ServletContextListener {private ServletContext context = null; private static boolean initialized = false; public void contextInitialized(ServletContextEvent event) { context = event.getServletContext();initialized = true; } public void contextDestroyed(ServletContextEvent event) { context = event.getServletContext(); } public static ServletContext getServletContext(){ if( ! initialized ) throw new IllegalStateException(" ServletContext not initialized."); } public static boolean isServletContextInitialized(){ return initialized ; }}# web.xml<web-app> <listener> <listener-class>ConfigListener </listener> </listener><!-- other entries --></web-app> // 少写了一句。public static ServletContext getServletContext(){ if( ! initialized ) throw new IllegalStateException(" ServletContext not initialized."); return context; } 关于jsp页面注册,用jquery验证的几点问题? 关于Struts2上传文件的问题 用POI根据WORD模板生成WORD jsp中文乱码 还是编码问题!(乱码了) 紧急求助--可能是session问题!!!!!! 近来看看啊~ 急!!!!java的环配置问题! Myeclipse10 连接sql server 2000 一直出现红色感叹号 hibernate 如何自动生成非主键字段值? 谁能具体的说一下中间层是干什么 急求jsp中如何存取二进制文件(如AutoCAD图)?解决了马上结账!!
你可以得到的是package路径。
String path1 = this.getClass().getResource("/").getPath();
String path2 = path1.substring(1 , path1.indexOf("/classes"));
String realpath = path2
//System.out.println(path2);
return realpath;
}
是的话,就用上面的例子.
String path = System.getProperty("user.dir");
path就是你的web路径
例如
getPath(HttpServletRequest request)
就可以调用
config.getServletContext().getRealPath("/");
例如再rdas.properties里面加IMPORT_PATH = /url/
private ServletContext context = null;
private static boolean initialized = false;
public void contextInitialized(ServletContextEvent event)
{
context = event.getServletContext();
initialized = true;
} public void contextDestroyed(ServletContextEvent event)
{
context = event.getServletContext();
} public static ServletContext getServletContext(){
if( ! initialized )
throw new IllegalStateException(" ServletContext not initialized.");
}
public static boolean isServletContextInitialized(){
return initialized ;
}
}# web.xml<web-app>
<listener>
<listener-class>ConfigListener </listener> </listener><!-- other entries --></web-app>
public static ServletContext getServletContext(){
if( ! initialized )
throw new IllegalStateException(" ServletContext not initialized."); return context;
}