Struts2.0中获取项目的上下文的两种方式
2010-11-05 14:22
309 查看
方法一:
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);
}
}
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);
}
}
相关文章推荐
- spring boot 集成quartz 2.0 实现前端动态配置(获取spring上下文)的两种方式,启动数据库中已开启定时任务
- spring boot 集成quartz 2.0 实现前端动态配置(获取spring上下文)的两种方式,启动数据库中已开启定时任务
- xml文件解析类及Android项目中常用到的两种获取xml文件方式
- 获取项目部署名称的两种方式
- 批量修改Texture属性 两种方式加载贴图,通过Resources.LoadAll Directoy 获取项目路径:Application.dataPath; using UnityEn
- struts 2.0中获取当前action 的上下文对象
- Struts 获取HttpServletRequest和HttpServletResponse的两种方式
- 在web项目中获取ApplicationContext上下文的3种主要方式及适用情况
- 【Struts2七】struts2获取表单元素的两种方式
- XZ_CoreMotion框架获取数据的两种方式
- 两种版本实现C#获取快捷方式目标路径的代码
- SQL语句中插入标识列获取标识ID的两种方式
- Struts2.0中怎样获取Session
- jQuery 获取DOM节点的两种方式
- EJB3.0获取JNDI上下文的两种方法
- [Android]获取拍照完的图片的数据两种方式
- C#两种方式获取指定文件夹下所有子目录及文件
- 基于Maven的SpringBoot项目实现热部署的两种方式
- java配置文件读取和项目路径的多种获取方式
- (转载)Android两种Tab分页的方式:TabActivity和ActivityGroup以及Android项目几种常见的应用架构