如题。通过ServletActionContext以及ActionContext都行不通。因为不走action 想要直接在service中获取项目的路径。。请问有没有什么方法。。求指教

解决方案 »

  1.   

    突然想起来一种笨方法String rootPath =this.getClass().getClassLoader().getResource(".").getPath();  这是获取类的所在路径。。然后截。尴尬
      

  2.   

    项目跟路径?试试${pageContext.request.contextPath}
      

  3.   

    (1)、request.getRealPath("/");//不推荐使用获取工程的根路径
    (2)、request.getRealPath(request.getRequestURI());//获取jsp的路径,这个方法比较好用,可以直接在servlet和jsp中使用
    (3)、request.getSession().getServletContext().getRealPath("/");//获取工程的根路径,这个方法比较好用,可以直接在servlet和jsp中使用
    (4)、 this.getClass().getClassLoader().getResource("").getPath();//获取工程classes 下的路径,这个方法可以在任意jsp,servlet,java文件中使用,因为不管是jsp,servlet其实都是java程序,都是一个 class。所以它应该是一个通用的方法
      

  4.   

    非action下获取request  这个是获取目录前置
    ActionContext ac = ActionContext.getContext();
    HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);