struts2中取得web项目的根文件夹


struts2中取得web项目的根文件夹


        /**
	  * 获得web项目根文件夹
	  */
	public String getWebRootPath() throws Exception {
		ActionContext actionContext = ActionContext.getContext();
		ServletContext servletContext = (ServletContext)actionContext.get(ServletActionContext.SERVLET_CONTEXT);
		String rootPath = servletContext.getRealPath("/");
		return rootPath;
	}


在项目中使用了该段代码。可能出现错误,原因是还没有导入对应的包。以上的代码须要导入的包有: 


import javax.servlet.ServletContext;


import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;



在别的程序中调用getWebRootPath()方法就可以得到web项目的根文件夹


  

posted @ 2017-06-14 08:18  jzdwajue  阅读(96)  评论(0编辑  收藏  举报