实现目的:

      1.点击“Login”的超链接。进入登录页面
      2.填写登录信息。提交表单,将用户信息保存进Session
      3.显示用户名,并计算在线人数
      4.点击“Logout”的超链接。在线人数减一,并使Session失效

Struts2实现:

      1.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2-LoginAndLogout</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

      2.导入Struts2所需的基本jar包

      
      3.编写Action类
package com.lewa.action;

import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.SessionAware; public class LoginAction implements SessionAware,ApplicationAware{ private static final String SUCCESS = "success"; private static final String LOGOUT="logout";
//与表单域中的属性相相应
private String username;
//JavaBean-style
public void setUsername(String username){
this.username=username;
} public String execute(){
//将用户信息保存到Session中
//1.获取Session。通过SessionAware接口实现 //2.获取登录信息 //3.把登录信息保存到Session中
session.put("username", username); //在线人数 +1
//1.获取当前在线人数,从Application中获取
Integer count=(Integer) application.get("count");
if(count==null){
count=0;
} //使当前在线人数 +1
count++;
application.put("count", count);
return SUCCESS;
} public String Logout(){
//在线人数 -1
//1.获取在线人数
Integer count=(Integer) application.get("count"); //2.在在线人数>0的情况下 -1
if(count!=null && count!=0){
count--;
application.put("count", count);
} //使Session失效
((SessionMap)session).invalidate(); return LOGOUT;
}
private Map<String,Object> session;
@Override
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session = session;
} private Map<String,Object> application;
@Override
public void setApplication(Map<String, Object> application) {
// TODO Auto-generated method stub
this.application = application;
}
}

      4.在struts.xml中配置Action

<?

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>
<!--以常量配置的形式改动default.properties里所配置的action.extension -->
<constant name="struts.action.extension" value="action,do,,"/> <package name="LoginAndLogout" extends="struts-default">
<!--
> Action类默觉得ActionSupport
> 运行的方法默觉得execute
> result默觉得success
以上三者能够省略不写
--> <!-- 进入登录页面的Action配置 -->
<action name="login-ui" class="com.opensymphony.xwork2.ActionSupport" method="execute">
<result name="success">/login.jsp</result>
</action> <!-- 登录提交表单的Action配置 -->
<action name="login" class="com.lewa.action.LoginAction">
<result>/login-success.jsp</result>
</action> <!-- 登出的Action配置 -->
<action name="logout" class="com.lewa.action.LoginAction" method="Logout">
<result name="logout">/logout-success.jsp</result>
</action>
</package>
</struts>

      5.相应的JSP页面

      
        index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index</title>
</head>
<body>
<a href="login-ui.do">Login</a>
</body>
</html>

      login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<!-- struts2 UI 表单便签 -->
<s:form action="login.do" method="post">
<s:textfield name="username" label="用户名"/>
<s:submit value="Login"/>
</s:form>
</body>
</html>

      login-success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login-success</title>
</head>
<body>
<h3>使用EL</h3>
欢迎,${sessionScope.username } <br><br> 当前在线人数:${applicationScope.count } <br><br> <h3>使用OGNL和Struts2 标签</h3>
欢迎,<s:property value="#session.username"/> <br><br> 当前在线人数:<s:property value="#application.count"/> <br><br> <a href="logout.action">Logout</a>
</body>
</html>

      logout-success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Log out</title>
</head>
<body>
当前在线人数:${applicationScope.count }
</body>
</html>

      6.用户请求的扩展名问题

      > org/apache/struts2/default.properties: 配置了struts2应用的一些常量 
   > struts.action.extension=action, ,  表示配置struts2受理请求扩展名(默认以.action或者无扩展)
   > 在struts.xml文件里以常量配置的方式改动default.properties所配置的常量:
     <constant name="struts.action.extension" value="action,do,,"></constant>

   7.web应用结构文件夹

     

struts2学习笔记(二)—— 获取登录信息及计算在线人数的更多相关文章

  1. openresty 学习笔记二:获取请求数据

    openresty 学习笔记二:获取请求数据 openresty 获取POST或者GET的请求参数.这个是要用openresty 做接口必须要做的事情.这里分几种类型:GET,POST(urlenco ...

  2. struts2学习笔记二

    一.分文件编写框架配置文件 1.不分文件开发可能产生的问题 就类似于我们在写java类时,所有代码都写在一个类里,甚至写在一个方法里. 当3个人都checkout了struts.xml文件时,第一个人 ...

  3. Struts2学习笔记二 配置详解

    Struts2执行流程 1.简单执行流程,如下所示: 在浏览器输入请求地址,首先会被过滤器处理,然后查找主配置文件,然后根据地址栏中输入的/hello去每个package中查找为/hello的name ...

  4. struts2学习笔记--总结获取servletAPI的几种方式

    struts2的Action放弃了request,response等ServletAPI,使得在业务层上更加独立,在有时候使用struts2进行Web开发的时候,不可避免的要在action中使用ser ...

  5. Struts2学习笔记(二):第一个Struts2应用

    一.创建Action类. 创建工程Struts2Demo struts 2中的Action类并不需要继承struts 2中的某个父类,普遍的java类就可以. 在org.sunny.user.acti ...

  6. Struts2学习笔记(二)——配置详解

    1.Struts2配置文件加载顺序: default.properties(默认常量配置) struts-default.xml(默认配置文件,主要配置bean和拦截器) struts-plugin. ...

  7. Struts2学习笔记二:开发流程

    一:创建项目,添加依赖包 二:在web.xml配置核心控制器 <filter> <filter-name>struts2</filter-name> <fil ...

  8. struts2学习笔记 day02 获取参数 访问ServletAPI 结果类型

  9. python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码

    python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码 淘宝IP地址库 http://ip.taobao.com/目前提供的服务包括:1. 根据用户提供的 ...

随机推荐

  1. windows ubuntu bcdeditor

    双系统. 先装windows,后装ubuntu12.04 为了避免grub引导,所以安装bcdeditor. 平时使用没有什么不适,可是每次linux升级内核以后,grub列表可能就会消失,自然是不能 ...

  2. (转)淘淘商城系列——使用maven构建工程

    http://blog.csdn.net/yerenyuan_pku/article/details/72669269 开发工具和环境 这里,我统一规范一下淘淘商城的开发工具和环境,如下: Eclip ...

  3. QList模板类常用接口函数

    插入操作:insert()函数原型:void QList::insert(int i, const T &value) 在索引后插入值 i:索引 value:插入值 Example: QLis ...

  4. Java8新特性 Stream流式思想(三)

    Stream接口中的常用方法 forEach()方法package cn.com.cqucc.demo02.StreamMethods.Test02.StreamMethods; import jav ...

  5. 【Linux】CentOS tar压缩与解压命令大全

    tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...

  6. 【git】搭建git服务器

    在 Linux 下搭建 Git 服务器 目录 ① 安装 Git ② 服务器端创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码 ③ 服务器端创建 Git 仓库 ④ 客户端 clon ...

  7. redisd的非持久化配置

    如何关闭redis持久化?我的需求是只把redis当作缓存来用,所以持久化到硬盘对我的需求来说没有意义. 修改redis配置文件,redis.conf 第115行左右. 1.注释掉原来的持久化规则 # ...

  8. ubuntu18.04 frpc安装与自动启动

    1. 下载, 解压 export FRP_VERSION='0.25.3' wget --no-check-certificate https://github.com/fatedier/frp/re ...

  9. hive纯命令行

    vim /etc/profileexport HIVE_HOME=/export/servers/hive...export PATH=:$HIVE_HOME/bin:$PATH 前台启动hive:h ...

  10. 阿里云报错Redirecting to /bin/systemctl restart sshd.service

    转:http://blog.csdn.net/caijunfen/article/details/70599138 云服务器 ECS Linux CentOS 7 下重启服务不再通过 service  ...