• Struts2学习第六课 实现登录登出功能


    关于Struts2请求的扩展名问题:

    1).org.apache.struts2包下的default.properties中配置了struts2应用的一些常量

    2).struts.action.extension定义了当前struts2应用可以接受的请求的扩展名。

    3).可以在struts.xml文件中以常量配置的方式修改default.properties所配置的常量。

    <constant name="struts.action.extension" value="action,do,"></constant>

    直接看代码:

    package logan.struts.study;
    
    import java.util.Map;
    
    import org.apache.struts2.dispatcher.SessionMap;
    import org.apache.struts2.interceptor.ApplicationAware;
    import org.apache.struts2.interceptor.SessionAware;
    
    public class UserAction implements SessionAware,ApplicationAware{
        
        private Map<String, Object> session = null;
        private Map<String, Object> application = null;
        
        private String username;
        
        public void setUsername(String username) {
            this.username = username;
        }
        
        public String logout(){
            //数量减一
            Integer count = (Integer) application.get("count");
            if(count != null && count >0){
                count--;
                application.put("count", count);
            }
            count--;
            
            //session失效
            ((SessionMap)session).invalidate();
            
            return "logout-success";
        }
        
        public String execute(){
            
            //把用户信息存入Session域中
            
            //1.获取session,通过实现RequestAware接口
            
            //获取登录信息
            session.put("username", username);
            //把用户信息存入Session域中
            
            //
            Integer count = (Integer) application.get("count");
            if(count == null){
                count = 0;
            }
            //2.使当前在线人数 + 1
            count++;
            application.put("count", count);
            
            
            return "login-success";
        }
    
        @Override
        public void setSession(Map<String, Object> session) {
            // TODO Auto-generated method stub
            this.session = session;
            
        }
    
        @Override
        public void setApplication(Map<String, Object> application) {
            // TODO Auto-generated method stub
            this.application = application;
            
        }
    
    }

    struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
        <!-- 配置Struts2可以受理的请求的扩展名 -->
        <constant name="struts.action.extension" value="action,do"></constant>
        <package name="default" namespace="/" extends="struts-default">
            <action name="login-ui">
                <result>/login.jsp</result>
            </action>
            
            <action name="user-login" class="logan.struts.study.UserAction">
                <result name="login-success">/login-success.jsp</result>
            </action>
            
            <action name="logout" class="logan.struts.study.UserAction"
            method="logout">
                <result name="logout-success">/login.jsp</result>
            </action>
        
        </package>
        
    </struts>

    web.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
        <!-- 配置Struts2可以受理的请求的扩展名 -->
        <constant name="struts.action.extension" value="action,do"></constant>
        <package name="default" namespace="/" extends="struts-default">
            <action name="login-ui">
                <result>/login.jsp</result>
            </action>
            
            <action name="user-login" class="logan.struts.study.UserAction">
                <result name="login-success">/login-success.jsp</result>
            </action>
            
            <action name="logout" class="logan.struts.study.UserAction"
            method="logout">
                <result name="logout-success">/login.jsp</result>
            </action>
        
        </package>
        
    </struts>

    index.jsp

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
        <!-- 配置Struts2可以受理的请求的扩展名 -->
        <constant name="struts.action.extension" value="action,do"></constant>
        <package name="default" namespace="/" extends="struts-default">
            <action name="login-ui">
                <result>/login.jsp</result>
            </action>
            
            <action name="user-login" class="logan.struts.study.UserAction">
                <result name="login-success">/login-success.jsp</result>
            </action>
            
            <action name="logout" class="logan.struts.study.UserAction"
            method="logout">
                <result name="logout-success">/login.jsp</result>
            </action>
        
        </package>
        
    </struts>

    login-success.jsp

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
        <!-- 配置Struts2可以受理的请求的扩展名 -->
        <constant name="struts.action.extension" value="action,do"></constant>
        <package name="default" namespace="/" extends="struts-default">
            <action name="login-ui">
                <result>/login.jsp</result>
            </action>
            
            <action name="user-login" class="logan.struts.study.UserAction">
                <result name="login-success">/login-success.jsp</result>
            </action>
            
            <action name="logout" class="logan.struts.study.UserAction"
            method="logout">
                <result name="logout-success">/login.jsp</result>
            </action>
        
        </package>
        
    </struts>

    login.jsp

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
        <!-- 配置Struts2可以受理的请求的扩展名 -->
        <constant name="struts.action.extension" value="action,do"></constant>
        <package name="default" namespace="/" extends="struts-default">
            <action name="login-ui">
                <result>/login.jsp</result>
            </action>
            
            <action name="user-login" class="logan.struts.study.UserAction">
                <result name="login-success">/login-success.jsp</result>
            </action>
            
            <action name="logout" class="logan.struts.study.UserAction"
            method="logout">
                <result name="logout-success">/login.jsp</result>
            </action>
        
        </package>
        
    </struts>

    实现了简单的登录登出功能。

  • 相关阅读:
    2-Requests库的使用
    1-urllib库的使用
    (一)数据结构-基本数学知识
    maven配置阿里云仓库
    mac安装homebrew
    创建简单spring boot项目
    Java反射
    Python3 去除空格
    Spot 安装和使用
    安装LLVM
  • 原文地址:https://www.cnblogs.com/LoganChen/p/6972751.html
Copyright © 2020-2023  润新知