【发布时间】:2011-03-25 14:37:39
【问题描述】:
也许标题有点晦涩,但让我解释一下......
String path = getServletContext.getRealPath("/");
问题是你必须在一个 servlet 中。现在我想将此路径传递给一个通用 bean,它连接在我的 servlet-context.xml 中。该怎么做?
【问题讨论】:
标签: spring servlets configuration
也许标题有点晦涩,但让我解释一下......
String path = getServletContext.getRealPath("/");
问题是你必须在一个 servlet 中。现在我想将此路径传递给一个通用 bean,它连接在我的 servlet-context.xml 中。该怎么做?
【问题讨论】:
标签: spring servlets configuration
您的 bean 可以请求注入当前的 ServletContext,方法是实现 ServletContextAware 接口,或者使用自动装配,即
private @Autowired ServletContext servletContext;
使用最适合您的代码和配置的那个。
您也可以考虑使用 Spring 的 ServletContextResource 类对 ServletContext 执行文件系统访问,例如使用ServletContextResource.getFile()。
【讨论】: