【问题标题】:How to get ContextPath in struts2 action?如何在 struts2 动作中获取 ContextPath?
【发布时间】:2012-02-21 04:17:38
【问题描述】:

我在我的项目中使用 struts2。必须在我的 JSP 操作中生成图像并将其存储在 war 文件路径中。为此,我必须在 Struts2 中获取 Contextpath。那么如何获取WAR文件路径来存储WAR文件下指定文件夹中的图片/文件呢?

谁能帮我在struts2中找到上下文路径。提前致谢。

【问题讨论】:

    标签: java struts2


    【解决方案1】:

    使用下面的代码...

    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;  
        }
    }
    

    【讨论】:

    • 非常感谢您的回复!这对我找到解决方案帮助很大。
    • 如何从struts2action获取特定java包的类列表?我正在尝试使用 getContextPath() 但遇到异常。
    【解决方案2】:
    String path = request.getRealPath("/");
    

    此代码将起作用

    【讨论】:

    • getRealPath() on HttpServletRequest 现已弃用。请改用 request.getServletContext().getRealPath("/")。
    猜你喜欢
    • 2011-10-10
    • 1970-01-01
    • 2011-04-27
    • 2011-06-19
    • 2012-12-02
    • 2015-02-19
    • 2014-08-25
    • 1970-01-01
    • 2013-12-06
    相关资源
    最近更新 更多