2012-02-21 40 views
3

我在我的項目中使用struts2。必須在我的JSP動作中生成映像並將其存儲在war文件路徑中。爲此,我必須在Struts2中獲取Contextpath。那麼如何獲取WAR文件路徑來將映像/文件存儲在WAR文件下的指定文件夾中?如何在struts2 action中獲取ContextPath?

任何人都可以幫助我找到struts2中的Contextpath。提前致謝。

回答

8

使用下面的代碼...

public class Page1 extends ActionSupport implements ServletRequestAware { 
    public HttpServletRequest request; 

    @Override 
    public String execute() { 
    String contextPath = request.getContextPath(); 
    System.out.println("Context Path " + contextPath); 
    return SUCCESS; 
    } 

    @Override 
    public void setServletRequest(HttpServletRequest request) { 
    this.request = request; 
    } 
} 
+0

非常感謝您的回覆!它幫助我找到了解決方案。 – shiva 2012-02-21 06:06:06

+1

如何從struts2action獲取特定java包的類的列表?我嘗試使用getContextPath()但獲取異常。 – 2012-07-18 11:14:10

1
String path = request.getRealPath("/"); 

這個代碼將工作

+0

HttpServletRequest上的getRealPath()現在已被棄用。改爲使用request.getServletContext()。getRealPath(「/」)。 – Wextux 2013-11-27 19:20:26

相關問題