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
    欢迎任何形式的转载,但请务必注明出处。
    分享到:
  • 相关阅读:
    【转】GitHub 中国区前 100 名到底是什么样的人?
    不同服务器数据库之间的数据操作
    行列互换
    千万级数据查询
    用命令对sql进行备份
    通过SQL Server 2008数据库复制实现数据库同步备份
    各种字符串合并处理示例.
    字符串分解
    四大排序函数
    cross apply 和 outer apply
  • 原文地址:https://www.cnblogs.com/xwdreamer/p/2297083.html
Copyright © 2011-2022 走看看