java类中获取WEB-INF路径】的更多相关文章

在java web项目中获取项目的src/main/resource下的文件路径 当前类名.class.getClassLoader().getResource("/").getPath(); 比如我的当前类名叫Demo 那么: Demo.class.getClassLoader().getResource("/").getPath();…
起因是我想要获取一个相对路径,需要用到servletContext的getRealPath()方法,于是上网搜索,找到两种方法来获取ServletContext. 方法1:第一种方法是这样的: ServletActionContext.getServletContext(): 或者ServletContext servletContext= req.getServletContext();其中req是一个HttpServletRequest对象. 上述两种方法第一种局限于Struts中使用,且两…
//从spring容易中获取bean public static Object getBean(String beanName){ ApplicationContext context = ContextLoader.getCurrentWebApplicationContext(); return context.getBean(beanName); } //获取ServletContext public static ServletContext getServletContext(){ W…
WEB-INF路径 String path = WsTestBOImpl.class.getClass().getResource("/").getPath(); path = path.substring(1, path.indexOf("classes")); web-inf 下面的classses路径(也就是src路径) String path = WsTestBOImpl.class.getClass().getResource("/")…
一.java获取web工程路径 1),在servlet可以用一下方法取得: request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/”) ”//upload//”; 2),不从jsp,或servlet中获取,只从普通java类中获取: String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); SAXReader()…
在普通的Java类中获取service接口目的是调用接口中的方法,实现数据的持久化等操作: Java类中的获取service接口方法: IfaceDetectService faceDetectService = (IFaceDetectService)SpringUtil.getObject("faceDetectService"); 注意:括号中的“faceDetectService”一定要与serviceimpl上注入的一致. SpringUtil类: public static…
JAVA文件中获取路径及WEB应用程序获取路径方法 1. 基本概念的理解 `绝对路径`:你应用上的文件或目录在硬盘上真正的路径,如:URL.物理路径 例如: c:/xyz/test.txt代表了test.txt文件的绝对路径: http://www.sun.com/index.htm也代表了一个URL绝对路径: `相对路径`:相对与某个基准目录的路径,包含Web的相对路径(HTML中的相对目录). 例如: 在Servlet中,"/"代表Web应用的根目录,和物理路径的相对表示. 例如:…
不使用Spring,怎样能在Listener启动的Thread中获取web目录,还真不完全确定.其实我觉得实际代码也很简单.就是基于普通的listener,然后在listener中获取web目录并放到JRE全局变量中. 但使用Spring,就可以用一种比较优雅的方式来获取了. 在web.xml中的<web-app>节点内加入: <context-param> <param-name>webAppRootKey</param-name> <param-v…
引言 在web项目开发过程中,可能会经常遇到要获取项目根路径的情况,那接下来我就总结一下,java中获取项目根路径的7种方法,主要是通过thisClass和System,线程和request等方法. (1):this.getClass().getResource("/"): (2):file.getCanonicalPath(): (3):this.getClass().getClassLoader(): (4):System.getProperty("user.dir&qu…
在使用spring时,经常需要在普通类中获取session,request等对像. 1.第一钟方式,针对Spring和Struts2集成的项目: 在有使用struts2时,因为struts2有一个接口使用org.apache.struts2.ServletActionContext即可很方便的取到session对像.用法: ServletActionContext.getRequest().getSession(); 例如: // 整合了Struts,所有用这种方式获取session中属性(亲测…