在 Struts 操作类中如何获取应用程序路径?
我想从 webcontent\applicationName\Config\myxml.xml
读取 XML 文件谢谢。
最佳答案
我建议不要将该文件保留在该位置,因为这样就可以使用 http://yourURl/applicationContext/Config/myxml.xml
轻松公开访问该文件。我认为您应该将其移至 WEB-INF
文件夹下。
您应该能够执行如下所示的操作(快速的动态代码不会让我坚持下去):-
public ActionForward foo(HttpServletRequest request, HttpServletResponse response){
ServletContext context = request.getSession().getServletConext();
InputStream stream = context.getResourceAsStream("Config/myxml.xml");
if(stream!=null){
//load your XML file from the stream
}
}
如果您使用的是 struts2,那么您可以使用 ServletActionContext 访问 ServletContext还有:-
ServletContext context = ServletActionContext.getServletContext();
关于java - Weblogic服务器中的应用程序路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7053717/