• 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
    欢迎任何形式的转载,但请务必注明出处。
    分享到:
  • 相关阅读:
    算法之--回溯法-迷宫问题【python实现】
    awk积累
    mysql自动化安装脚本(二进制安装)
    ${FUNCNAME[@]}和$LINENO使用
    shell脚本配置ssh免密登陆
    /etc/passwd和/etc/group文件详解
    Bagging与随机森林算法原理小结
    js之如何获取css样式
    Jetty源码学习-编译Jetty源码二三事
    maven安装和与IDE集成
  • 原文地址:https://www.cnblogs.com/xwdreamer/p/2297083.html
Copyright © 2020-2023  润新知