`
longgangbai
  • 浏览: 7254324 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Struts2.0中获取项目的上下文的两种方式

阅读更多

  Struts2.0中获取项目的上下文的两种方式

 

方法一:

  StringBuffer sb=new StringBuffer();
  sb.append(File.separator+"images"+File.separator+"vacationImage");
  String path=ServletActionContext.getServletContext().getRealPath(sb.toString());

 

方法二:

通过RequestUtils获取项目的上下文的信息:

 

String path=RequestUtils.getServletPath(ServletActionContext.getRequest());

源代码如下:

public class RequestUtils {

    /**
     * Retrieves the current request servlet path.
     * Deals with differences between servlet specs (2.2 vs 2.3+)
     *
     * @param request the request
     * @return the servlet path
     */
    public static String getServletPath(HttpServletRequest request) {
        String servletPath = request.getServletPath();
       
        String requestUri = request.getRequestURI();
        // Detecting other characters that the servlet container cut off (like anything after ';')
        if (requestUri != null && servletPath != null && !requestUri.endsWith(servletPath)) {
            int pos = requestUri.indexOf(servletPath);
            if (pos > -1) {
                servletPath = requestUri.substring(requestUri.indexOf(servletPath));
            }
        }
       
        if (null != servletPath && !"".equals(servletPath)) {
            return servletPath;
        }
       
        int startIndex = request.getContextPath().equals("") ? 0 : request.getContextPath().length();
        int endIndex = request.getPathInfo() == null ? requestUri.length() : requestUri.lastIndexOf(request.getPathInfo());

        if (startIndex > endIndex) { // this should not happen
            endIndex = startIndex;
        }

        return requestUri.substring(startIndex, endIndex);
    }

}

 

 

分享到:
评论

相关推荐

    struts2.0中文教程

    05 转换器(Converter)——Struts 2.0中的魔术师 06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts...

    Struts2.0视频教程+struts2.0中文教程

    Struts2.0视频教程,struts2.0中文教程,Struts2.0视频教程,struts2.0中文教程,

    struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0

    struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0

    Struts 2.0系列(MAX)

    转换器(Converter)——Struts 2.0中的魔术师 在Struts 2.0中实现表单数据校验(Validation) Struts 2的基石——拦截器(Interceptor) 在Struts 2中实现IoC 在Struts 2中实现文件上传 在Struts 2中实现CRUD ...

    sstruts2.0 struts2.0

    sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0

    struts2.0jar包

    struts2.0jar包 struts2.0包 struts2.0源文件

    struts2.0的数据校验框架struts2.0的数据校验框架

    struts2.0的数据校验框架struts2.0的数据校验框架struts2.0的数据校验框架struts2.0的数据校验框架

    JavaEE源代码 Struts2.0

    JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0...

    struts 2.0 详细配置

    struts 2.0 详细配置 struts 2.0 详细配置 struts 2.0 详细配置

    Struts2.0 Jar包

    此为Struts2.0最新Jar包,方便各位用于Struts2.0的开发.

    Struts2.0中文教程

    Struts2.0中文教程Struts2.0中文教程Struts2.0中文教程Struts2.0中文教程Struts2.0中文教程Struts2.0中文教程

    Struts 2.0

    清晰的介绍了Struts 2.0框架的工作流程,Action线程安全,程序入口,配置文件。

    Struts2.0中文教程权威版

    05 转换器(Converter)——Struts 2.0中的魔术师 06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts...

    Struts2.0学习Struts2.0文档

    为Struts 2.0做好准备 Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到用广泛的应用。作为最成功的Web框架,Struts自然拥有众多的优点: MVC 2模型的使用 功能齐全的标志库(Tag Library) 开放...

    struts2.0入门案例

    struts2.0 入门案例、简单的struts2.0入门案例 2.0配置包,基础

    struts2.0的特点

    struts2.0的特点

Global site tag (gtag.js) - Google Analytics