Struts2+DAO层实现实例03——添加监听器跟踪用户行为

实例说明

  根据上两次的成品进行二次加工。

  加入Listener,监听用户的登陆注销情况。

所用知识说明

  采用SessionBindingListener对Session进行监听。

  同时,Action中获取Application,Session,request的方法(在此只列出一种)更多方法

    public class LoginAction {
        private Map request;
        private Map session;
        private Map application;  

        public String execute() {
            request = (Map)ActionContext.getContext().get("request");
            session = ActionContext.getContext().getSession();
            application = ActionContext.getContext().getApplication();
            request.put("username1", "jingjing1");
            session.put("username2", "jingjing2");
            application.put("username3", "jingjing3");
            return "success";
        }
    }  

代码实例

  登陆控制UserManagment

package UserBlock;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import javafx.application.Application;
import org.apache.struts2.views.util.ContextUtil;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.util.ArrayList;
import java.util.Map;

/**
 * Servlet监听器,控制记录用户的登陆注销信息
 * Created by Richard on 2017/6/16.
 */
public class UserManagment {
    class Userlistener implements HttpSessionBindingListener{
        private  String username;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        @Override
        public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
                ActionContext context = ActionContext.getContext();
                Map application = context.getApplication();
                ArrayList online= (ArrayList) application.get("online");
            if(online==null){
                    online=new ArrayList();
                }
            online.add(username);
            application.put("online",online);
        }

        @Override
        public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
            ActionContext context = ActionContext.getContext();
            Map application = context.getApplication();
            ArrayList online= (ArrayList) application.get("online");
            online.remove(username);
        }

    }

    /*
            登陆
                首先判别是否登陆
                已经登陆---->return
                没有登陆---->获取对应的Session,存入对应用户名的listener
     */

    public void login(String username){
        if(islogin(username)){
            return;
        }else{
            Userlistener newUser=new Userlistener();
            newUser.setUsername(username);
            ActionContext actionContext=ActionContext.getContext();
            Map session=actionContext.getSession();
            session.put("username",newUser);
        }

    }

    /*
            判断是否登陆:
                判别条件Session中是否有对应的该用户名的Listener
                有--->已经登陆,未注销
                无--->没有登陆
     */
    public  boolean islogin(String username){
        ActionContext actionContext=ActionContext.getContext();
        Map session=actionContext.getSession();
        Userlistener judge= (Userlistener) session.get("username");
        if(judge!=null){
            return true;
        }else {
            return false;
        }
    }

    /*
              注销
                    首先判断是否登陆
                    已经登陆--->移除Listener--->true
                    没有登陆--->false

     */

    public boolean logoff(String username){
        if(islogin(username)){
            ActionContext actionContext=ActionContext.getContext();
            Map session=actionContext.getSession();
            session.remove(username);
            return true;
        }else {
            return false;
        }
    }
    /*
            人数统计
                 返回Session中List的Size。
     */

    public int returnNum(){
        ActionContext actionContext=ActionContext.getContext();
        Map session=actionContext.getSession();
        ArrayList online= (ArrayList) session.get("online");
        if(online==null){
            online=new ArrayList();
        }
        return online.size();
    }

    /*
            list返回
     */
    public ArrayList  returnlist(){
        ActionContext actionContext=ActionContext.getContext();
        Map session=actionContext.getSession();
        ArrayList online= (ArrayList) session.get("online");
        if(online==null){
            online=new ArrayList();
        }
        return online;
    }
}

主页in.jsp

<%--
  Created by IntelliJ IDEA.
  User: Richard
  Date: 2017/6/16
  Time: 21:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Welcome</title>
</head>
<body>
<h1>WelCome to My Struts Page</h1>
<hr>
当前登录的用户:${param.username}<br>
<hr>
当前所有登陆的用户:
<table border=1 width=200>
    <s:iterator value="#application.online" var="user">
        <tr <s:if test="#user.odd"> style="background-color: dimgray"  </s:if> >
            <td><s:property value="#user.count"></s:property> </td>
            <td><s:property value="#user"></s:property></td>
        </tr>
    </s:iterator>

</table>

<a href="login.jsp">注销</a>
</body>
</html>

Action:

package UserBlock;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.util.ArrayList;
import java.util.Map;
import java.util.logging.LogManager;

/**
 * Created by Richard on 2017/6/16.
 * 继承ActionSupport实现一个Action
 * 登陆界面通过loginuser.action调用login()方法来处理数据
 *          login()方法中通过ActionContext调用Session对象,存入输入错误的数据
 *          通过addActionMessage()方法向页面输出相关信息
 * 注册页面通过reguser.action调用reg()方法来处理数据
 */
public class UserAction extends ActionSupport {
    private String INDEX="index";
    private String LOGIN="login";
    private  String REG="register";
    private String username;
    private String password;
    private String compassword;
    private UserDao user;
    private UserManagment managment;

    public String getCompassword() {
        return compassword;
    }

    public void setCompassword(String compassword) {
        this.compassword = compassword;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String login(){
        try{
            managment=new UserManagment();
            user=new UserDao();
            ArrayList result=user.up_select(username);
            if(result.size()>0){
                User aim= (User) result.get(0);
                if(aim.getPassword().equals(password)){
                    /*登陆成功*/
                    managment.login(username);
                    return INDEX;
                }else{
                    ActionContext applicton=ActionContext.getContext();
                    Map session=applicton.getSession();
                    int count;
                    if(session.get("count")==null){
                        count=0;
                    }else{
                        count= (int) session.get("count");
                    }
                    if(count>=3){
                        addActionMessage("错误次数过多");
                        count=0;
                        session.put("count",count);
                        return LOGIN;
                    }else{
                        count++;
                        addActionMessage("您输入的用户名或密码错误"+count);
                        session.put("count",count);
                        return LOGIN;
                    }

                }
            }else{
                addActionMessage("该用户不存在,已经跳转到注册页面");
                return REG;
            }
        }catch (Exception e){
            addActionError(e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
            return LOGIN;
        }
    }

    public String reg(){
        try{
            managment=new UserManagment();
            user=new UserDao();
            ArrayList result=user.up_select(username);
            if(result.size()>0)
            {
                addActionMessage("该用户已经存在");
                return REG;
            }
            else{
                if(user.insert(username,password)){
                    managment.login(username);
                    return INDEX;
                }else{
                    addActionMessage("发生未知错误,请重试!");
                    return REG;
                }

            }
        }catch (Exception e){
                addActionError(e.getMessage());
                return REG;
        }
    }
}

    

时间: 2024-08-27 03:15:52

Struts2+DAO层实现实例03——添加监听器跟踪用户行为的相关文章

Struts2+DAO层实现实例01——搭建Struts2基本框架

实例内容 利用Strust2实现一个登陆+注册功能的登陆系统. 实现基础流程: 实现代码 JSP页面部分: <%-- 登陆页面 Created by IntelliJ IDEA. User: Richard Date: 2017/6/21 Time: 12:36 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UT

系统管理模块_部门管理_改进_抽取添加与修改JSP页面中的公共代码_在显示层抽取BaseAction_合并Service层与Dao层

系统管理模块_部门管理_改进1:抽取添加与修改JSP页面中的公共代码 commons.jspf <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <meta http-equiv="Conten

设置storage模块的数据库操作支持、添加仓储分类列表接口(dao层)

一.在storage模块的pom文件中加入jdbc,mybatis,mysql依赖 二.在yml文件中加入datasource数据库连接的四个属性 三.在启动类的包下新建controller,service,serviceImpl,dao,entity包并建好对应4个表的四组类 四.在dao层设置基础增删改查方法,添加@Repository和@Mapper注解 1.StorageTopTypeDao: 2.StorageDao: 3.StorageLogDao(发现表设计不合理,改变字段): 4

【j2ee spring】29、巴巴运动网-整合hibernate4+spring4(4)DAO层

巴巴运动网-整合hibernate4+spring4(3)DAO层 1.项目图解 2.首先我们引入相应的jar包 3.我们配置一下数据库中相应的实体对象 ProductType.java /** * 功能:这是产品类别的 * 文件:ProductType.java * 时间:2015年5月12日10:16:21 * 作者:cutter_point */ package com.cutter_point.bean.product; import javax.persistence.Column;

[转]JAVA中Action层, Service层 ,modle层 和 Dao层的功能区分

首先这是现在最基本的分层方式,结合了SSH架构.modle层就是对应的数据库表的实体类.Dao层是使用了Hibernate连接数据库.操作数据库(增删改查).Service层:引用对应的Dao数据库操作,在这里可以编写自己需要的代码(比如简单的判断).Action层:引用对应的Service层,在这里结合Struts的配置文件,跳转到指定的页面,当然也能接受页面传递的请求数据,也可以做些计算处理.以上的Hibernate,Struts,都需要注入到Spring的配置文件中,Spring把这些联系

框架 day65 Mybatis入门(基础知识:框架原理,入门[curd],开发dao层,全局与映射配置)

Mybatis 基础知识(一) 第一天:基础知识(重点) mybatis介绍 mybatis框架原理(掌握) mybaits入门程序(掌握) 用户信息进行增.删.改.查 mybatis开发dao层方法:(掌握) 原始dao开发方法(dao接口和实现类需要程序员编写) mapper代理开发方法(程序员只需要编写接口) SqlMapConfig.xml(mybatis全局配置文件)(掌握) mybatis输入映射(掌握) mybatis输出映射(掌握) mybatis动态sql(掌握)   1   

ssh_maven的搭建之dao层的开发

之前都是使用我们传统的方式进行引入jar包,现在我们使用maven进行管理依赖,这样,我们的jar就不需要我们进行管理,而且,我们的maven还可以进行项目构建,一个项目从编写源代码到编译,测试,运行,打包,部署的过程,只需要对应的一个命令即可以了,方便了很多 这里,我们直接进入主题,像eclipse中怎么配置maven等这里我们暂时不说,直接从创建项目开始,而且我们采用的是纯配置文件的方式 我们可以看一下我们的目录结构 这咯我们的ssh_parent是父工程,而剩下的都是它的子模块 这里我们的

基于Mybatis的Dao层的开发

基于Mybatis的Dao层开发 SqlSessionFactoryBuilder用于创建SqlSessionFacoty,SqlSessionFacoty一旦创建完成就不需要SqlSessionFactoryBuilder了,因为SqlSession是通过SqlSessionFactory生产,所以可以将SqlSessionFactoryBuilder当成一个工具类使用,最佳使用范围是方法范围即方法体内局部变量. SqlSessionFactory是一个接口,接口中定义了openSession

obj-c编程15[Cocoa实例03]:MVC以及归档化示例

前面的博文里介绍了归档和解档,这里我们把它实际应用到一个简单的代码中去,将它作为一个多文档应用程序的打开和保存的背后支持.另外这里介绍一下MVC思想,这个在任何语言里都会有,它是一种设计思想,主要可以概括为一个程序由3部分组成: 1 模式:是程序的数据支持: 2 视图:是程序的表示支持: 3 控制:连接模式和视图,将程序构为一个整体: Cocoa框架中对MVC提供了非常好的支持,你只需要写很少的代码就可以完成一个程序的MVC绑定了.下面的例子中,我生成一个基于多文档的程序,使用了NSArrayC