zoukankan      html  css  js  c++  java
  • ServletActionContext.getRequest().getServletPath()错误的解决方法

    BaseAction

    package control.util;
    import org.apache.struts2.ServletActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ActionContext;
    public class BaseAction extends ActionSupport {
        private static final long serialVersionUID = 1L;
        // 取得当前登录的用户名
        protected String getLoginUsername() {
            return (String) ActionContext.getContext().getSession().get(
                    Constants.USERNAME_KEY);
        }
        // 判断当前用户是否超时
        protected boolean isTimeout() {
            if (ActionContext.getContext().getSession().get(Constants.USERNAME_KEY) == null) {
                return true;
            } else {
                return false;
            }
        }
        // 检查Session对象是否存在
        protected boolean isExistSession(String key) {
            if (ActionContext.getContext().getSession().get(key) != null) {
                return true;
            } else {
                return false;
            }
        }
        // 保存Session对象 
        @SuppressWarnings("unchecked")
        protected void setSession(Object key, Object obj) {
            ActionContext.getContext().getSession().put(key, obj);
        }
        // 取得Session对象
        protected Object getSession(String key) {
            return ActionContext.getContext().getSession().get(key);
        }
        // 保存一条错误
        protected void saveActionError(String key) {
            super.addActionError(super.getText(key));
        }
        // 保存一个消息
        protected void saveActionMessage(String key) {
            super.addActionMessage(super.getText(key));
        }
        // 取得查询的URL
        protected String getRequestPath() {        
            return (String) ServletActionContext.getRequest().getServletPath();
        }
    }
    在BaseAction中调用ServletActionContext.getRequest().getServletPath()显示错误。
    ServletActionContext.getRequest().getServletPath();是在%tomcatHome%/lib/servlet-api.jar中,而lib中没有添加servlet-api.jar,所以会显示错误。
    解决方法:在lib中添加servlet-api.jar,或者添加Library中引用servlet-api.jar。
    作者:xwdreamer
    欢迎任何形式的转载,但请务必注明出处。
    分享到:
  • 相关阅读:
    memcached学习笔记——存储命令源码分析上篇
    个人理解正则表达式——懒惰匹配
    Symfony2中的设计模式——装饰者模式
    Symfony2源码分析——启动过程2
    Symfony2 HttpKernel事件驱动
    memcached学习笔记——连接模型
    阅读优秀代码是提高开发人员修为的一种捷径
    Linux下部署Symfony2对app/cache和app/logs目录的权限设置
    Symfony2源码分析——启动过程1
    Symfony2 EventDispatcher组件
  • 原文地址:https://www.cnblogs.com/xwdreamer/p/2297083.html
Copyright © 2011-2022 走看看